前提条件:
需要对邮箱做出设置,开启 :
POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV等服务
一方面是接收这些代理服务器发送的邮件
另一方面就是让邮箱接受这些邮件协议的代理
在登录的时候,就是用户名和授权码,进行登录
获取授权码的过程:直接百度搜索就有结果,这里不再赘述
demo
这里需要引入javax.mail jar包
package test;
///**
// *
// * @author jingxl0327
// * @Description 邮件发送测试
// */
public class sendMailTest {
public static void main(String[] args) throws Exception {
// 配置信息
Properties pro = new Properties();
pro.put("mail.smtp.host", "smtp.163.com");
pro.put("mail.smtp.auth", "true");
// SSL加密
MailSSLSocketFactory sf = null;
sf = new MailSSLSocketFactory();
// 设置信任所有的主机
sf.setTrustAllHosts(true);
pro.put("mail.smtp.ssl.enable", "true");
pro.put("mail.smtp.ssl.socketFactory", sf);
// 根据邮件的会话属性构造一个发送邮件的Session,这里需要注意的是用户名那里不能加后缀,否则便不是用户名了
//还需要注意的是,这里的密码不是正常使用邮箱的登陆密码,而是客户端生成的另一个专门的授权码
MailAuthenticator authenticator = new MailAuthenticator("jingxl0327",
"jing0327");
Session session = Session.getInstance(pro, authenticator);
// 根据Session 构建邮件信息
Message message = new MimeMessage(session);
// 创建邮件发送者地址
Address from = new InternetAddress("jingxl0327@163.com");
// 设置邮件消息的发送者
message.setFrom(from);
// 验证收件人邮箱地址
List<String> toAddressList = new ArrayList<>();
toAddressList.add("1157496573@qq.com");
StringBuffer buffer = new StringBuffer();
if (!toAddressList.isEmpty()) {
String regEx = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
Pattern p = Pattern.compile(regEx);
for (int i = 0; i < toAddressList.size(); i++) {
Matcher match = p.matcher(toAddressList.get(i));
if (match.matches()) {
buffer.append(toAddressList.get(i));
if (i < toAddressList.size() - 1) {
buffer.append(",");
}
}
}
}
String toAddress = buffer.toString();
if (!toAddress.isEmpty()) {
// 创建邮件的接收者地址
Address[] to = InternetAddress.parse(toAddress);
// 设置邮件接收人地址
message.setRecipients(Message.RecipientType.TO, to);
// 邮件主题
message.setSubject("这个是主题");
// 邮件容器
MimeMultipart mimeMultiPart = new MimeMultipart();
// 设置HTML
BodyPart bodyPart = new MimeBodyPart();
// 邮件内容
String htmlText = "这个是内容";
bodyPart.setContent(htmlText, "text/html;charset=utf-8");
mimeMultiPart.addBodyPart(bodyPart);
// 添加附件
List<String> fileAddressList = new ArrayList<String>();
fileAddressList
.add("C:\\Users\\tuzongxun123\\Desktop\\新建 Microsoft Office Word 文档.docx");
if (fileAddressList != null) {
BodyPart attchPart = null;
for (int i = 0; i < fileAddressList.size(); i++) {
if (!fileAddressList.get(i).isEmpty()) {
attchPart = new MimeBodyPart();
// 附件数据源
DataSource source = new FileDataSource(
fileAddressList.get(i));
// 将附件数据源添加到邮件体
attchPart.setDataHandler(new DataHandler(source));
// 设置附件名称为原文件名
attchPart.setFileName(MimeUtility.encodeText(source
.getName()));
mimeMultiPart.addBodyPart(attchPart);
}
}
}
message.setContent(mimeMultiPart);
message.setSentDate(new Date());
// 保存邮件
message.saveChanges();
// 发送邮件
Transport.send(message);
}
}
}
class MailAuthenticator extends Authenticator {
/**
* 用户名
*/
private String username;
/**
* 密码
*/
private String password;
/**
* 创建一个新的实例 MailAuthenticator.
*
* @param username
* @param password
*/
public MailAuthenticator(String username, String password) {
this.username = username;
this.password = password;
}
public String getPassword() {
return password;
}
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
public String getUsername() {
return username;
}
public void setPassword(String password) {
this.password = password;
}
public void setUsername(String username) {
this.username = username;
}
}