使用javamail實現發送郵件

1.需求

公司有個需求要將頻繁登錄服務器的ip地址及時進行郵件和短信報警,故要寫一個發送郵件和短信的程序

2.問題描述

從網上down了一些java代碼,不是較爲理想,大部分都使用javamail來實現,發送到網易郵箱沒什麼問題,但qq郵箱不行,網易郵箱接收到郵件後提醒不及時,安裝了360郵箱通後還是不太理想,故還是要解決發送到QQ郵箱的問題

3.解決

最新QQ郵箱機制要將輸入密碼的地方改成開啓pop3/smtp的時候提供的驗證碼 即可

4.代碼

發送到QQ郵箱

private static boolean send_qqmail(String strMail, String strTitle, String strText){
        boolean bret = false;
        try
        {
            final Properties props = new Properties();

            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.host", "smtp.qq.com");
            //你自己的郵箱
            props.put("mail.user", "[email protected]"); 
            //你開啓pop3/smtp時的驗證碼
            props.put("mail.password", "xxxxx");
            props.put("mail.smtp.port", "25");
            props.put("mail.smtp.starttls.enable", "true");

            Authenticator authenticator = new Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    String userName = props.getProperty("mail.user");
                    String password = props.getProperty("mail.password");
                    return new PasswordAuthentication(userName, password);
                }
            };
            // 使用環境屬性和授權信息,創建郵件會話
            Session mailSession = Session.getInstance(props, authenticator);
            // 創建郵件消息
            MimeMessage message = new MimeMessage(mailSession);
            // 設置發件人
            String username = props.getProperty("mail.user");
            InternetAddress form = new InternetAddress(username);
            message.setFrom(form);

            InternetAddress to = new InternetAddress(strMail); 
            message.setRecipient(RecipientType.TO, to);

            // 設置郵件標題
            message.setSubject(strTitle);

            // 設置郵件的內容體
            message.setContent(strText, "text/html;charset=UTF-8");

            // 發送郵件
            Transport.send(message);
            bret = true;
        }
        catch (AddressException e) {
             e.printStackTrace();
        }
        catch (MessagingException e) {
             e.printStackTrace();
        }
        catch (Exception e){
            e.printStackTrace();
        }
        return bret;
    }

發送到網易郵箱

private static boolean send_163mail(String strMail, String strTitle, String strText){
        boolean bret = false;
        try
        {
            final Properties props = new Properties();
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.host", "smtp.163.com");

            // 發件人的賬號
            props.put("mail.user", "[email protected]");
            //發件人的密碼
            props.put("mail.password", "xxxx"); 

            // 構建授權信息,用於進行SMTP進行身份驗證
            Authenticator authenticator = new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    // 用戶名、密碼
                    String userName = props.getProperty("mail.user");
                    String password = props.getProperty("mail.password");
                    return new PasswordAuthentication(userName, password);
                }
            };
            // 使用環境屬性和授權信息,創建郵件會話
            Session mailSession = Session.getInstance(props, authenticator);
            // 創建郵件消息
            MimeMessage message = new MimeMessage(mailSession);
            // 設置發件人
            String username = props.getProperty("mail.user");
            InternetAddress form = new InternetAddress(username);
            message.setFrom(form);

            // 設置收件人
            InternetAddress to = new InternetAddress(strMail); 
            message.setRecipient(RecipientType.TO, to);

            // 設置郵件標題
            message.setSubject(strTitle);

            // 設置郵件的內容體
            message.setContent(strText, "text/html;charset=UTF-8");
            // 發送郵件
            Transport.send(message);
            bret = true;
        }
        catch (AddressException e) {
             e.printStackTrace();
        }
        catch (MessagingException e) {
             e.printStackTrace();
        }
        catch (Exception e){
            e.printStackTrace();
        }
        return bret;
    }

測試部分

import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.internet.AddressException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public static void main(String[] args) {
        // TODO Auto-generated method stub
        if (send_qqmail("[email protected]", "測試QQ郵箱發送", "<body><p>你們好嗎</p></body>"))
            System.out.println("QQ郵件發送成功");

        if (send_163mail("[email protected]", "測試網易郵箱發送", "<body><p>你們好嗎</p></body>"))
            System.out.println("網易郵件發送成功");
    }
備註http://www.oracle.com/technetwork/java/javamail/index-138643.html下載javamail 導入mail.jar即可運行

參考文章

http://www.cnblogs.com/hongten/archive/2011/07/26/2117431.html

http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=371

發佈了65 篇原創文章 · 獲贊 8 · 訪問量 15萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章