java實現郵件發送驗證碼

來源:互聯網
上載者:User

標籤:步驟   方式   transport   mem   throw   有關   規範   註冊   pass   

 建議不要用qq郵箱,我使用qq郵箱直接一直給我報530錯誤,我一直認為我代碼寫的有錯誤或者POP3/SMTP服務沒弄好。所以建議註冊個別的郵箱,我就申請了個網易163郵箱瞬間就好了。所以去申請個別的郵箱吧。

  用郵件發送驗證碼,首先需要jar包。我寫的代碼只需要mail.jar,沒有的話可以給我留言,我給你發個。

廢話不說了直接上代碼(這個代碼就是個工具類直接用就好)


package com.itheima.utils;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.AddressException;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeMessage;import javax.mail.internet.MimeMessage.RecipientType;public class MailUtils {    public static void sendMail(String email, String emailMsg) throws AddressException, MessagingException {        // 1.建立一個程式與郵件伺服器會話對象 Session        // 建立參數配置, 用於串連郵件伺服器的參數配置        Properties props = new Properties(); // 參數配置         props.setProperty("mail.transport.protocol", "SMTP");// 使用的協議(JavaMail規範要求)        props.setProperty("mail.host", "smtp.163.com");// // 寄件者的郵箱的 SMTP 伺服器位址        props.setProperty("mail.smtp.auth", "true");//請求認證,參數名稱與具體實現有關 指定驗證為true        // 建立驗證器        Authenticator auth = new Authenticator() {            @Override            public PasswordAuthentication getPasswordAuthentication() {                // 網易郵箱Yang_li_g 使用者名稱 hao123授權碼,改為自己的帳號和密碼                return new PasswordAuthentication("Yang_li_g", "hao123");            }        };        Session session = Session.getInstance(props, auth);        // 2.建立一個Message,它相當於是郵件內容        Message message = new MimeMessage(session);        //這裡也要改和上面對應,注意尾碼和上面設定的一樣不然會報錯        message.setFrom(new InternetAddress("[email protected]")); // 設定寄件者        message.setRecipient(RecipientType.TO, new InternetAddress(email)); // 設定發送方式與接收者        message.setSubject("使用者啟用");        // message.setText("這是一封啟用郵件,請<a href=‘#‘>點擊</a>");        message.setContent(emailMsg, "text/html;charset=utf-8");        // 3.建立 Transport用於將郵件發送        Transport.send(message);    }}這裡是servlet的代碼,響應代碼如下        // 發送啟用郵件    activeCode:是驗證碼        String emailMsg = "恭喜您註冊成功,請點擊下面的串連進行啟用賬戶" + "<a href=‘http://localhost:8080/Shop/active?activeCode="                + activeCode + "‘>" + "http://localhost:8080/Shop/active?activeCode=" + activeCode + "</a>";        try {            //user.getEmail()  :是註冊人的郵件使用者名            MailUtils.sendMail(user.getEmail(), emailMsg);        } catch (MessagingException e) {            e.printStackTrace();        }
網易郵箱開通POP3/SMTP服務的步驟

先點擊設定--》POP3/SMTP/IMAP---》找到開啟的選項點擊開啟---》開啟成功後設定密碼就好了

我這是設定好的可以看下。設定好了後就可以實現用郵件發驗證碼了。

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.