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

來源:互聯網
上載者:User

標籤:

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 發送郵件配置代碼

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");   

props.setProperty("mail.host", "smtp.sohu.com");   

props.setProperty("mail.smtp.auth", "true");// 指定驗證為true

  // 建立驗證器   

Authenticator auth = new Authenticator() {    

  public PasswordAuthentication getPasswordAuthentication() {    

 return new PasswordAuthentication("hoobey", "1234567890");    

}  

 };

  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);  } }

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.