注意以下问题:发送者使用邮件发送功能必须在邮箱中开启smtp功能
//代码演示
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.util.Date;
import java.util.Properties;
/**
* @author: ltj
* date: 2019-03-27-13:33
**/
public class testMail{
public static void main(String[] args){
/**
* 分别对应响应的参数
* 本处设置例子为新浪的smtp服务器的地址
* 发送邮箱的帐号=发件人的邮箱地址
* 发送邮箱的密码
*/
String host = "smtp.sina.cn";
String smtpUserid="13692@sina.cn";
String smtpPwd="1234";
Session session;
//链接smtp服务器
// 获取系统环境
Properties props = new Properties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth", "true");
// 进行邮件服务用户认证
Authenticator auth = new Email_Autherticator(smtpUserid,smtpPwd);
// 设置session,和邮件服务器进行通讯
session = Session.getInstance(props, auth);
//获取发送者信息
String mailTitle="邮件标题";
String mailBody="邮件内容";
mailBody=mailBody.replace("\r\n","<br>");
//获得发送者的邮件地址
String sendTo="94937@qq.com";
//获得抄送者的邮件地址
String copyTo="94937@qq.com";
try {
Boolean sign = sendMail(sendTo,copyTo,mailTitle,mailBody,session);
if(sign){
System.out.print("发送成功!");
}
}catch (Exception e){
e.printStackTrace();
}
//发送邮件
}
/**
* 发送邮件
* @param mail_to 主送多个用户用逗号分隔
* @param copy_to 抄送多个用户用逗号分隔
* @param mail_subject 邮件标题
* @param mail_body 邮件内容
* @param session
* @throws SendFailedException
*/
public static boolean sendMail(String mail_to,String copy_to,String mail_subject,String mail_body,Session session) throws SendFailedException {
try {
MimeMessage message = new MimeMessage(session);
/**
* 发件人的邮箱地址
* 相当于称呼,通常显示在你的发件人栏的发件人邮箱地址前
*/
String from = "13692@sina.cn";
String personalName = "发件人称呼";
//设置发送邮件的内容
Multipart multipart = new MimeMultipart();
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(mail_body, "text/html; charset=utf-8");
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
//保存信息
message.saveChanges();
// 设置邮件主题
message.setSubject(mail_subject);
// 设置邮件发送时期
message.setSentDate(new Date());
//发邮件的出发地(from:发件人的信箱,personalName:发送标题)
Address address = new InternetAddress(from, personalName);
// 设置邮件发送者的地址
message.setFrom(address);
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(mail_to));
// message.setRecipients(Message.RecipientType.CC, InternetAddress.parse(copy_to));
Transport.send(message);
return true;
} catch (Exception e) {
System.out.println("SMTP邮件发送失败!");
e.printStackTrace();
return false;
}
}
}
开启验证邮件服务用户验证
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
public class Email_Autherticator extends Authenticator {
String username = "";
String password = "";
public Email_Autherticator() {
super();
}
public Email_Autherticator(String user, String pwd) {
super();
username = user;
password = pwd;
}
//Authenticator是抽象类,必须实现它的getPasswordAuthentication
//方法返回一个PasswordAuthentication对象
@Override
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
}
内文件中使用到的jar包下载
链接:https://pan.baidu.com/s/1i5rMwPOjlkkUJz3VBR9w3Q 提取码:qgx6