java实现发送qq邮件
【java实现发送qq邮件】//测试邮件
public static void main(String[] args) throws Exception {
Properties props = config();
// 构建授权信息,用于进行SMTP进行身份验证
Authenticator authenticator = new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
// 用户名、密码
String userName = props.getProperty("mail.user");
String password = props.getProperty("mail.password");
return new PasswordAuthentication(userName, password);
}
};
// 使用环境属性和授权信息,创建邮件会话
Session mailSession = Session.getInstance(props, authenticator);
// 创建邮件消息
MimeMessage message = new MimeMessage(mailSession);
// 设置发件人
InternetAddress form = new InternetAddress(props.getProperty("mail.user"));
message.setFrom(form);
// 设置收件人的邮箱
List email = new ArrayList<>();
email.add("***@163.com");
email.add("***@qq.com");
InternetAddress[] to_email = new InternetAddress[email.size()];
for (int i = 0;
i < email.size();
i++) {
to_email[i] = new InternetAddress(String.valueOf(email.get(i)));
}
//InternetAddress to = new InternetAddress(email.size());
//message.setRecipient(Message.RecipientType.TO, to);
message.setRecipients(Message.RecipientType.TO,to_email);
// 设置邮件标题
message.setSubject("====test====");
//收件人
//List email = new ArrayList<>();
//email.add("**@163.com");
//message.setRecipients(Message.RecipientType.TO, String.valueOf(email));
// 设置邮件的内容体
//message.setContent("新闻联播", "text/html;
charset=UTF-8");
/*MimeBodyPart messageBodyPart =new MimeBodyPart();
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
File attachment;
attachment= new File("E:/video/青玉案-元夕.txt");
DataSource fileDataSource=new FileDataSource(attachment);
messageBodyPart.setDataHandler(new DataHandler(fileDataSource));
messageBodyPart.setFileName(attachment.getName());
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
*///文本
//String content = SendMailUtil.readHtmlContent("E:/video/青玉案-元夕.txt");
String content = SendMailUtil.readHtmlContent("E:/video/video02.mp4");
content = content.replaceAll("\\{\\{title\\}\\}","==============ceshi=================");
message.setContent(content.toString(),"text/html;
charset=UTF-8");
//message.setText("明月别枝惊鹊,清风半夜鸣蝉。稻花香里说丰年,听取蛙声一片。","text/html;
charset=UTF-8");
Transport.send(message);
}/**
* 自定义配置
*/
private static Properties config(){
Properties props = new Properties();
// 表示SMTP发送邮件,必须进行身份验证
props.put("mail.smtp.auth", "true");
//此处填写SMTP服务器
props.put("mail.smtp.host", "smtp.qq.com");
//端口号,QQ邮箱端口587
props.put("mail.smtp.port", "587");
// 此处填写,写信人的账号
props.put("mail.user", "***@qq.com");
// 此处填写16位STMP口令
props.put("mail.password", "*****");
return props;
}
推荐阅读
- JAVA(抽象类与接口的区别&重载与重写&内存泄漏)
- 关于QueryWrapper|关于QueryWrapper,实现MybatisPlus多表关联查询方式
- MybatisPlus使用queryWrapper如何实现复杂查询
- python学习之|python学习之 实现QQ自动发送消息
- 事件代理
- 孩子不是实现父母欲望的工具——林哈夫
- opencv|opencv C++模板匹配的简单实现
- Java|Java OpenCV图像处理之SIFT角点检测详解
- Node.js中readline模块实现终端输入
- java中如何实现重建二叉树