java郵件發送

來源:互聯網
上載者:User

標籤:技術   etc   host   5折   ram   har   sls   message   否則   

package test;import javax.mail.Session;import javax.mail.Transport;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeMessage;import java.util.Date;import java.util.Properties;public class test {        public static String myEmailAccount = "[email protected]";//寄件者郵箱    public static String myEmailPassword = "751312449";//寄件者郵箱沒密碼    public static String myEmailSMTPHost = "smtp.163.com";//郵箱伺服器位址    public static String receiveMailAccount = "[email protected]";//收件者郵箱    public static void main(String[] args) throws Exception {        // 1. 建立參數配置, 用於串連郵件伺服器的參數配置        Properties props = new Properties();                    // 參數配置        props.setProperty("mail.transport.protocol", "smtp");   // 使用的協議(JavaMail規範要求)        props.setProperty("mail.smtp.host", myEmailSMTPHost);   // 寄件者的郵箱的 SMTP 伺服器位址        props.setProperty("mail.smtp.auth", "true");            // 需要請求認證        // PS: 某些郵箱伺服器要求 SMTP 串連需要使用 SSL 安全認證 (為了提高安全性, 郵箱支援SSL串連, 也可以自己開啟),        //     如果無法串連郵件伺服器, 仔細查看控制台列印的 log, 如果有有類似 “串連失敗, 要求 SSL 安全連線” 等錯誤,        //     開啟下面 /* ... */ 之間的注釋代碼, 開啟 SSL 安全連線。        /*        // SMTP 伺服器的連接埠 (非 SSL 串連的連接埠一般預設為 25, 可以不添加, 如果開啟了 SSL 串連,        //                  需要改為對應郵箱的 SMTP 伺服器的連接埠, 具體可查看對應郵箱服務的協助,        //                  QQ郵箱的SMTP(SLL)連接埠為465或587, 其他郵箱自行去查看)        final String smtpPort = "465";        props.setProperty("mail.smtp.port", smtpPort);        props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");        props.setProperty("mail.smtp.socketFactory.fallback", "false");        props.setProperty("mail.smtp.socketFactory.port", smtpPort);        */        // 2. 根據配置建立會話對象, 用於和郵件伺服器互動        Session session = Session.getDefaultInstance(props);        session.setDebug(true);                                 // 設定為debug模式, 可以查看詳細的發送 log        // 3. 建立一封郵件        MimeMessage message = createMimeMessage(session, myEmailAccount, receiveMailAccount);        // 4. 根據 Session 擷取郵件傳輸對象        Transport transport = session.getTransport();        // 5. 使用 郵箱帳號 和 密碼 串連郵件伺服器, 這裡認證的郵箱必須與 message 中的寄件者郵箱一致, 否則報錯        transport.connect(myEmailAccount, myEmailPassword);        // 6. 發送郵件, 發到所有的收件地址, message.getAllRecipients() 擷取到的是在建立郵件對象時添加的所有收件者, 抄送人, 密送人        transport.sendMessage(message, message.getAllRecipients());        // 7. 關閉串連        transport.close();    }    /**     * 建立一封只包含文本的簡單郵件     *     * @param session 和伺服器互動的會話     * @param sendMail 寄件者郵箱     * @param receiveMail 收件者郵箱     * @return     * @throws Exception     */    public static MimeMessage createMimeMessage(Session session, String sendMail, String receiveMail) throws Exception {        // 1. 建立一封郵件        MimeMessage message = new MimeMessage(session);        // 2. From: 寄件者        message.setFrom(new InternetAddress(sendMail, "智聯招聘", "UTF-8"));        // 3. To: 收件者(可以增加多個收件者、抄送、密送)        message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(receiveMail, "XX使用者", "UTF-8"));        // 4. Subject: 郵件主題        message.setSubject("打折钜惠", "UTF-8");        // 5. Content: 郵件內文(可以使用html標籤)        message.setContent("XX使用者你好, 今天全場5折, 快來搶購, 錯過今天再等一年。。。", "text/html;charset=UTF-8");        // 6. 設定發件時間        message.setSentDate(new Date());        // 7. 儲存設定        message.saveChanges();        return message;    }}
View Code

 

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.