javamail邮件实现

QQ给其他邮箱发邮件;

既可以发送不带附件的,也可以发送带附件的,也可以同时给多个人发送

import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
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 com.sun.mail.util.MailSSLSocketFactory;


/**
 * TODO 
 * 
 * @author ZhangPeng
 * @date 2021年5月19日 下午2:29:47
 */
public class TestMail {
	
	private static final String ACCEPT_CODE = "cummlmoizgiicfef";

	public static void main(String[] args) throws Exception {
		
		Properties prop = new Properties();
		prop.setProperty("mail.host", "smtp.qq.com");//设置QQ邮件服务器
		prop.setProperty("mail.transport.protocol", "smtp");//邮件发送协议
		prop.setProperty("mail.smtp.auth", "true");//需要验证用户名和密码
		
		//关于QQ邮箱,还要设置SSL加密,加上以下代码即可
	
		MailSSLSocketFactory sf = new MailSSLSocketFactory();
		sf.setTrustAllHosts(true);
		prop.put("mail.smtp.ssl.enable", "true");
		prop.put("mail.smtp.ssl.socketFactory", sf);
		
		//QQ邮箱才有,其他邮箱不用
		Session session = Session.getDefaultInstance(prop, new Authenticator() {
			@Override
			protected PasswordAuthentication getPasswordAuthentication() {
				// TODO 发件人邮件用户名、授权码,授权码需要到QQ邮箱设置账户里生成
				return new PasswordAuthentication("1648873838@qq.com", ACCEPT_CODE);
			}
		});
		
		session.setDebug(true);//不想要发送日志,可以注释掉
		Transport ts = session.getTransport();
		ts.connect("smtp.qq.com", "1648873838@qq.com", ACCEPT_CODE);
		
		MimeMessage message = new MimeMessage(session);
		message.setFrom(new InternetAddress("1648873838@qq.com"));//发件人
		//收件人,现在是给自己发
//		message.setRecipient(Message.RecipientType.TO, new InternetAddress("1648873838@qq.com"));
		message.setRecipient(Message.RecipientType.TO, new InternetAddress("wa_wj999@163.com"));
		//也可以设置多个收件人
//		message.setRecipients(Message.RecipientType.TO, new InternetAddress[]{new InternetAddress("1648873838@qq.com"),new InternetAddress("24736743@qq.com")});
		message.setSubject("欢迎来到德莱联盟");
//		message.setContent("<h1 style='color:blue;'>hello,你好啊……</h1>","text/html;charset=utf-8");
		
		//=========================
		MimeBodyPart image = new MimeBodyPart();
		DataHandler handler = new DataHandler(new FileDataSource("src/1.jpg"));
		image.setDataHandler(handler);
		image.setContentID("1.jpg");
		
		MimeBodyPart text = new MimeBodyPart();
		text.setContent("这是一封邮件正文带图片<img src='cid:1.jpg'/>的邮件","text/html;charset=utf-8");
		
		MimeBodyPart fujian1 = new MimeBodyPart();
		DataHandler handler1 = new DataHandler(new FileDataSource("src/baoming.txt"));
		fujian1.setDataHandler(handler1);
		fujian1.setFileName("baoming.txt");
		
		MimeBodyPart fujian2 = new MimeBodyPart();
		DataHandler handler2 = new DataHandler(new FileDataSource("src/empty.txt"));
		fujian2.setDataHandler(handler2);
		fujian2.setFileName("empty.txt");
		
		MimeMultipart mm = new MimeMultipart();
		mm.addBodyPart(text);
		mm.addBodyPart(image);
		mm.setSubType("related");
		
		MimeBodyPart contentText = new MimeBodyPart();
		contentText.setContent(mm);
		
		MimeMultipart allFile = new MimeMultipart();
		allFile.addBodyPart(fujian1);
		allFile.addBodyPart(fujian2);
		allFile.addBodyPart(contentText);
		allFile.setSubType("mixed");
		
		message.setContent(allFile);
		message.saveChanges();
		
		//=========================
		
		Transport.send(message, message.getAllRecipients());
		ts.close();
	}
}

项目结构:
image

另外可以参考:
https://www.cnblogs.com/ysocean/p/7666061.html

原文地址:https://www.cnblogs.com/kaka-qiqi/p/14785606.html