java 給多人發送、抄送


關鍵技術:	
	1、MimeMessage的setRecipients方法設置郵件的收件人,其中Message.RecipientType.TO常量表示收件人類型是郵件接收者,Message.RecipientType.
CC常量表示收件人類型是抄送者,Message.RecipientType.BCC常量表示收件人的類型是密送着。 2、在調用MimeMessage的setRecipients方法時,除了可以指定收件人的類型外,還可以傳入一個數組,指定多個收件人的地址。 SourceCode: import javax.mail.Address; import javax.mail.BodyPart; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Multipart; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; public class MultiMailsender { /** * 發送郵件給多個接收者 * @param mailInfo 帶發送郵件的信息 * @return */ public static boolean sendMailtoMultiReceiver(MultiMailSenderInfo mailInfo){ MyAuthenticator authenticator = null; if (mailInfo.isValidate()) { authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword()); } Session sendMailSession = Session.getInstance(mailInfo .getProperties(), authenticator); try { Message mailMessage = new MimeMessage(sendMailSession); // 創建郵件發送者地址 Address from = new InternetAddress(mailInfo.getFromAddress()); mailMessage.setFrom(from); // 創建郵件的接收者地址,並設置到郵件消息中 Address[] tos = null; String[] receivers = mailInfo.getReceivers(); if (receivers != null){ // 為每個郵件接收者創建一個地址 tos = new InternetAddress[receivers.length + 1]; tos[0] = new InternetAddress(mailInfo.getToAddress()); for (int i=0; i<receivers.length; i++){ tos[i+1] = new InternetAddress(receivers[i]); } } else { tos = new InternetAddress[1]; tos[0] = new InternetAddress(mailInfo.getToAddress()); } // 將所有接收者地址都添加到郵件接收者屬性中 mailMessage.setRecipients(Message.RecipientType.TO, tos); mailMessage.setSubject(mailInfo.getSubject()); mailMessage.setSentDate(new Date()); // 設置郵件內容 Multipart mainPart = new MimeMultipart(); BodyPart html = new MimeBodyPart(); html.setContent(mailInfo.getContent(), "text/html; charset=GBK"); mainPart.addBodyPart(html); mailMessage.setContent(mainPart); // 發送郵件 Transport.send(mailMessage); return true; } catch (MessagingException ex) { ex.printStackTrace(); } return false; } /** * 發送帶抄送的郵件 * @param mailInfo 待發送郵件的消息 * @return */ public static boolean sendMailtoMultiCC(MultiMailSenderInfo mailInfo){ MyAuthenticator authenticator = null; if (mailInfo.isValidate()) { authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword()); } Session sendMailSession = Session.getInstance(mailInfo .getProperties(), authenticator); try { Message mailMessage = new MimeMessage(sendMailSession); // 創建郵件發送者地址 Address from = new InternetAddress(mailInfo.getFromAddress()); mailMessage.setFrom(from); // 創建郵件的接收者地址,並設置到郵件消息中 Address to = new InternetAddress(mailInfo.getToAddress()); mailMessage.setRecipient(Message.RecipientType.TO, to); // 獲取抄送者信息 String[] ccs = mailInfo.getCcs(); if (ccs != null){ // 為每個郵件接收者創建一個地址 Address[] ccAdresses = new InternetAddress[ccs.length]; for (int i=0; i<ccs.length; i++){ ccAdresses[i] = new InternetAddress(ccs[i]); } // 將抄送者信息設置到郵件信息中,注意類型為Message.RecipientType.CC mailMessage.setRecipients(Message.RecipientType.CC, ccAdresses); } mailMessage.setSubject(mailInfo.getSubject()); mailMessage.setSentDate(new Date()); // 設置郵件內容 Multipart mainPart = new MimeMultipart(); BodyPart html = new MimeBodyPart(); html.setContent(mailInfo.getContent(), "text/html; charset=GBK"); mainPart.addBodyPart(html); mailMessage.setContent(mainPart); // 發送郵件 Transport.send(mailMessage); return true; } catch (MessagingException ex) { ex.printStackTrace(); } return false; } public static void main(String[] args) { // 創建郵件信息 MultiMailSenderInfo mailInfo = new MultiMailSenderInfo(); mailInfo.setMailServerHost("smtp.sina.com.cn"); mailInfo.setMailServerPort("25"); mailInfo.setValidate(true); mailInfo.setUserName("***"); mailInfo.setPassword("***"); mailInfo.setFromAddress("***@sina.com"); mailInfo.setToAddress("***@163.com"); mailInfo.setSubject("MyMail測試"); mailInfo.setContent("我的郵件測試/n/rMy test mail/n/r"); String[] receivers = new String[]{"***@163.com", "***@tom.com"}; String[] ccs = receivers; mailInfo.setReceivers(receivers); mailInfo.setCcs(ccs); MultiMailsender.sendMailtoMultiReceiver(mailInfo); MultiMailsender.sendMailtoMultiCC(mailInfo); } /** * 發送多接收者類型郵件的基本信息 */ public static class MultiMailSenderInfo extends MailSenderInfo{ // 郵件的接收者,可以有多個 private String[] receivers; // 郵件的抄送者,可以有多個 private String[] ccs; public String[] getCcs() { return ccs; } public void setCcs(String[] ccs) { this.ccs = ccs; } public String[] getReceivers() { return receivers; } public void setReceivers(String[] receivers) { this.receivers = receivers; } } }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM