最近想寫一個郵箱自動驗證功能,在網上看了很多,寫到自己上面出了很多問題,記錄下來給後面的人一個參考,網上寫到

來源:互聯網
上載者:User

最近想寫一個郵箱自動驗證功能,在網上看了很多,寫到自己上面出了很多問題,記錄下來給後面的人一個參考,網上寫到

package com.app.tools;import java.util.Date;import java.util.Properties;import javax.mail.Authenticator;import javax.mail.Message;import javax.mail.MessagingException;import javax.mail.PasswordAuthentication;import javax.mail.Session;import javax.mail.Transport;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeMessage;public class SendEmail {//    public static final String HOST = "mx3.qq.com";    public static final String HOST = "smtp.163.com";    public static final String PROTOCOL = "smtp";    public static final int PORT = 25;    /*465    這是QQ的連接埠*/    public static final String FROM = "xxxxx@163.com";//寄件者的email    public static final String PWD = "xxxxxxx";//寄件者密碼    private static Session getSession() {        Properties props = new Properties();        props.put("mail.smtp.host", HOST);//設定伺服器位址          props.put("mail.store.protocol" , PROTOCOL);//設定協議          props.put("mail.smtp.port", PORT);//設定連接埠          props.put("mail.smtp.auth" , "true");        Authenticator authenticator = new Authenticator() {            @Override            protected PasswordAuthentication getPasswordAuthentication() {                return new PasswordAuthentication(FROM, PWD);            }        };        Session session = Session.getDefaultInstance(props , authenticator);        return session;    }    public static void send(String toEmail , String content) {        Session session = getSession();        try {            System.out.println("--send--"+content);            // Instantiate a message              Message msg = new MimeMessage(session);            //Set message attributes              msg.setFrom(new InternetAddress(FROM));            InternetAddress[] address = {new InternetAddress(toEmail)};            msg.setRecipients(Message.RecipientType.TO, address);            msg.setSubject("帳號啟用郵件");            msg.setSentDate(new Date());            msg.setContent(content,"text/html;charset=utf-8");            //Send the message              Transport.send(msg);        }        catch (MessagingException mex) {            mex.printStackTrace();        }    }    public static void main(String[] args) {        new SendEmail().send("xxxxx@qq.com","dsfsdf");        System.out.println("success");    }} 
請注意以下要點:

1、是否開啟了認證,QQ郵箱、163郵箱均要開啟認證

2、javax.mail.MessagingException: Could not connect to SMTP host: smtp.163.com, port: 25;      //連線逾時  

解決參考:將這個屬性的true加上引號,請奇葩的異常! 注意:如果沒有這條屬性,可能或拋出  需要認證異常(Required  Authentication) 這個異常一般是由1、2造成的

3、在設定屬性的時候,一定要注意弄清楚使用郵箱的地址和連接埠。  javax.mail.AuthenticationFailedException

 QQ郵箱連接埠:  mx3.qq.com  連接埠:465         163郵箱:smtp.163.com   連接埠:25       這是都是發送郵件時使用的smtp協議所用。如果是收郵件則用的是pop3協議,那又不一樣了。這個請自行尋找

4、

在使用QQ郵箱發送郵件的時候,出現這個異常。 查了一下,官方說法是發送太頻繁所以被鎖定了!目前還沒有解決。明天試試看看能不能成功(未成功)

 

以上是幾個容易出現的異常,解決之後,其他異常可根據具體拋出的資訊解決!

聯繫我們

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