Java Mail多人群发与多附件发送




  1. 相当于Maven的groupId,artifactId,version

    构建后应该引用了两个JAR:。如果第二个JAR没有的话,大家可以去Maven库找一下,就不多讲述了。

    实现的源码如下,一般直接拿来就可以用了~





  2. import java.io.UnsupportedEncodingException;  
  3. import java.util.ArrayList;  
  4. import java.util.Date;  
  5. import java.util.List;  
  6. import java.util.Properties;  
  7.   
  8. import javax.activation.DataHandler;  
  9. import javax.activation.FileDataSource;  
  10. import javax.mail.Message;  
  11. import javax.mail.MessagingException;  
  12. import javax.mail.Multipart;  
  13. import javax.mail.Session;  
  14. import javax.mail.Transport;  
  15. import javax.mail.internet.AddressException;  
  16. import javax.mail.internet.InternetAddress;  
  17. import javax.mail.internet.MimeBodyPart;  
  18. import javax.mail.internet.MimeMessage;  
  19. import javax.mail.internet.MimeMultipart;  
  20. import javax.mail.internet.MimeUtility;  
  21.   
  22.   
  23. /** 
  24.  * Java Mail 工具类 
  25.  *  
  26.  * @author XueQi 
  27.  * @version 1.0 
  28.  *  
  29.  */  
  30. public class MailUtils {  
  31.     private static String host;  
  32.     private static String username;  
  33.     private static String password;  
  34.     private static String from;  
  35.     private static String nick;  
  36.   
  37.     static {  
  38.         try {  
  39.             // Test Data  
  40.             host = "smtp.163.com";  
  41.             username = "邮箱用户名";  
  42.             password = "邮箱密码";  
  43.             from = "xxx@abc.com";  
  44.             nick = "测试admin";  
  45.             // nick + from 组成邮箱的发件人信息  
  46.         } catch (Exception e) {  
  47.             e.printStackTrace();  
  48.         }  
  49.     }  
  50.   
  51.     /** 
  52.      * 发送邮件 
  53.      *  
  54.      * @param to 
  55.      *            收件人列表,以","分割 
  56.      * @param subject 
  57.      *            标题 
  58.      * @param body 
  59.      *            内容 
  60.      * @param filepath 
  61.      *            附件列表,无附件传递null 
  62.      * @return 
  63.      * @throws MessagingException 
  64.      * @throws AddressException 
  65.      * @throws UnsupportedEncodingException 
  66.      */  
  67.     public static boolean sendMail(String to, String subject, String body,  
  68.             List<String> filepath) throws AddressException, MessagingException,  
  69.             UnsupportedEncodingException {  
  70.         // 参数修饰  
  71.         if (body == null) {  
  72.             body = "";  
  73.         }  
  74.         if (subject == null) {  
  75.             subject = "无主题";  
  76.         }  
  77.         // 创建Properties对象  
  78.         Properties props = System.getProperties();  
  79.         // 创建信件服务器  
  80.         props.put("mail.smtp.host", host);  
  81.         props.put("mail.smtp.auth""true"); // 通过验证  
  82.         // 得到默认的对话对象  
  83.         Session session = Session.getDefaultInstance(props, null);  
  84.         // 创建一个消息,并初始化该消息的各项元素  
  85.         MimeMessage msg = new MimeMessage(session);  
  86.         nick = MimeUtility.encodeText(nick);  
  87.         msg.setFrom(new InternetAddress(nick + "<" + from + ">"));  
  88.         // 创建收件人列表  
  89.         if (to != null && to.trim().length() > 0) {  
  90.             String[] arr = to.split(",");  
  91.             int receiverCount = arr.length;  
  92.             if (receiverCount > 0) {  
  93.                 InternetAddress[] address = new InternetAddress[receiverCount];  
  94.                 for (int i = 0; i < receiverCount; i++) {  
  95.                     address[i] = new InternetAddress(arr[i]);  
  96.                 }  
  97.                 msg.addRecipients(Message.RecipientType.TO, address);  
  98.                 msg.setSubject(subject);  
  99.                 // 后面的BodyPart将加入到此处创建的Multipart中  
  100.                 Multipart mp = new MimeMultipart();  
  101.                 // 附件操作  
  102.                 if (filepath != null && filepath.size() > 0) {  
  103.                     for (String filename : filepath) {  
  104.                         MimeBodyPart mbp = new MimeBodyPart();  
  105.                         // 得到数据源  
  106.                         FileDataSource fds = new FileDataSource(filename);  
  107.                         // 得到附件本身并至入BodyPart  
  108.                         mbp.setDataHandler(new DataHandler(fds));  
  109.                         // 得到文件名同样至入BodyPart  
  110.                         mbp.setFileName(fds.getName());  
  111.                         mp.addBodyPart(mbp);  
  112.                     }  
  113.                     MimeBodyPart mbp = new MimeBodyPart();  
  114.                     mbp.setText(body);  
  115.                     mp.addBodyPart(mbp);  
  116.                     // 移走集合中的所有元素  
  117.                     filepath.clear();  
  118.                     // Multipart加入到信件  
  119.                     msg.setContent(mp);  
  120.                 } else {  
  121.                     // 设置邮件正文  
  122.                     msg.setText(body);  
  123.                 }  
  124.                 // 设置信件头的发送日期  
  125.                 msg.setSentDate(new Date());  
  126.                 msg.saveChanges();  
  127.                 // 发送信件  
  128.                 Transport transport = session.getTransport("smtp");  
  129.                 transport.connect(host, username, password);  
  130.                 transport.sendMessage(msg,  
  131.                         msg.getRecipients(Message.RecipientType.TO));  
  132.                 transport.close();  
  133.                 return true;  
  134.             } else {  
  135.                 System.out.println("None receiver!");  
  136.                 return false;  
  137.             }  
  138.         } else {  
  139.             System.out.println("None receiver!");  
  140.             return false;  
  141.         }  
  142.     }  
  143.   
  144.     public static void main(String[] args) throws AddressException,  
  145.             UnsupportedEncodingException, MessagingException {  
  146.         List<String> filepath = new ArrayList<>();  
  147.         filepath.add("E:\\Resources\\Development Test\\AuctionServer\\src\\main\\java\\com\\auction\\dao\\IBaseDAO.java");  
  148.         filepath.add("E:\\Resources\\Development Test\\AuctionServer\\src\\main\\java\\com\\auction\\dao\\IMemcacheDAO.java");  
  149.         sendMail("000000@qq.com,000002@live.cn""注册信息邮件""注册邮件,有附件",  
  150.                 filepath);  
  151.     }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值