java 實現郵件發送

來源:互聯網
上載者:User

標籤:multipart   blank   auth   郵件   file   instance   lan   tde   base64   

1.下載一個mail.jar架包

http://download.csdn.net/detail/woainimax/9748744

2.AnnexMailService.java

import java.util.Properties;import javax.activation.DataHandler;import javax.activation.DataSource;import javax.activation.FileDataSource;import javax.mail.BodyPart;import javax.mail.Message;import javax.mail.Message.RecipientType;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 sun.misc.BASE64Encoder;public class AnnexMailService {    private String host = "";    private String from = "";    private String to = "";    private String affix = "";    private String affixName = "";    private String user = "";    private String pwd = "";    private String subject = "";    public void setAddress(String from, String to, String subject) {        this.from = from;        this.to = to;        this.subject = subject;    }    public void setAffix(String affix, String affixName) {        this.affix = affix;        this.affixName = affixName;    }    public void send(String host, String user, String pwd) {        this.host = host;        this.user = user;        this.pwd = pwd;        Properties props = new Properties();        props.put("mail.smtp.host", host);        props.put("mail.smtp.auth", "true");        props.put("mail.smtp.port", Integer.valueOf(465));        props.put("mail.smtp.ssl.enable", Boolean.valueOf(true));        Session session = Session.getDefaultInstance(props);        session.setDebug(true);        MimeMessage message = new MimeMessage(session);        try {            message.setFrom(new InternetAddress(this.from));            message.addRecipient(Message.RecipientType.TO, new InternetAddress(                    this.to));            message.setSubject(this.subject);            Multipart multipart = new MimeMultipart();            BodyPart contentPart = new MimeBodyPart();            contentPart.setText("第二種方法···");            multipart.addBodyPart(contentPart);            BodyPart messageBodyPart = new MimeBodyPart();            DataSource source = new FileDataSource(this.affix);            messageBodyPart.setDataHandler(new DataHandler(source));            BASE64Encoder enc = new BASE64Encoder();            messageBodyPart.setFileName("=?GBK?B?"                    + enc.encode(this.affixName.getBytes()) + "?=");            multipart.addBodyPart(messageBodyPart);            message.setContent(multipart);            message.saveChanges();            Transport transport = session.getTransport("smtp");            transport.connect(host, user, pwd);            transport.sendMessage(message, message.getAllRecipients());            transport.close();        } catch (Exception e) {            e.printStackTrace();        }    }    public static void main(String[] args) {                AnnexMailService cn = new AnnexMailService();        // 設定寄件者地址、收件者地址和郵件標題        cn.setAddress("[email protected]", "[email protected]", "text測試");        // 設定要發送附件的位置和標題        cn.setAffix("D:\\adcfg.json", "abcfg.json");        // 設定smtp伺服器以及郵箱的帳號和密碼        cn.send("smtp.163.com", "[email protected]", "****");    }}

 

java 實現郵件發送

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.