QQ邮箱发送信息

QQ邮箱发送信息

一、发送普通的信息

( 1 )、步骤

​ 【1】导入javax.mail 和 activation 这两个包(可以到Maven仓库中找)

​ 【2】使用一个Properties保存连接协议、主机名、端口名、端口号、是否使用ssl安全连接 和 是否实现debug信息

​ 【3】使用 Session.getInstance(保存好的Properties对象) 来获取会话对象

​ 【4】使用 获取到的会话对象来创建 MimeMessage 对象

​ 【5】使用 邮箱来创建 InternetAddress对象,并封装到 MimeMessage 对象中

​ 【6】在 MimeMessage 对象中设置 标题 和 内容

​ 【7】根据 Session对象来得到邮件的全部信息,然后使用该对象连接到自己的邮箱

​ 【8】发送邮件

​ 【9】关闭连接

( 2 )、代码示例

1、第一种写法:
public class MyJavaMail {
    
    @Test
    public void test01() throws MessagingException {
        // 用于封装连接信息的对象(这个对象一般用于解析 .Properties的文件)
        Properties properties = new Properties();
        // 连接协议
        properties.put("mail.transport.protocol", "smtp");
        // 主机名(官方会给指定的)
        properties.put("mail.smtp.host", "smtp.qq.com");
        // 端口号 (邮箱官方会给指定的)
        properties.put("mail.smtp.port", 465);
        // properties.put("mail.smtp.auth", "true");
        // 设置是否使用SSL安全连接 (通常使用)
        properties.put("mail.smtp.ssl.enable", "true");
        // 设置是否显示debug信息
        properties.put("mail.debug", "true");
        
        // 利用连接信息的对象创建session对象
		Session session = Session.getInstance(properties);
        
        // 利用session对象 创建邮件对象
        MimeMessage message = new MimeMessage(session);
        // 设置发件人邮箱地址
		message.setFrom(new InternetAddress("发件人的邮箱");
        // 设置收件人邮箱
		message.setRecipients(Message.RecipientType.TO, new InternetAddress[]{new InternetAddress("收件人的邮箱")});
        // 设置标题
		message.setSubject("标题");
		// 设置内容
		message.setText("内容");
                        
         // 根据session对象,得到邮件对象
		Transport transport = session.getTransport();
		// 连接自己的邮箱账户
		transport.connect("3203366196@qq.com", "msofyknbtdaidfej");

		// 发送邮件
		transport.sendMessage(message, message.getAllRecipients());
         // 关闭邮箱对象
		transport.close();
    }
    
}
2、第二种写法:

Properties :
1、表示了一个持久的属性集,可保存在流中 或者 从流中加载出来的数据
2、特别之处:一般用来加载 后缀为.properties 的文件

public class JavaxMail {
    
	@Test
	public void test02() throws MessagingException, UnsupportedEncodingException {
		// 初始化默认参数
		Properties props = new Properties();
		// 设置协议
		props.setProperty("mail.transport.protocol", "smtp");
		// 设置 主机名
		props.setProperty("mail.host", "smtp.qq.com");
		// 设置发件人的名字 
		props.setProperty("mail.user", "邮箱@前面的内容");
		// 设置 发件人的邮箱 
		props.setProperty("mail.from", "自己的邮箱");

		// 获取 Session对象
		Session session = Session.getInstance(props, null);
		// 开启调试信息
		session.setDebug(true);
		// 通过 MimeMessage 来创建 Message接口的子类
		MimeMessage message = new MimeMessage(session);

		/*
			=========== 对邮箱的基本设置 ==========
		 */
		// 设置 发件人
		String formName = MimeUtility.encodeWord("自己定义名字") + " <" + "自己的邮箱" + ">";
		InternetAddress from = new InternetAddress(formName);
        // 或 InternetAddress from = new InternetAddress("也可以是自己邮箱@前面的内容");
        // 加入到 MimeMessage对象中
		message.setFrom(from);
		// 设置 收件人
		InternetAddress to = new InternetAddress("收件人邮箱");
        // 加入到 MimeMessage对象中
		message.setRecipient(Message.RecipientType.TO, to);

		// 设置抄送人(可无)
		//List<InternetAddress> addresses = Arrays.asList(new InternetAddress("自己看着写一个邮箱"));
		//InternetAddress[] addressesArr = (InternetAddress[]) addresses.toArray();
		//message.setRecipients(Message.RecipientType.CC, addressesArr);

		// 设置密送人(可无)
		//InternetAddress toBCC = new InternetAddress("自己看着写一个邮箱");
		//message.setRecipient(Message.RecipientType.BCC, toBCC);

		// 设置邮箱主题
		message.setSubject(subject);
		// 设置内容 ( html:text/html;    纯文本:text/plain)
		message.setContent("<h1>双击666</h1>", "html/html;charset=UTF-8");

		// 保存上边设置的内容
		message.saveChanges();

		// 获取 Transport对象
		Transport transport = session.getTransport();

		// smtp验证 (发件人的密码==校验码]【注:如在 properties中配置了就不用了】)
		transport.connect(null, null, authCode);

		// 发送邮件
		transport.sendMessage(message, message.getAllRecipients());
        // 关闭连接
		transport.close();

	}

}

注意:

注意点1:授权码要和收件人的邮箱对应上,不然会报 501 错误。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值