JAVA實現郵件抄送,密送,多個附件發送


以下代碼在本地環境可以測試成功,假如部署到服務器上,無法運行成功,因為必須使用SSL,端口號為465。

 

 

 

所以需要將

此處修改為

經過測試,郵件發送成功

 

 

 

 

//原文鏈接:http://liuwanxiang.iteye.com/blog/2212309  
//需要下載mail.jar  地址:http://www.oracle.com/technetwork/java/index-138643.html
//簡單調試一下可用,替換一下郵箱和授權碼,郵箱密碼是授權碼,詳情百度。
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
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;
import javax.mail.internet.MimeUtility;

/**
 * 郵件多人發送,可設置發送,抄送,密送
 * 
 * @author zhutongyu
 * 
 */
public class SendMail {

    private static SendMail instance = null;

    private SendMail() {

    }

    public static SendMail getInstance() {
        if (instance == null) {
            instance = new SendMail();
        }
        return instance;
    }

    public void send(String to[], String cs[], String ms[], String subject,
            String content, String formEmail, String fileList[]) {
        try {
            Properties p = new Properties(); // Properties p =
            // System.getProperties();
            p.put("mail.smtp.auth", "true");
            p.put("mail.transport.protocol", "smtp");
            p.put("mail.smtp.host", "smtp.qq.com");
            p.put("mail.smtp.port", "25");
            // 建立會話
            Session session = Session.getInstance(p);
            Message msg = new MimeMessage(session); // 建立信息
            BodyPart messageBodyPart = new MimeBodyPart();
            Multipart multipart = new MimeMultipart();
            msg.setFrom(new InternetAddress(formEmail)); // 發件人

            String toList = null;
            String toListcs = null;
            String toListms = null;

            // 發送,
            if (to != null) {
                toList = getMailList(to);
                InternetAddress[] iaToList = new InternetAddress()
                        .parse(toList);
                msg.setRecipients(Message.RecipientType.TO, iaToList); // 收件人
            }

            // 抄送
            if (cs != null) {
                toListcs = getMailList(cs);
                InternetAddress[] iaToListcs = new InternetAddress()
                        .parse(toListcs);
                msg.setRecipients(Message.RecipientType.CC, iaToListcs); // 抄送人
            }

            // 密送
            if (ms != null) {
                toListms = getMailList(ms);
                InternetAddress[] iaToListms = new InternetAddress()
                        .parse(toListms);
                msg.setRecipients(Message.RecipientType.BCC, iaToListms); // 密送人
            }
            msg.setSentDate(new Date()); // 發送日期
            msg.setSubject(subject); // 主題
            msg.setText(content); // 內容
            // 顯示以html格式的文本內容
            messageBodyPart.setContent(content, "text/html;charset=gbk");
            multipart.addBodyPart(messageBodyPart);

            // 2.保存多個附件
            if (fileList != null) {
                addTach(fileList, multipart);
            }

            msg.setContent(multipart);
            // 郵件服務器進行驗證
            Transport tran = session.getTransport("smtp");
            tran.connect("smtp.qq.com", "postmaster@qq.com",
                    "asiamedia");
            tran.sendMessage(msg, msg.getAllRecipients()); // 發送
            System.out.println("郵件發送成功");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 添加多個附件
    public void addTach(String fileList[], Multipart multipart)
            throws MessagingException, UnsupportedEncodingException {
        for (int index = 0; index < fileList.length; index++) {
            MimeBodyPart mailArchieve = new MimeBodyPart();
            FileDataSource fds = new FileDataSource(fileList[index]);
            mailArchieve.setDataHandler(new DataHandler(fds));
            mailArchieve.setFileName(MimeUtility.encodeText(fds.getName(),
                    "GBK", "B"));
            multipart.addBodyPart(mailArchieve);
        }
    }

    private String getMailList(String[] mailArray) {

        StringBuffer toList = new StringBuffer();
        int length = mailArray.length;
        if (mailArray != null && length < 2) {
            toList.append(mailArray[0]);
        } else {
            for (int i = 0; i < length; i++) {
                toList.append(mailArray[i]);
                if (i != (length - 1)) {
                    toList.append(",");
                }

            }
        }
        return toList.toString();

    }

    public static void main(String args[]) {
        SendMail send = SendMail.getInstance();
        String to[] = { "zhutongyu@qq.com" };
        String cs[] = null;
        String ms[] = null;
        String subject = "測試一下";
        String content = "這是郵件內容,僅僅是測試,不需要回復";
        String formEmail = "zhutongyu@qq.com";
        String[] arrArchievList = new String[4];
        arrArchievList[0] = "c:\\2012052914033429140297.rar";
        arrArchievList[1] = "c:\\topSearch.html";
        arrArchievList[2] = "c:\\topSearch2.html";
        arrArchievList[3] = "c:\\logo_white.png";
        // 2.保存多個附件
        send.send(to, cs, ms, subject, content, formEmail, arrArchievList);
    }

}

 


免責聲明!

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



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