下文需要打开自己邮箱授权码,很简单,网易邮箱 详见https://jingyan.baidu.com/article/8ebacdf065a1f149f65cd5b5.html
腾讯邮箱详见 https://jingyan.baidu.com/article/90895e0f2af42664ec6b0b14.html
package com.didispace; import javax.mail.Message; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import java.util.Properties; public class SendMail { public static void main(String[] args) throws Exception { Properties prop = new Properties(); prop.setProperty("mail.transport.protocol", "smtp"); //协议 prop.setProperty("mail.smtp.host", "smtp.163.com"); //主机名 prop.setProperty("mail.smtp.auth", "true"); //是否开启权限控制 prop.setProperty("mail.debug", "true"); //返回发送的cmd源码 Session session = Session.getInstance(prop); Message msg = new MimeMessage(session); msg.setFrom(new InternetAddress("17607183394@163.com")); //自己的email.即发件箱 InternetAddress[] adds = new InternetAddress[2]; //群发邮件 adds[0] = new InternetAddress("963066@qq.com"); //添加需要发送的地址 adds[1] = new InternetAddress("761568@qq.com"); msg.setRecipients(Message.RecipientType.TO, adds); //msg.setRecipient(Message.RecipientType.TO, "963066@qq.com"); // 要发送的email,单独发送 msg.setSubject("公司公开信2");//邮件标题 msg.setText("请你于4:30到办公室开会");//邮件正文 //不被当作垃圾邮件的关键代码--Begin ,如果不加这些代码,发送的邮件会自动进入对方的垃圾邮件列表 msg.addHeader("X-Priority", "3"); msg.addHeader("X-MSMail-Priority", "Normal"); msg.addHeader("X-Mailer", "Microsoft Outlook Express 6.00.2900.2869"); //本文以outlook名义发送邮件,不会被当作垃圾邮件 msg.addHeader("X-MimeOLE", "Produced By Microsoft MimeOLE V6.00.2900.2869"); msg.addHeader("ReturnReceipt", "1"); //不被当作垃圾邮件的关键代码--end Transport trans = session.getTransport(); trans.connect("17603394@163.com", "zsq123456"); // 邮件的账号密码,密码为授权码,不是登录密码 trans.sendMessage(msg, msg.getAllRecipients()); } }