首页 > 代码库 > JavaMail开发

JavaMail开发

---------------------------------------------发邮件------------------------------------------

需要jar包:

  javaMail:mail.jar

  jaf:activation.jar

  1 public class MailTest {
  2 
  3     /**
  4      * 发送一个正常的邮件--只含文本
  5      * @throws Exception 
  6      */
  7     @Test
  8     public void sendNormal() throws Exception {
  9         
 10         //1,设置邮箱服务器参数
 11         Properties props = new Properties();
 12         props.put("mail.transport.protocol", "smtp");    // 指定协议
 13         props.put("mail.smtp.host", "localhost");        // 主机   
 14         props.put("mail.smtp.port", 25);                // 端口
 15         props.put("mail.smtp.auth", "true");            // 用户密码认证
 16         props.put("mail.debug", "true");                // 调试模式
 17         
 18         //2,创建邮件会话对象
 19         Session session = Session.getInstance(props, new Authenticator() {
 20             @Override
 21             protected PasswordAuthentication getPasswordAuthentication() {
 22                 return new PasswordAuthentication("user1", "user1");
 23             }
 24         });
 25         
 26         MimeMessage message = new MimeMessage(session);
 27         message.setSentDate(new Date());
 28         message.setFrom(new InternetAddress("user1@test.com"));
 29         message.setRecipient(RecipientType.TO, new InternetAddress("user2@test.com"));
 30         message.setSubject("标题");
 31         message.setText("内容");
 32         
 33         Transport.send(message);
 34         
 35     }
 36     
 37     /**
 38      * 发送带图片的邮件
 39      * 
 40      */
 41     @Test
 42     public void sendPhothMail() throws Exception {
 43         //1,设置邮箱服务器参数
 44         Properties props = new Properties();
 45         props.put("mail.transport.protocol", "smtp");    // 指定协议
 46         props.put("mail.smtp.host", "localhost");        // 主机   
 47         props.put("mail.smtp.port", 25);                // 端口
 48         props.put("mail.smtp.auth", "true");            // 用户密码认证
 49 //        props.put("mail.debug", "true");                // 调试模式
 50         
 51         //2,创建邮件会话对象
 52         Session session = Session.getInstance(props, new Authenticator() {
 53             @Override
 54             protected PasswordAuthentication getPasswordAuthentication() {
 55                 return new PasswordAuthentication("user1", "user1");
 56             }
 57         });
 58         
 59         //3,邮件信息
 60         MimeMessage message = new MimeMessage(session);
 61         message.setSentDate(new Date());
 62         message.setFrom(new InternetAddress("user1@test.com"));
 63         message.setRecipient(RecipientType.TO, new InternetAddress("user2@test.com"));
 64         message.setSubject("带图片的邮件");
 65         message.setSentDate(new Date());
 66 //        message.setText("哈哈");  当使用Multipart时候,设置文本就无效
 67 
 68         /**************1. 邮件设置图片资源********************/
 69         // 1.1 创建负责复杂邮件体
 70         MimeMultipart mul = new MimeMultipart("relassssted");
 71         // 邮件体related  = 内容 +  资源
 72         MimeBodyPart content = new MimeBodyPart(); // 内容
 73         MimeBodyPart resource = new MimeBodyPart();  // 资源
 74         // 把内容资源设置到复杂邮件中
 75         mul.addBodyPart(content);
 76         mul.addBodyPart(resource);
 77         
 78         //---- 设置邮件资源-----
 79         
 80         File file = new File("f:/1.jpg");
 81         // 设置本地数据资源
 82         resource.setDataHandler(new DataHandler(new FileDataSource(file)));  
 83         // 设置网络数据
 84 //        resource.setDataHandler(new DataHandler(new URL("https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=1623986359,132242757&fm=117&gp=0.jpg")));
 85         resource.setContentID("1.jpg");  // 图片资源在邮件中的名称
 86         
 87         //---- 设置邮件内容------
 88         content.setContent("<img src=http://www.mamicode.com/‘cid:1.jpg‘ />正文邮件,你看图片!", "text/html;charset=UTF-8");
 89         
 90         // 1.2 把复杂邮件,设置到邮件对象中(message)!
 91         message.setContent(mul);
 92         
 93         message.saveChanges(); // 保存邮件
 94         
 95         Transport.send(message);
 96         
 97     }
 98     
 99     /**
100      * 发送带图片和附件的邮件
101      */
102     @Test
103     public void sendAttachMail() throws Exception {
104         //1,设置邮箱服务器参数
105         Properties props = new Properties();
106         props.put("mail.transport.protocol", "smtp");    // 指定协议
107         props.put("mail.smtp.host", "localhost");        // 主机   
108         props.put("mail.smtp.port", 25);                // 端口
109         props.put("mail.smtp.auth", "true");            // 用户密码认证
110         props.put("mail.debug", "true");                // 调试模式
111         
112         //2,创建邮件会话对象
113         Session session = Session.getInstance(props, new Authenticator() {
114             @Override
115             protected PasswordAuthentication getPasswordAuthentication() {
116                 return new PasswordAuthentication("user1", "user1");
117             }
118         });
119         
120         MimeMessage message = new MimeMessage(session);
121         message.setFrom(new InternetAddress("user1@test.com"));
122         message.setRecipient(RecipientType.TO, new InternetAddress("user2@test.com"));
123         message.setSubject("带图片和附件的邮件:");
124         message.setSentDate(new Date());
125         
126         //3.设置复杂邮件格式
127         MimeMultipart mix = new MimeMultipart();
128         //3.1附件
129         MimeBodyPart attach = new MimeBodyPart();
130         //3.2正文
131         MimeBodyPart content = new MimeBodyPart();
132         //3.3附件正文添加到复杂格式中
133         mix.addBodyPart(attach);
134         mix.addBodyPart(content);
135         
136         //3.3.1附件内容设置
137         attach.setDataHandler(new DataHandler(new FileDataSource(zipFile(new File("f:/1.jpg")))));
138         attach.setFileName(MimeUtility.encodeText("附件.zip"));
139         
140         //3.3.2正文设置
141         MimeMultipart mul = new MimeMultipart();
142         MimeBodyPart text = new MimeBodyPart();
143         MimeBodyPart img = new MimeBodyPart();
144         mul.addBodyPart(text);
145         mul.addBodyPart(img);
146         //图片文本设置
147         img.setDataHandler(new DataHandler(new FileDataSource(new File("f:/1.jpg"))));
148         img.setContentID("1.jpg");
149         text.setContent("<img src=http://www.mamicode.com/‘cid:1.jpg‘ />哈哈附件+文本+图片", "text/html;charset=utf-8");
150         //正文内容添加到邮件
151         content.setContent(mul);
152         
153         //复杂邮件格式添加到邮件主体
154         message.setContent(mix);
155         
156         //4,发送邮件
157         Transport.send(message);
158     }
159 
160     /**
161      * 数据压缩
162      * @throws IOException
163      */
164     public File zipFile(File file) throws IOException {
165         
166         String tem = "f:/2.zip";
167         
168         InputStream in = new FileInputStream(file);
169         GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(new File(tem)));
170         byte[] buf = new byte[8*1024];
171         int len = 0;
172         while ((len = in.read(buf)) != -1) {
173             out.write(buf, 0, len);
174         }
175         in.close();
176         out.close();
177         
178         File temFile = new File(tem);
179         
180         return file;
181         
182     }
183 
184 }

 

JavaMail开发