<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Cyh的博客

    Email:kissyan4916@163.com
    posts - 26, comments - 19, trackbacks - 0, articles - 220

    JavaMail(3)--給多人發送郵件

    Posted on 2009-05-31 00:45 啥都寫點 閱讀(8378) 評論(0)  編輯  收藏 所屬分類: J2SE
    關鍵技術:
    • MimeMessage的setRecipients方法設置郵件的收件人,其中Message.RecipientType.TO常量表示收件人類型是郵件接收者,Message.RecipientType.CC常量表示收件人類型是抄送者,Message.RecipientType.BCC常量表示收件人的類型是密送著。
    • 在調用MimeMessage的setRecipients方法時,除了可以指定收件人的類型外,還可以傳入一個數組,指定多個收件人的地址。

    package book.email;

    import java.util.Date;

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



                                                                                                           --    學海無涯
            

    主站蜘蛛池模板: 亚洲综合一区二区| 美女被羞羞网站免费下载| 男女超爽刺激视频免费播放| 亚洲国产精品成人午夜在线观看| 亚洲精品国产精品国自产观看| 91精品国产免费久久国语蜜臀| 亚洲日本成本人观看| 亚洲综合精品香蕉久久网| 久久久久免费看黄A片APP| 激情吃奶吻胸免费视频xxxx| 亚洲国产精品久久久久婷婷软件| 无码人妻精品一二三区免费| 在线视频网址免费播放| 色偷偷亚洲女人天堂观看欧| 亚洲色爱图小说专区| 日本精品人妻无码免费大全| 久久国产精品免费一区二区三区| 亚洲AV无码一区二区三区在线| 老司机亚洲精品影视www| 久久久久免费看黄A片APP| A级毛片高清免费视频在线播放| 亚洲性无码AV中文字幕| 亚洲国产精品久久| 亚洲精品国产精品国自产观看| 久久久久国色AV免费观看性色| 国产一区二区三区免费观在线| 亚洲AV永久无码天堂影院| 亚洲精品资源在线| 亚洲美女又黄又爽在线观看| 成人在线视频免费| 5g影院5g天天爽永久免费影院 | 亚洲最大黄色网站| 激情综合色五月丁香六月亚洲| 久久久久免费看黄A片APP| 久久精品无码专区免费青青 | 成年女人免费视频播放77777 | 国产亚洲精品资在线| 免费无码又爽又刺激高潮| 最近2019年免费中文字幕高清| 国产VA免费精品高清在线| 亚洲a∨国产av综合av下载 |