-
需要引用的jar包;
<dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4.7</version> </dependency>
-
工具类;
`package cn.qin.xxltest.xxltask.service;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
public class Email {
private static final String MAIL_USER = "******@qq.com"; //邮件服务器登录用户名
private static final String MAIL_PASSWORD = "bybtfpssyfvahrbeid"; //邮件服务器登录密码
private static final String MAIL_FROM_SMTP = "939634038@qq.com"; //发送邮件地址
public void sendmail(String[] mailArray,String subject,String content){
Properties props = new Properties();
//设置服务器验证
props.setProperty("mail.smtp.auth", "true");
//设置传输协议
props.setProperty("mail.transport.protocol", "smtp");
//选择服务类型
props.setProperty("mail.host", "smtp.qq.com");
//通过认证创建一个session实例
Session session = Session.getInstance(props,
new Authenticator()
{
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(MAIL_USER,MAIL_PASSWORD);
}
}
);
//显示邮件发送过程中的交互信息
session.setDebug(true);
Message msg = new MimeMessage(session);
Transport transport=null;
try {
//邮件发送人
msg.setFrom(new InternetAddress(MAIL_FROM_SMTP));
//邮件主题
msg.setSubject(subject);
//邮件内容
msg.setText(content);
int len=mailArray.length;
InternetAddress address[]=new InternetAddress[len];
for (int i = 0; i < mailArray.length; i++) {
address[i]=new InternetAddress(mailArray[i]);
}
//邮件接收方
msg.addRecipients(Message.RecipientType.TO, address);
transport.send(msg);
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if(transport!=null){
transport.close();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args) throws Exception{
Email demo2=new Email();
String[] mailArray ={"******@qq.com","******@eautosh.com"};
String subject="冬天来啦!";
String content="Hello world啊!";
demo2.sendmail(mailArray,subject,content);
}
}`
3.qq邮件服务器登录密码获取方式;
第一步:登录邮箱,点击设置;
第二步:选择账户;
第三步:开启POP3/SMTP服务,获取密码