admin管理员组文章数量:1559386
引入相关依赖:
javax.mail
1.4.7
commons-io
commons-io
2.4
如果使用spring的发邮件,还需要添加依赖:
org.springframework
spring-context-support
${spring.version}
登录腾讯企业邮箱:
生成一个授权密码,后面会用到.
下面SSL等 腾讯QQ和企业邮箱默认是开启的
POP3/SMTP协议
接收邮件服务器:pop.exmail.qq ,使用SSL,端口号995
发送邮件服务器:smtp.exmail.qq ,使用SSL,端口号465
海外用户可使用以下服务器
接收邮件服务器:hwpop.exmail.qq ,使用SSL,端口号995
发送邮件服务器:hwsmtp.exmail.qq ,使用SSL,端口号465
IMAP协议
接收邮件服务器:imap.exmail.qq ,使用SSL,端口号993
发送邮件服务器:smtp.exmail.qq ,使用SSL,端口号465
海外用户可使用以下服务器
接收邮件服务器:hwimap.exmail.qq ,使用SSL,端口号993
发送邮件服务器:hwsmtp.exmail.qq ,使用SSL,端口号465
QQ邮箱
smtp.qq
587
主要的一个方法
/**
* 发送邮件
*
* @param content 内容
*/
public static void sendMail(String host, String port, String protocol,String auth,String sendMailAccount,
String sendMailPassword, String receiveMailAccount, String subject, String content) {
Properties prop = new Properties();
//协议
prop.setProperty("mail.transport.protocol", "smtp");
//服务器
prop.setProperty("mail.smtp.host", "smtp.exmail.qq");
//端口
prop.setProperty("mail.smtp.port","465");
//使用smtp身份验证
prop.setProperty("mail.smtp.auth","true");
//企业邮箱必须要SSL
MailSSLSocketFactory sf = null;
try {
sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
} catch (GeneralSecurityException e1) {
e1.printStackTrace();
}
prop.put("mail.smtp.ssl.enable", "true");
prop.put("mail.smtp.ssl.socketFactory", sf);
//获取Session对象
Session s = Session.getDefaultInstance(prop, new Authenticator() {
//此访求返回用户和密码的对象
@Override
protected PasswordAuthentication getPasswordAuthentication() {
PasswordAuthentication pa = new PasswordAuthentication("kf@zz1x", "生成的客户端专用密码");
return pa;
}
});
//设置session的调试模式,发布时取消
s.setDebug(true);
MimeMessage mimeMessage = new MimeMessage(s);
try {
mimeMessage.setFrom(new InternetAddress(kf@zz1x));
mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress("接受邮件的邮箱"));
//设置主题
mimeMessage.setSubject("subject");//邮件标题
mimeMessage.setSentDate("new Date()");
//设置内容
mimeMessage.setText("content");//邮件内容
mimeMessage.saveChanges();
//发送
Transport.send(mimeMessage);
} catch (MessagingException e) {
e.printStackTrace();
}
}
这里是一个方法写成的公共的。host、port都是调用方传过来的
最后测试的效果:
如果要群发的话,只需要更改
//单个人
// mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(receiveMailAccount));
变成多人
mimeMessage.addRecipients(Message.RecipientType.TO,addressMails(receiveMailAccount));
public static InternetAddress[] addressMails(String receiveMails) {
//多个接收账号
// String str = "xxx@xxx,xxx@xxx";
InternetAddress[] address = null;
try {
List list = new ArrayList();//不能使用string类型的类型,这样只能发送一个收件人
String[] median = receiveMails.split(",");//对输入的多个邮件进行逗号分割
for (int i = 0; i < median.length; i++) {
list.add(new InternetAddress(median[i]));
}
address = (InternetAddress[]) list.toArray(new InternetAddress[list.size()]);
} catch (AddressException e) {
e.printStackTrace();
}
return address;
}
参数receiveMails为String字符串以逗号隔开,比如:1195564387@qq,17258232@qq
版权声明:本文标题:java发送QQ群邮件,Java使用腾讯企业邮箱 、javamail 、 SSL 发送邮件群发 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/xitong/1727421237a1113606.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论