java mail使用qq郵箱發郵件的配置方法

來源:互聯網
上載者:User

標籤:

最近自己折騰了下Java中利用mai發送QQ郵件

1.QQ郵箱設定

  1.1 進去QQ郵箱-->設定-->帳號-->進行設定如

  

2.foxmail設定(由於我要利用它收郵件)

  2.1 參照官方的設定即可 http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=371

  ps:填寫的郵箱密碼是獨立密碼:需要注意的就是SSL連結要勾選;smtp連接埠是465

3.Java中代碼配置

  3.1 發送郵件配置代碼

//發送郵箱驗證        try {            Properties prop = new Properties();            prop.setProperty("mail.transport.protocol", "smtp");            prop.setProperty("mail.smtp.host", "smtp.qq.com");            prop.setProperty("mail.smtp.auth", "true");            prop.put("mail.smtp.port","25");            prop.setProperty("mail.debug", "true");            Authenticator authenticator = new PopAuthenticator("[email protected]", "4444444");            //建立會話            Session session = Session.getInstance(prop,authenticator);            //填寫信封寫信            Message msg = new MimeMessage(session);            msg.setFrom(new InternetAddress("[email protected]"));            msg.setRecipient(RecipientType.TO, new InternetAddress(user.getEmail()));            msg.setSubject(user.getUsername()+"啟用郵箱!");            msg.setText(user.getUsername()+",你好請到這個地址啟用你的帳號:http://www.estore.com/ActiveServlet?activecode="+user.getActivecode());            //驗證使用者名稱密碼發送郵件            Transport transport = session.getTransport();//transport.connect("[email protected]","4444444");            transport.send(msg);        }         
View Code

  3.2輔助類

public class PopAuthenticator extends Authenticator {     String userName = null;        String password = null;        public PopAuthenticator() {        }        public PopAuthenticator(String username, String password) {            this.userName = username;            this.password = password;        }        protected PasswordAuthentication getPasswordAuthentication() {            return new PasswordAuthentication(userName, password);        }}
View Code

  3.3 如果要發送html可以參考如下代碼:

MimeMessage mailMessage = new MimeMessage(sendMailSession);        mailMessage.setFrom(new InternetAddress("[email protected]"));        // Message.RecipientType.TO屬性工作表示接收者的類型為TO        mailMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(to));        mailMessage.setSubject(subject, "UTF-8");        mailMessage.setSentDate(new Date());        // MiniMultipart類是一個容器類,包含MimeBodyPart類型的對象        Multipart mainPart = new MimeMultipart();        // 建立一個包含HTML內容的MimeBodyPart        BodyPart html = new MimeBodyPart();        html.setContent(content.trim(), "text/html; charset=utf-8");        mainPart.addBodyPart(html);        mailMessage.setContent(mainPart);        Transport.send(mailMessage);
View Code

 

java mail使用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.