JavaMail 发送邮件,收件人为多人,抄送多人。其中包含收件人邮箱错误时的处理

您所在的位置:网站首页 发邮件时格式错误 JavaMail 发送邮件,收件人为多人,抄送多人。其中包含收件人邮箱错误时的处理

JavaMail 发送邮件,收件人为多人,抄送多人。其中包含收件人邮箱错误时的处理

2024-06-26 17:01| 来源: 网络整理| 查看: 265

1.给客户做一个发邮件的功能。收件人和抄送人可能为单个人,也可能为多个人。但是当收件人或抄送人中某一个邮箱的格式错误时,整个邮件发送就会出错停止发送。但我还需要给那些其余的人发邮件,该怎么办?

2.解决思路,当收件人邮箱错误时,可以catch到异常,并从异常信息提取错误的邮箱,再把错误的邮箱从,收件人或抄送人中剔除,再次发送即可。

伪代码:

try{ 发送邮件 }catch(Exception e){ if(判断是否为邮件发送错误异常){ 获取错误的邮件,并从收件人或抄送人中剔除 再次发送邮件 } }

3.完整代码

@Override public void sendEmail(String main, String cc, IWorkItem weekly) throws Exception { List sendTo1 = new ArrayList(Arrays.asList(main.split(","))); List copyTo1 = new ArrayList(Arrays.asList(cc.split(","))); String mode = "client";//test / client try { // 创建一个配置文件并保存 Properties properties = new Properties(); if(mode.equals("test")) { properties.setProperty("mail.host", "smtp.qq.com"); }else { properties.setProperty("mail.host", "10.0.3.28"); } properties.setProperty("mail.transport.protocol", "smtp"); properties.setProperty("mail.smtp.auth", "true"); if(mode.equals("test")) { //QQ存在一个特性设置SSL加密 MailSSLSocketFactory sf = new MailSSLSocketFactory(); sf.setTrustAllHosts(true); properties.put("mail.smtp.ssl.enable", "true"); properties.put("mail.smtp.ssl.socketFactory", sf); } // 创建一个session对象 Session session = Session.getDefaultInstance(properties, new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { if(mode.equals("test")) { return new PasswordAuthentication("[email protected]", "asdasdasd"); }else { return new PasswordAuthentication("[email protected]", "!QAZ2wsx"); } } }); // 开启debug模式 session.setDebug(true); // 获取连接对象 Transport transport = session.getTransport(); // 连接服务器 if(mode.equals("test")) { transport.connect("smtp.qq.com", "[email protected]", "fx"); }else { transport.connect("10.0.3.28", "[email protected]", "!QAZ2wsx"); } // 创建邮件对象 MimeMessage mimeMessage = new MimeMessage(session); // 邮件发送人 if(mode.equals("test")) { mimeMessage.setFrom(new InternetAddress("[email protected]")); }else { mimeMessage.setFrom(new InternetAddress("[email protected]")); } // 邮件接收人 InternetAddress[] sendTo = InternetAddress.parse(main); mimeMessage.setRecipients(Message.RecipientType.TO, sendTo); LOG.error("sendTo====================================" + main); // 抄送 InternetAddress[] copyTo = InternetAddress.parse(cc); LOG.error("copyTo====================================" + cc); mimeMessage.setRecipients(Message.RecipientType.CC, copyTo); //邮件标题 String title = weekly.getTitle(); mimeMessage.setSubject(title + "_项目周报"); // 邮件内容 String content = getMailContent(weekly); if(mode.equals("test")) { mimeMessage.setContent(content,"text/html;charset=UTF-8"); LOG.error("content1====================================" + content); }else { mimeMessage.setContent(content,"text/html;charset=gb2312"); LOG.error("content2====================================" + content); } // 发送邮件 transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients()); // 关闭连接 transport.close(); } catch (Exception e) { if (e instanceof SendFailedException) { for(Address address: ((SendFailedException) e).getInvalidAddresses()){ LOG.error("错误信息邮箱====================================" + address.toString()); if(sendTo1.contains(address.toString())) { sendTo1.remove(address.toString()); } if(copyTo1.contains(address.toString())) { copyTo1.remove(address.toString()); } } try { // 创建一个配置文件并保存 Properties properties = new Properties(); if(mode.equals("test")) { properties.setProperty("mail.host", "smtp.qq.com"); }else { properties.setProperty("mail.host", "10.0.3.28"); } properties.setProperty("mail.transport.protocol", "smtp"); properties.setProperty("mail.smtp.auth", "true"); if(mode.equals("test")) { //QQ存在一个特性设置SSL加密 MailSSLSocketFactory sf = new MailSSLSocketFactory(); sf.setTrustAllHosts(true); properties.put("mail.smtp.ssl.enable", "true"); properties.put("mail.smtp.ssl.socketFactory", sf); } // 创建一个session对象 Session session = Session.getDefaultInstance(properties, new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { if(mode.equals("test")) { return new PasswordAuthentication("[email protected]", "foajwfjgwcihbbfa"); }else { return new PasswordAuthentication("[email protected]", "!QAZ2wsx"); } } }); // 开启debug模式 session.setDebug(true); // 获取连接对象 Transport transport = session.getTransport(); // 连接服务器 if(mode.equals("test")) { transport.connect("smtp.qq.com", "[email protected]", "foajwfjgwcihbbfa"); }else { transport.connect("10.0.3.28", "[email protected]", "!QAZ2wsx"); } // 创建邮件对象 MimeMessage mimeMessage = new MimeMessage(session); // 邮件发送人 if(mode.equals("test")) { mimeMessage.setFrom(new InternetAddress("[email protected]")); }else { mimeMessage.setFrom(new InternetAddress("[email protected]")); } String sendTo2 = ""; // 邮件接收人 for(String email : sendTo1) { sendTo2 += email + ","; } sendTo2 = sendTo2.substring(0,sendTo2.length()-1); String copyTo2 = ""; for(String email : copyTo1) { copyTo2 += email + ","; } copyTo2 = copyTo2.substring(0,copyTo2.length()-1); InternetAddress[] sendTo = InternetAddress.parse(sendTo2); mimeMessage.setRecipients(Message.RecipientType.TO, sendTo); LOG.error("sendTo2====================================" + sendTo2); // 抄送 InternetAddress[] copyTo = InternetAddress.parse(copyTo2); LOG.error("copyTo2====================================" + copyTo2); mimeMessage.setRecipients(Message.RecipientType.CC, copyTo); //邮件标题 String title = weekly.getTitle(); mimeMessage.setSubject(title + "_项目周报"); // 邮件内容 String content = getMailContent(weekly); if(mode.equals("test")) { mimeMessage.setContent(content,"text/html;charset=UTF-8"); }else { mimeMessage.setContent(content,"text/html;charset=gb2312"); } // 发送邮件 transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients()); // 关闭连接 transport.close(); }catch (Exception e1) { LOG.error("错误信息====================================" + e1.getMessage()); } } } }

 



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3