发送Html邮件

1.本文提供一个java编写的发送邮件工具类。直接新建工具类,复制代码即可。

import java.security.GeneralSecurityException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
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;

public class SendMailUtil {

	public static void sendMailUtil(ArrayList<String> to)
			throws GeneralSecurityException, AddressException {

		InternetAddress[] sendTo = new InternetAddress[to.size()];
		for (int i = 0; i < to.size(); i++) {
			sendTo[i] = new InternetAddress(to.get(i));
		}

		// 发件人电子邮箱
		final String from = "xxx@sineva.com.cn";
		// 指定发送邮件的主机为 smtp.qq.com
		// String host = "smtp.qq.com"; // QQ 邮件服务器
		String host = "pop.exmail.qq.com"; // 腾讯企业邮箱服务器
		// 获取系统属性
		Properties properties = System.getProperties();
		// 设置邮件服务器
		properties.setProperty("mail.smtp.host", host);
		properties.put("mail.smtp.auth", "true");
		// 关于QQ邮箱,还要设置SSL加密,加上以下代码即可
		MailSSLSocketFactory sf = new MailSSLSocketFactory();
		sf.setTrustAllHosts(true);
		properties.put("mail.smtp.ssl.enable", "true");
		properties.put("mail.smtp.ssl.socketFactory", sf);
		// 获取默认session对象
		Session session = Session.getDefaultInstance(properties, new Authenticator() {
			public PasswordAuthentication getPasswordAuthentication() {
				// 发件人邮件用户名、密码
				return new PasswordAuthentication(from, "发件人邮箱密钥"); // 发件人邮箱用户名、密码
			}
		});
		try {
			// 创建默认的 MimeMessage 对象
			MimeMessage message = new MimeMessage(session);
			// Set From: 
			message.setFrom(new InternetAddress(from));
			// Set To: 
			// message.addRecipient(Message.RecipientType.TO, new InternetAddress(tos));
			message.setRecipients(MimeMessage.RecipientType.TO, sendTo);
			// message.addRecipient(Message.RecipientType.CC,new InternetAddress(to));
			// Set Subject: 
			message.setSubject("这个是邮件的主题");
			// 设置消息体
			// message.setText("This is 测试2内容");
			// 发送 HTML 消息, 可以插入html标签
			message.setContent("这里边的内容是html代码",
					"text/html;charset=gb2312");
			// 发送
			Transport.send(message);
		} catch (MessagingException mex) {
			mex.printStackTrace();
		}
	}
}

2.测试信息

@Test 
	 public void name() throws AddressException, GeneralSecurityException {
	 	//配置收件人集合
	 	ArrayList<String> arrayList = new ArrayList<>();
		arrayList.add("xxx@qq.com"); 
		arrayList.add("xxx@163.com");
	 	SendMailUtil.sendMailUtil(arrayList)
	 }

3.所需jar包(maven)

<dependency>
	<groupId>javax.mail</groupId>
	<artifactId>mail</artifactId>
	<version>1.4.7</version>
</dependency>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值