`

javaMail学习(五)——使用javaMail给Q Q 邮 箱 账 户 发简单邮件

阅读更多

代码跟网 易 邮 箱的那篇差不多,除了smtp服务器地址不同之外,还需要添加“开 启 S S L 加 密 ”的代码。

package com.wjl.mail.utils;

import java.security.GeneralSecurityException;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.Message.RecipientType;
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 com.sun.mail.util.MailSSLSocketFactory;

/**
 * 邮 件 发 送 的 工 具 类
 * Q Q 邮 箱 互 相 发 送 消 息
*/
public class MailUtils {
	private static String userName = "XXXX";//发 送邮 件的Q Q邮 箱 账 号
	private static String password = "XXXX";//授 权 码
	private static String userName2 = "XXXXX";//接 收 邮 件 的Q Q邮 箱 账 号
	private static String port = "465";//465、587
	/**
	 * 该 方 法 用 来 发 送 邮 件
	 * @param to:给 谁 发 邮 件
	 * **/
	public static void sendMain(String to) throws AddressException, MessagingException, GeneralSecurityException{
		//1、创 建 连 接 对 象,连 接 到 邮 箱 服 务 器
		Properties props = new Properties();
		//开 启 debug 调 试  
		props.setProperty("mail.debug", "true");  
		//stmp服 务 器 需 要 进 行 身 份 验 证,也 就 是 用  户 名 和 密 码 的 校 验,这 样 才 能 通 过 验 证
		props.setProperty("mail.smtp.auth", "true"); 
		//发 送 邮 件 协 议 名 称  
		props.setProperty("mail.transport.protocol", "smtp");  
		//设 置 邮 件服 务 器 主 机 名  
		props.setProperty("mail.host", "smtp.qq.com");//设 置 成 q q 的发 件 服 务 器:(不要使用smtp.exmail.qq.com)
		//设 端 口 号 (该 配 置 可 写 可 不 写)
		props.setProperty("mail.smtp.port", port);
		//授 权 码
		props.setProperty("mail.smtp.password",password);        
		 
		//开 启 S S L 加 密,否 则 会 失 败
		MailSSLSocketFactory sf = new MailSSLSocketFactory();
		sf.setTrustAllHosts(true);
		props.put("mail.smtp.ssl.enable", "true");
		props.put("mail.smtp.ssl.socketFactory", sf);
		
		//Authenticator:认 证 信 息
		Session session = Session.getInstance(props, new Authenticator(){
			@Override
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(userName,password);//使 用 它 给 其 他 账 户 发 邮 件
			}
		});
		
		//2、创 建 邮 件 对 象
		Message message = new MimeMessage(session);
		//2.1 设 置 发 件 人
		message.setFrom(new InternetAddress(userName));
		//2、2 设 置 收 件 人
		message.setRecipient(RecipientType.TO, new InternetAddress(to));
		//2.3 邮 件 的 主 题
		message.setSubject("测 试 发 消 息");
		//2.4 邮 件 的 正 文(即 邮 件 的 内 容)
		message.setContent("测 试 邮 件:javaMail-Q Q 邮 箱 测 试","text/html;charset=utf-8");
		
		//3.发 送 邮 件
		Transport trans = session.getTransport();
		//连 接 邮 件 服 务 器
		trans.connect(userName, password);  
		//发 送 邮 件
		trans.sendMessage(message, message.getAllRecipients());  
		//关 闭 连 接
		trans.close();
		
		//Transport.send(message);(两 种 方 式 都 可 以)
		System.out.println("发 送 成 功");
	}
	
	public static void main(String[] args) {
		try {
			sendMain(userName2);
		} catch (Exception e) {
			e.printStackTrace();
		} 
	}
}

开 启 SSL 加 密的代码:

//开 启 SSL加 密,否则会失败
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
props.put("mail.smtp.ssl.enable", "true");
props.put("mail.smtp.ssl.socketFactory", sf);

 

在FoxMail中添加Q Q 邮 箱 账 户 用来查看邮件:

注意点

1、邮 箱 必 须 开   启 S M T P ,获 取 授 权 码(如 何 获 取?点 我),且 使 用 最 新 的 授 权 码 发 邮  件。

2、必 须  添 加 开 启 SSL 加 密 的 代 码,不 然 会 报 错。

3、在 Foxmail 中 添 加 Q Q 邮 箱 账 户 时 必 须 使用 授 权 码(不 能 是 Q Q 邮 箱 密 码) ,且必须勾选“SSL”,否则将添 加 账 户失败。

 

在弄Q Q 邮 箱的过程中碰到的问题太多了,下一篇专门写问题,这里就不多说了。

  • 大小: 22.2 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics