java 发送邮件怎么抄送_javaMail发送qq邮件(二):可发送抄送密送多人,支持附件...
关于javaMail发送邮件的详细教程可以看Java 发送邮件(菜鸟教程)本文的基础代码也来自这里。
关于实现的效果图如下
Snipaste_2020-03-25_18-05-06.png
Snipaste_2020-03-25_18-06-44.png
Snipaste_2020-03-25_18-09-25.png
Snipaste_2020-03-25_18-12-12.png
在网上看到有说发送QQ邮箱需要添加SSL加密的代码
// 关于QQ邮箱,还要设置SSL加密,加上以下代码即可
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
props.put("mail.smtp.ssl.enable", "true");
props.put("mail.smtp.ssl.socketFactory", sf);
但在测试的时候发现不用开启SSL加密也能发送成功。
如何添加多个收件人
InternetAddress[] addArr = null;
// 设置收件人
addArr = new InternetAddress[toList.size()];
for (int i = 0; i < toList.size(); i++) {
String mailtoString = (String) toList.get(i);
addArr[i] = new InternetAddress(mailtoString);
}
message.setRecipients(Message.RecipientType.TO, addArr); // 设置收件人地址
抄送人和密送人类似,更改Message.RecipientType.TO为Message.RecipientType.CC(抄送)和Message.RecipientType.BCC(密送即可)
如何发送附件
需要在创建MimeMessage对象后
// 创建默认的 MimeMessage 对象
MimeMessage message = new MimeMessage(session);
设置消息体的文件数据
代码如下
// 带有附件的邮件发送
MimeMultipart multi = new MimeMultipart();
BodyPart bodyPartText = new MimeBodyPart();
bodyPartText.setText(messageContent); // 设置邮件内容
multi.addBodyPart(bodyPartText);
String fileName = null;
for (int i = 0; i < fileList.size(); i++) {
fileName = (String) fileList.get(i);
FileDataSource fds = new FileDataSource(fileName);
BodyPart fileBodyPart = new MimeBodyPart();
fileBodyPart.setDataHandler(new DataHandler(fds)); // 设置附件文件数据
fileBodyPart.setFileName(fds.getName()); // 设置附件文件名称
multi.addBodyPart(fileBodyPart);
}
message.setContent(multi);
发送邮件的整体代码
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;
import java.util.Vector;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
/**
* 发送邮件服务
*
* @author lxh96
*
*/
public class SendEmailService {
private String mailServerPort = "25"; // 默认端口号
/**
* 发送邮件,验证用户权限
*
* @param smtpHost 邮箱服务器
* @param mailServerPort 邮箱服务器端口
* @param mailFrom 发送方邮箱地址
* @param toList 收件方邮箱集合
* @param ccList 抄送邮箱地址集合
* @param bbList 密送邮箱地址集合
* @param subJect 邮件主题
* @param messageStr 邮件内容
* @param fileList 附件路径集合
* @return
* @throws UnsupportedEncodingException
*/
public int authSend(String userName, String password, String smtpHost, String serverPort, String mailFrom,
Vector toList, Vector ccList, Vector bccList, String subJect, String messageContent, Vector fileList)
throws UnsupportedEncodingException {
// 获取系统属性
Properties properties = System.getProperties();
// 设置邮件服务器
properties.setProperty("mail.smtp.host", smtpHost);
// 设置端口
properties.setProperty("mail.smtp.port", mailServerPort);
// 设置邮箱是否需要验证
properties.setProperty("mail.smtp.auth", "true");
// 获取默认session对象
Session session = Session.getDefaultInstance(properties, new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, password); // qq邮箱用户名 密码(qq为授权码) --用于登录邮箱服务器的用户名密码
}
});
session.setDebug(false); // 是否开启debug模式,用于调试
try {
// 创建默认的 MimeMessage 对象
MimeMessage message = new MimeMessage(session);
// Set From: 头部头字段
message.setFrom(new InternetAddress(mailFrom, "nickName")); // 发件人名称可以不设置
InternetAddress[] addArr = null;
// 设置收件人
addArr = new InternetAddress[toList.size()];
for (int i = 0; i < toList.size(); i++) {
String mailtoString = (String) toList.get(i);
addArr[i] = new InternetAddress(mailtoString);
}
message.setRecipients(Message.RecipientType.TO, addArr); // 设置收件人地址
// 设置抄送人地址
if ((ccList != null) && (ccList.size() != 0)) {
addArr = new InternetAddress[ccList.size()];
for (int i = 0; i < ccList.size(); i++) {
String ccMailStr = (String) ccList.get(i);
addArr[i] = new InternetAddress(ccMailStr);
}
message.setRecipients(Message.RecipientType.CC, addArr); // 设置抄送人地址
}
// 设置密送人地址
if ((bccList != null) && (bccList.size() != 0)) {
addArr = new InternetAddress[bccList.size()];
for (int i = 0; i < bccList.size(); i++) {
String bccMailStr = (String) bccList.get(i);
addArr[i] = new InternetAddress(bccMailStr);
}
message.setRecipients(Message.RecipientType.BCC, addArr); // 设置密送人地址
}
// Set Subject: 头部头字段
message.setSubject(subJect);
if ((fileList == null) || (fileList.size() == 0)) { // 没有附件
// 设置消息体
message.setText(messageContent);
} else {
// 带有附件的邮件发送
MimeMultipart multi = new MimeMultipart();
BodyPart bodyPartText = new MimeBodyPart();
bodyPartText.setText(messageContent); // 设置邮件内容
multi.addBodyPart(bodyPartText);
String fileName = null;
for (int i = 0; i < fileList.size(); i++) {
fileName = (String) fileList.get(i);
FileDataSource fds = new FileDataSource(fileName);
BodyPart fileBodyPart = new MimeBodyPart();
fileBodyPart.setDataHandler(new DataHandler(fds)); // 设置附件文件数据
fileBodyPart.setFileName(fds.getName()); // 设置附件文件名称
multi.addBodyPart(fileBodyPart);
}
message.setContent(multi);
}
message.setSentDate(new Date()); // 邮件邮件发送时间
message.saveChanges();
long btime = System.currentTimeMillis();
// 发送消息
Transport.send(message);
long etime = System.currentTimeMillis();
String sendmessage = "发件人:"+mailFrom+"发送成功\n收件人:"+toList+"\n";
if (ccList != null && ccList.size() != 0) {
sendmessage += "抄送人:"+ccList+"\n";
}
if (bccList != null && bccList.size() != 0) {
sendmessage += "密送人:"+bccList+"\n";
}
sendmessage += "用时:"+(etime - btime)+"ms";
System.out.println("Sent message successfully....\n"+sendmessage);
} catch (MessagingException mex) {
mex.printStackTrace();
return 1;
}
return 0;
}
public void setMailServerPort(String mailServerPort) {
this.mailServerPort = mailServerPort;
}
}
测试类
import java.io.UnsupportedEncodingException;
import java.util.Vector;
import mail.service.SendEmailService;
/**
* 发送邮件测试
*
* @author lxh96
*
*/
public class SendEmail {
public static void main(String[] args) {
SendEmailService sendEmail = new SendEmailService();
String userName = "34**9@qq.com";
String password = "QQ邮箱授权码或者其他登陆邮箱密码";
String smtpHost = "smtp.qq.com";
String serverPort = "465";
String mailFrom = "34**9@qq.com";
Vector toList = new Vector() ;
toList.addElement("*****4@163.com");
toList.addElement("56***7@qq.com");
Vector ccList = new Vector();
ccList.addElement("56**7@qq.com");
Vector bccList = new Vector();
bccList.addElement("*****@163.com");
String subJect = "hello 测试";
String messageContent = "hello ,我是一个javamail的测试邮件";
Vector fileList = new Vector();
fileList.addElement("C:\\Users\\lxh96\\Pictures\\B01.jpg");
fileList.addElement("C:\\Users\\lxh96\\Pictures\\unnamed.jpg");
try {
sendEmail.authSend(userName, password, smtpHost, serverPort, mailFrom, toList, ccList, bccList, subJect,
messageContent, fileList);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("邮件发送失败");
}
}
}
能够实现简单邮件多人发送、多人抄送、密送和多附件的发送功能。可能需要根据不同的邮件发送服务器修改或添加相应的properties配置,但应该大同小异。
java 发送邮件怎么抄送_javaMail发送qq邮件(二):可发送抄送密送多人,支持附件...相关推荐
- python发送qq邮件(加密非加密)
python发送qq邮件,加密发送,非加密发送 设置qq邮箱 设置开启客户端功能 设置--->账户 开启POP3/SMTP服务 根据提示开启 授权码 开启成功 查看qq邮件服务器信息1 查看qq ...
- Java中利用qqemai发送QQ邮件
最近自己了解下Java中利用mai发送QQ邮件 1.QQ邮箱设置 1.1 进去QQ邮箱-->设置-->账号-->进行设置如下图 以下源码 package test; import j ...
- Java 通过SMTP实现发送QQ邮件
在eclipse中创建项目,并把javax.amil.jar和commons-email-1.5,jar复制到项目中 链接:https://pan.baidu.com/s/1sQjA1GEpKi6IJ ...
- Java发送QQ邮件问题Could not connect to SMTP host: smtp.qq.com, port: 465(内附完整代码)
邮件发送失败,本地测试可以,部署到服务器就不行. 之前就碰到这个问题,解决办法就是把port端口改成587,然后注释这一段 MailSSLSocketFactory sf = new MailSSLS ...
- Simple Java Mail的使用,发送qq邮件
Simple Java Mail的使用,发送qq邮件 第一步 开启SMTP服务 第二步 导入jar包 第三步 简单的使用 四 集中配置,批量发送 第一步 开启SMTP服务 打开qq邮箱,设置-账户 开 ...
- java怎么发qq邮件_java代码如何发送QQ邮件
近来想写一个qq之间互相发送邮件的工具.奈何一直报错服务错误: org.apache.commons.mail.EmailException: Sending the email to the fol ...
- java通过SMTP发送QQ邮件的完全步骤
java通过SMTP发送QQ邮件的完全步骤 首先,我们需要打开QQ邮箱的SMTP服务,因为QQ邮箱对于一般的用户都是默认关闭SMTP服务的. 打开QQ邮箱,点击设置 点击帐户 找到SMTP服务的选项, ...
- Java发送QQ邮件的3种形式(commons-email)
第一步:项目引入相应资源 commons-email-xx.jar,mail.jar,activation.jar maven项目pom引入 (注意对应配置的版本问题哦~) 可参考 --& ...
- 通过QQ邮箱的SMTP服务器发送QQ邮件至163邮箱提示“发送邮件失败”的解决方案(三种可能性,不妨一试)...
IDE:Visual Studio 2010[C#] + .NET 4.0框架 引用.NET 4.0框架的System.Net.Mail命名空间开发发送邮件的程序.发送方:QQ邮箱 接收方:163邮箱 ...
- java 发送qq邮件
发送qq邮件的简单demo 一: 在myEclipse中创建一个java项目 1:直接将该文件引入到src , 2:将activation.ja ...
最新文章
- Ubuntu16.04下搭建开发环境及编译tiny4412 Android系统【转】
- 基于双目事件相机的视觉里程计
- Xamarin.Android开发实践(十七)
- 关于Exception类
- ISE 14.7安装教程
- hive建立内部表映射hbase_快手 HBase 在千亿级用户特征数据分析中的应用与实践...
- golang 升级到新版本_Scikit-learn新版本发布,一行代码秒升级
- H3C批量收集服务器信息,H3C设备服务器采集参数认证过程(包含redfish和restfull协议)...
- python标注工具_Python labelImg 图像标注工具安装及使用教程windows版(亲测有效)
- linux程序改ip地址吗,如何在Linux中从C设置IP地址
- Pandorabox等类OpenWrt的路由器 实现后端设备接入IPV6(中继代理+NAT)
- 【静态化平台】☞( 一)网页静态化服务
- ubuntu 终端透明
- 正方形里面两个扇形相交部分_正方形里面为两个半圆相交,求相交部分阴影面积...
- 7的整除特征 三位一截_7的倍数特征
- g++ warn_unused_result
- 【一起入门NLP】中科院自然语言处理作业二:中英文语料训练CBOW模型获得词向量(pytorch实现)【代码+报告】
- 八皇后问题 回溯法
- 2021年中国鸡尾酒杯市场趋势报告、技术动态创新及2027年市场预测
- 天猫精灵智能设备对接(2)