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

1.QQ郵箱設置html

  1.1 進去QQ郵箱-->設置-->帳號-->進行設置以下圖服務器

  

2.foxmail設置(因爲我要利用它收郵件)session

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

  ps:填寫的郵箱密碼是獨立密碼:須要注意的就是SSL連接要勾選;smtp端口是465對象

3.Java中代碼配置blog

  3.1 發送郵件配置代碼ip

public class MailUtils {utf-8

 public static void sendMail(String email, String emailMsg)    throws AddressException, MessagingException {   // 1.建立一個程序與郵件服務器會話對象 Sessionci

  Properties props = new Properties();   get

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("hoobey@sohu.com")); // 設置發送者

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

相關文章
相關標籤/搜索