编程语言
首页 > 编程语言> > JavaMail 发送邮件,收件人为多人,抄送多人。其中包含收件人邮箱错误时的处理

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

作者:互联网

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

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

伪代码:

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

3.完整代码

@Override
	public void sendEmail(String main, String cc, IWorkItem weekly) throws Exception {
		List<String> sendTo1 = new ArrayList(Arrays.asList(main.split(",")));
		List<String> 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("569296263@qq.com", "asdasdasd");
					}else {
						return new PasswordAuthentication("Polarion@bocfullertonbank.com", "!QAZ2wsx");
					}
				}
			});

			// 开启debug模式
			session.setDebug(true);

			// 获取连接对象
			Transport transport = session.getTransport();

			// 连接服务器
			if(mode.equals("test")) {
				transport.connect("smtp.qq.com", "569296263@qq.com", "fx");
			}else {
				transport.connect("10.0.3.28", "Polarion@bocfullertonbank.com", "!QAZ2wsx");
			}
			// 创建邮件对象
			MimeMessage mimeMessage = new MimeMessage(session);

			// 邮件发送人
			if(mode.equals("test")) {
				mimeMessage.setFrom(new InternetAddress("569296263@qq.com"));
			}else {
				mimeMessage.setFrom(new InternetAddress("Polarion@bocfullertonbank.com"));
			}

			// 邮件接收人
			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("569296263@qq.com", "foajwfjgwcihbbfa");
	    					}else {
	    						return new PasswordAuthentication("Polarion@bocfullertonbank.com", "!QAZ2wsx");
	    					}
	    				}
	    			});
	
	    			// 开启debug模式
	    			session.setDebug(true);
	
	    			// 获取连接对象
	    			Transport transport = session.getTransport();
	
	    			// 连接服务器
	    			if(mode.equals("test")) {
	    				transport.connect("smtp.qq.com", "569296263@qq.com", "foajwfjgwcihbbfa");
	    			}else {
	    				transport.connect("10.0.3.28", "Polarion@bocfullertonbank.com", "!QAZ2wsx");
	    			}
	
	    			// 创建邮件对象
	    			MimeMessage mimeMessage = new MimeMessage(session);
	
	    			// 邮件发送人
	    			if(mode.equals("test")) {
	    				mimeMessage.setFrom(new InternetAddress("569296263@qq.com"));
	    			}else {
	    				mimeMessage.setFrom(new InternetAddress("Polarion@bocfullertonbank.com"));
	    			}

	    			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());
				}
			}
		}
	}

 

标签:收件人,mimeMessage,抄送,JavaMail,new,com,properties,邮件,mode
来源: https://blog.csdn.net/qq_29622845/article/details/114365634