【工具笔记】发送邮件

该博客介绍了一种使用JavaMail API发送带有附件的电子邮件的方法,特别是针对QQ邮箱的配置。代码示例展示了如何设置SMTP服务器属性、创建认证器、构建MimeMessage对象并添加附件和正文内容。此外,还提供了发送邮件的完整流程,包括设置debug模式以查看日志。
摘要由CSDN通过智能技术生成
		<dependency>
		    <groupId>com.sun.mail</groupId>
		    <artifactId>javax.mail</artifactId>
		    <version>1.6.2</version>
		</dependency>
import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
import javax.mail.util.ByteArrayDataSource;

/**
 * 邮件工具类
 * @author xiaokong
 *
 */
public class EmailUtil {
	public static void sendMail(String receiveName, String htmlContent, String subjectText, File []files) throws Exception {
		sendMail("smtp.qq.com", "dhbpmasfptoscagb", "530457326@qq.com", receiveName, htmlContent, subjectText, files);
	}
	
	public static void sendMail(String host, String password, String username, String receiveName, String htmlContent, String subjectText, File []files) throws Exception {
		// 参数设置
		Properties properties = new Properties();
		/*
		properties.setProperty("mail.transport.protocol", "smtp");
		properties.setProperty("mail.smtp.host", host);
		properties.setProperty("mail.smtp.auth", "true");
		properties.put("mail.smtp.starttls.enable", "true");
		*/
		
		properties.setProperty("mail.host", "smtp.qq.com");
		properties.setProperty("mail.transport.protocol", "smtp");
		properties.setProperty("mail.smtp.auth", "true");
		properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
		properties.setProperty("mail.smtp.port", "465");
		properties.setProperty("mail.smtp.socketFactory.port", "465");

		Session session = Session.getInstance(properties, new Authenticator() {
			@Override
			protected PasswordAuthentication getPasswordAuthentication() {
				// 在session中设置账户信息,Transport发送邮件时会使用W
				return new PasswordAuthentication(username, password);
			}
		});
		//设置为debug模式,查看日志
		session.setDebug(true); 

		MimeMessage mimeMessage = createMessage(session, username, receiveName, htmlContent, subjectText, files);
		// 发送邮件
		Transport.send(mimeMessage, mimeMessage.getAllRecipients());
	}

	private static MimeMessage createMessage(Session session, String username, String receiveName,
			String htmlContent, String subjectText, File []files) throws Exception {
		// 邮件
		MimeMessage mimeMessage = new MimeMessage(session);
		// 设置主题
		mimeMessage.setSubject(subjectText);
		// 发件人  MimeUtility.encodeText(username)中文处理
		mimeMessage.setFrom(new InternetAddress("\"" + MimeUtility.encodeText(username) + "\"<" + username + ">"));

		// 附件
		MimeMultipart mimeMultipart = new MimeMultipart("mixed");// 混合的组合关系
		mimeMessage.setContent(mimeMultipart);
		if (files != null) {
			for (int i = 0; i < files.length; i++) {
				File file = files[i];
				MimeBodyPart mimeBodyPart = new MimeBodyPart();
				mimeMultipart.addBodyPart(mimeBodyPart);
				FileInputStream fis = null;
				try {
					fis = new FileInputStream(files[i]);
					ByteArrayDataSource dataSource = new ByteArrayDataSource(fis, "text/data");
					mimeBodyPart.setDataHandler(new DataHandler(dataSource));
					mimeBodyPart.setFileName(file.getName());
				} catch (Exception e) {
				} finally {
					if(fis!=null) {
						fis.close();
					}
				}
			}
		}

		mimeMessage.setRecipients(Message.RecipientType.TO, receiveName);
		
		MimeBodyPart htmlPart = new MimeBodyPart();
		htmlPart.setContent(htmlContent, "text/html;charset=utf-8"); // html
		mimeMultipart.addBodyPart(htmlPart);
		return mimeMessage;
	}

//	public static void main(String[] args) throws Exception {
//		String host = "smtp.qq.com";
//		String username = "xxxxxxxx@qq.com";
//		String password = "xxxxxxxx";
//		File file = new File("E:\\test.xlsx");
//		FileInputStream fis = new FileInputStream(file);
//		byte[] bs = new byte[fis.available()];
//		fis.read(bs);
//		File []files = new File[] {file};
//		EmailUtil.sendMail(host, password, username, "xxxxxxxxx@qq.com", "2222哈哈哈aaaa", "bbbb哈哈哈cccc", files);
//		fis.close();
//	}
}

QQ邮箱设置

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

孔小胖子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值