使用Java程式發送Email

來源:互聯網
上載者:User

標籤:

目前很多大型的網站忘記登入密碼常見的一種形式是使用郵箱找回密碼  最近做項目也有這個需求  現在總結一下  以便以後查看使用到的包有 mailapi.jar smtp.jar這兩個jar檔案  封裝發送郵件的實體 package com.tes;import java.util.Date;import java.util.Properties;import javax.mail.Authenticator;import javax.mail.Message;import javax.mail.Multipart;import javax.mail.PasswordAuthentication;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 Javen * @Email [email protected] */public class Mail {    String to = ""; // 收件者    String from = ""; // 寄件者    String host = ""; // smtp主機    String username = ""; // 使用者名稱    String password = ""; // 密碼    String subject = ""; // 郵件主題    String content = ""; // 郵件內文    public Mail() {    }    public Mail(String to, String from, String host, String username,            String password, String subject, String content) {        this.to = to;        this.from = from;        this.host = host;        this.username = username;        this.password = password;        this.subject = subject;        this.content = content;    }    /**     * 把主題轉換為中文     *      * @param strText     * @return     */    public String transferChinese(String strText) {        try {            strText = MimeUtility.encodeText(new String(strText.getBytes(),                    "GB2312"), "GB2312", "B");        } catch (Exception e) {            e.printStackTrace();        }        return strText;    }    /**     * 發送郵件     *      * @return 成功返回true,失敗返回false     */    public boolean sendMail() {        // 構造mail session        Properties props = System.getProperties();        props.put("mail.smtp.host", host);        props.put("mail.smtp.auth", "true");        Session session = Session.getDefaultInstance(props,                new Authenticator() {                    public PasswordAuthentication getPasswordAuthentication() {                        return new PasswordAuthentication(username, password);                    }                });        try {            // 構造MimeMessage並設定基本的值,建立訊息對象            MimeMessage msg = new MimeMessage(session);            // 設定訊息內容            msg.setFrom(new InternetAddress(from));            System.out.println(from);            // 把郵件地址映射到Internet地址上            InternetAddress[] address = { new InternetAddress(to) };            /**             * setRecipient(Message.RecipientType type, Address             * address),用於設定郵件的接收者。<br>             * 有兩個參數,第一個參數是接收者的類型,第二個參數是接收者。<br>             * 接收者類型可以是Message.RecipientType .TO,Message             * .RecipientType.CC和Message.RecipientType.BCC,TO表示主要接收人,CC表示抄送人             * ,BCC表示秘密抄送人。接收者與寄件者一樣,通常使用InternetAddress的對象。             */            msg.setRecipients(Message.RecipientType.TO, address);            // 設定郵件的標題            subject = transferChinese(subject);            msg.setSubject(subject);            // 構造Multipart            Multipart mp = new MimeMultipart();            // 向Multipart添加本文            MimeBodyPart mbpContent = new MimeBodyPart();            // 設定郵件內容(純文字格式)            // mbpContent.setText(content);            // 設定郵件內容(HTML格式)            mbpContent.setContent(content, "text/html;charset=utf-8");            // 向MimeMessage添加(Multipart代表本文)            mp.addBodyPart(mbpContent);            // 向Multipart添加MimeMessage            msg.setContent(mp);            // 設定郵件發送的時間。            msg.setSentDate(new Date());            // 發送郵件            Transport.send(msg);        } catch (Exception e) {            e.printStackTrace();            return false;        }        return true;    }    public String getTo() {        return to;    }    public void setTo(String to) {        this.to = to;    }    public String getFrom() {        return from;    }    public void setFrom(String from) {        this.from = from;    }    public String getHost() {        return host;    }    public void setHost(String host) {        this.host = host;    }    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 getSubject() {        return subject;    }    public void setSubject(String subject) {        this.subject = subject;    }    public String getContent() {        return content;    }    public void setContent(String content) {        this.content = content;    }}
測試向自己的郵箱發一封郵件package com.tes;/** * @author Javen * @Email [email protected] * */public class Test { public static void main(String[] args) { Mail mail = new Mail(); mail.setTo("[email protected]"); mail.setFrom("[email protected]");// 你的郵箱 mail.setHost("smtp.163.com"); //smtp.qq.com mail.setUsername("xxx");// 使用者 mail.setPassword("xxxx");// 密碼 mail.setSubject("[測試]找回您的賬戶密碼"); mail.setContent("測試發郵件"); if (mail.sendMail()) { System.out.println(" 發送成功"); } }}
轉載自:http://www.cnblogs.com/zyw-205520/p/3738258.html

 

使用Java程式發送Email

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.