程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Java發送郵件

Java發送郵件

編輯:關於JAVA
public class MailSender {
    /**
     * 發送單個郵件
     * @throws Exception
     */
    public void sendMail() throws Exception{
        Properties props = new Properties();//創建屬性對象
        props.put("mail.smtp.host", getHost());//設置smtp服務器地址
        props.put("mail.smtp.auth", "true");//設置服務器smtp需要驗證
        Session session = Session.getInstance(props, null);//創建新郵件並群發
        //Session session = Session.getDefaultInstance(props);
        //session.setDebug(true);
        MimeMessage message = new MimeMessage(session);//創建過程對象
        message.setFrom(new InternetAddress(getFromAddr()));
        message.addRecipient(Message.RecipientType.TO,new InternetAddress(getToAddr()));
        message.setSubject(getTitle());//設置主題
        Multipart multipart = new MimeMultipart();
        BodyPart contentPart = new MimeBodyPart();
        contentPart.setContent(this.getSendtext(), "text/html;charset=GBK");//設置信件內容
        multipart. addBodyPart(contentPart);
        if(getAttachPath() != null && getAttachName() != null){
            BodyPart attachmentPart= new MimeBodyPart();
            DataSource source = new FileDataSource(getAttachPath());
            attachmentPart.setDataHandler(new DataHandler(source));
           BASE64Encoder enc = new BASE64Encoder();
            attachmentPart.setFileName("=?GBK?B?"+enc.encode(getAttachName().getBytes())+"?=");
            multipart.addBodyPart(attachmentPart);
        }
        message.setContent(multipart);
        message.saveChanges();
        Transport transport = session.getTransport("smtp");
        transport.connect(host, getUsername(), getPassword());
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
    }
    /**
     * 群發郵件
     * @throws Exception
     */
    public void sendMails() throws Exception{
        Properties props = new Properties();//創建屬性對象
        props.put("mail.smtp.host", getHost());//設置smtp服務器地址
        props.put("mail.smtp.auth", "true");//設置服務器smtp需要驗證
        Session session = Session.getInstance(props, null);//創建新郵件並群發
        //Session session = Session.getDefaultInstance(props);
        //session.setDebug(true);
        MimeMessage message = new MimeMessage(session);//創建過程對象
        message.setFrom(new InternetAddress(getFromAddr()));//設置發送郵件地址
        message.setSentDate(new Date());//設置時間
        InternetAddress[] address = new InternetAddress[this.getMutliTo().length]; //群發地址
        for(int i = 0; i < this.getMutliTo().length; i++) { //循環發送
        address[i] = new InternetAddress((this.getMutliTo())[i]);
       }
     message.setRecipients(Message.RecipientType.TO, address);
       // message.addRecipient(Message.RecipientType.TO,new InternetAddress(getToAddr()));
        message.setSubject(getTitle());//設置主題
        Multipart multipart = new MimeMultipart();
        BodyPart contentPart = new MimeBodyPart();
        contentPart.setContent(this.getSendtext(), "text/html;charset=GBK");//設置信件內容
        multipart. addBodyPart(contentPart);
        if(getAttachPath() != null && getAttachName() != null){
            BodyPart attachmentPart= new MimeBodyPart();
            DataSource source = new FileDataSource(getAttachPath());
            attachmentPart.setDataHandler(new DataHandler(source));
           BASE64Encoder enc = new BASE64Encoder();
            attachmentPart.setFileName("=?GBK?B?"+enc.encode(getAttachName().getBytes())+"?=");
            multipart.addBodyPart(attachmentPart);
        }
        message.setContent(multipart);
        message.saveChanges();
        Transport transport = session.getTransport("smtp");
        transport.connect(host, getUsername(), getPassword());
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
    }
    private String host = null; //郵件服務器
    private String fromAddr = null; //發送郵件地址
    private String toAddr = null; //接收郵件地址
    private String username = null; //發送郵件用戶名
    private String password = null; //發送郵件密碼
    private String title = null; //郵件標題
    private String attachPath = null; //郵件附件路徑
    private String attachName = null; //郵件附件名
    private String sendtext = null; //郵件內容
    private String[] MutliTo = null; //群發用戶
    public String[] getMutliTo() {
return MutliTo;
}
public void setMutliTo(String[] mutliTo) {
MutliTo = mutliTo;
}
public String getSendtext() {
return sendtext;
}
public void setSendtext(String sendtext) {
this.sendtext = sendtext;
}
public String getHost() {
        return host;
    }
    public void setHost(String host) {
        this.host = host;
    }
    public String getFromAddr() {
        return fromAddr;
    }
    public void setFromAddr(String fromAddr) {
        this.fromAddr = fromAddr;
    }
    public String getToAddr() {
        return toAddr;
    }
    public void setToAddr(String toAddr) {
        this.toAddr = toAddr;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getAttachPath() {
        return attachPath;
    }
    public void setAttachPath(String attachPath) {
        this.attachPath = attachPath;
    }
    public String getAttachName() {
        return attachName;
    }
    public void setAttachName(String attachName) {
        this.attachName = attachName;
    }
}

折射配置文件的內容

#\u914d\u7f6eemail\u53d1\u9001\u6587\u4ef6
host=smtp.163.com
username=*****
password=*****
fromadd=*****@163.com
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved