javamail 学习及实例

JavaMail,是Sun发布的用来处理email的API,提供给开发者处理电子邮件相关的编程接口。通过使用它可以方便地执行一些常用的邮件传输。

可以看下《java邮件开发详解》第四章和第五章的内容,有比较详尽的主要接口的介绍,

相关API介绍参见:JavaMail API

如果要创建一幅图文并茂的邮件正文,最好不要采用在邮件正文中引用外部资源的方式,而是需要把图片文件内容当做邮件的内嵌资源处理,即把资源文件的内容和引用它的HTML邮件正文放在同一封邮件数据中,并把它们组合成MIME组合消息。要把图片当做邮件的内嵌资源进行引用,这需要把图片文件的内容引用它的邮件正文分别保存在单独的MimeBodyPart对象中,然后将这些MimeBodyPart对象加入进一个MimeMultipart对象中。

 下面将代码示例列出:

  1 /**  
  2  * @Title: Demo1.java
  3  * @Description: 群发邮件demo
  4  * @author :Xingle
  5  * @date 2014-7-14 下午12:01:19
  6  * @version   
  7  */
  8 
  9 package com.sendMail;
 10 
 11 import java.util.ArrayList;
 12 import java.util.List;
 13 import java.util.Properties;
 14 
 15 import javax.activation.DataHandler;
 16 import javax.activation.FileDataSource;
 17 import javax.mail.Authenticator;
 18 import javax.mail.Message;
 19 import javax.mail.MessagingException;
 20 import javax.mail.Multipart;
 21 import javax.mail.PasswordAuthentication;
 22 import javax.mail.Session;
 23 import javax.mail.Transport;
 24 import javax.mail.internet.InternetAddress;
 25 import javax.mail.internet.MimeBodyPart;
 26 import javax.mail.internet.MimeMessage;
 27 import javax.mail.internet.MimeMultipart;
 28 
 29 /**
 30  * 群发邮件
 31  * 
 32  * @ClassName: Demo1
 33  * @author Xingle
 34  * @date 2014-7-14 下午12:01:19
 35  */
 36 public class Demo1 {
 37 
 38     private static final String MAIL_USER = "****"; // 邮件服务器登录用户名
 39 
 40     private static final String MAIL_PASSWORD = "******"; // 邮件服务器登录密码
 41 
 42     private static final String MAIL_FROM_SMTP = "xingle@chinatelling.com"; // 发送邮件地址
 43     // 记录所有附件文件的集合
 44     List<String> attachments = new ArrayList<String>();
 45 
 46     public void attachfile(String[] fnameLs) {
 47         for (int i = 0; i < fnameLs.length; i++) {
 48             attachments.add(fnameLs[i]);
 49         }
 50     }
 51 
 52     public void sendmail(String[] mailArray, String subject, String content) {
 53 
 54         Properties props = new Properties();
 55         // 设置服务器验证
 56         props.setProperty("mail.smtp.auth", "true");
 57         // 设置传输协议
 58         props.setProperty("mail.transport.protocol", "smtp");
 59         // 选择服务类型
 60         props.setProperty("mail.host", "smtp.chinatelling.com");
 61         // 通过认证创建一个session实例
 62         Session session = Session.getInstance(props, new Authenticator() {
 63             protected PasswordAuthentication getPasswordAuthentication() {
 64                 return new PasswordAuthentication(MAIL_USER, MAIL_PASSWORD);
 65             }
 66         });
 67         // 构造Multipart
 68         Multipart mp = new MimeMultipart("related");
 69 
 70         // 向Multipart添加正文
 71         MimeBodyPart mbpContent = new MimeBodyPart();
 72 
 73         try {
 74             // mbpContent.setText(content);
 75             mbpContent.setContent(content, "text/html; charset=utf-8");
 76             // 将BodyPart添加到MultiPart中
 77             mp.addBodyPart(mbpContent);
 78         } catch (MessagingException e1) {
 79             e1.printStackTrace();
 80         }
 81         
 82         // 创建一个标示图片内容的MimeBodyPart对象,再添加到之前创建的MimeMultipart对象mp中
 83         MimeBodyPart picBodyPart = new MimeBodyPart();
 84         FileDataSource fds = new FileDataSource("D:/other/1.jpg");
 85         try {
 86             picBodyPart.setDataHandler(new DataHandler(fds));
 87             picBodyPart.setContentID("1_jpg");
 88             mp.addBodyPart(picBodyPart);
 89         } catch (MessagingException e1) {
 90             e1.printStackTrace();
 91         }
 92 
 93         // 向Multipart添加附件
 94         // 遍历附件列表,将所有文件添加到邮件消息里
 95         for (String efile : attachments) {
 96             MimeBodyPart mbpFile = new MimeBodyPart();
 97             // 以文件名创建FileDataSource对象
 98             FileDataSource fds1 = new FileDataSource(efile);
 99             // 处理附件
100             try {                
101                 mbpFile.setDataHandler(new DataHandler(fds1));
102                 mbpFile.setFileName(fds1.getName());
103                 // 将BodyPart添加到MultiPart中
104                 mp.addBodyPart(mbpFile);
105             } catch (MessagingException e) {
106                 e.printStackTrace();
107             }
108         }
109         // 清空附件列表
110         attachments.clear();
111         // 显示邮件发送过程中的交互信息
112         session.setDebug(true);
113 
114         Message msg = new MimeMessage(session);
115         Transport transport = null;
116         try {
117             // 邮件发送人
118             msg.setFrom(new InternetAddress(MAIL_FROM_SMTP));
119             // 邮件主题
120             msg.setSubject(subject);
121             // 邮件内容
122             msg.setContent(mp);
123             int len = mailArray.length;
124             InternetAddress address[] = new InternetAddress[len];
125             for (int i = 0; i < mailArray.length; i++) {
126                 address[i] = new InternetAddress(mailArray[i]);
127             }
128             // 邮件接收方
129             msg.addRecipients(Message.RecipientType.TO, address);
130             transport.send(msg);
131         } catch (Exception e) {
132             e.printStackTrace();
133         } finally {
134             try {
135                 if (transport != null) {
136                     transport.close();
137                 }
138             } catch (Exception e) {
139                 e.printStackTrace();
140             }
141         }
142     }
143 
144     public static void main(String[] args) throws Exception {
145         Demo1 demo2 = new Demo1();
146         String[] mailArray = { "xingle0917@gmail.com", "450137300@qq.com" };
147         String subject = "测试邮件群发";
148         String content = "<H1>hello,这里是群发邮件测试demo</H1> "
149                 + " <a href = http://www.baidu.com/> 测试链接</a>"
150                 + " <img src="cid:1_jpg">";
151         // 附件列表
152         String[] attachLs = { "D:/other/fujian2.docx", "D:/other/fujian1.txt" };
153         // 粘贴附件
154         demo2.attachfile(attachLs);
155         demo2.sendmail(mailArray, subject, content);
156     }
157 
158 }
原文地址:https://www.cnblogs.com/xingele0917/p/3842936.html