900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > java发送QQ群邮件 Java使用腾讯企业邮箱 javamail SSL 发送邮件/群发

java发送QQ群邮件 Java使用腾讯企业邮箱 javamail SSL 发送邮件/群发

时间:2024-06-13 17:11:40

相关推荐

java发送QQ群邮件 Java使用腾讯企业邮箱  javamail   SSL 发送邮件/群发

引入相关依赖:

javax.mail

mail

1.4.7

commons-io

commons-io

2.4

如果使用spring的发邮件,还需要添加依赖:

org.springframework

spring-context-support

${spring.version}

登录腾讯企业邮箱:

生成一个授权密码,后面会用到.

下面SSL等 腾讯QQ和企业邮箱默认是开启的

POP3/SMTP协议

接收邮件服务器:pop. ,使用SSL,端口号995

发送邮件服务器:smtp. ,使用SSL,端口号465

海外用户可使用以下服务器

接收邮件服务器:hwpop. ,使用SSL,端口号995

发送邮件服务器:hwsmtp. ,使用SSL,端口号465

IMAP协议

接收邮件服务器:imap. ,使用SSL,端口号993

发送邮件服务器:smtp. ,使用SSL,端口号465

海外用户可使用以下服务器

接收邮件服务器:hwimap. ,使用SSL,端口号993

发送邮件服务器:hwsmtp. ,使用SSL,端口号465

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.");

//端口

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@", "生成的客户端专用密码");

return pa;

}

});

//设置session的调试模式,发布时取消

s.setDebug(true);

MimeMessage mimeMessage = new MimeMessage(s);

try {

mimeMessage.setFrom(new InternetAddress(kf@));

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@";

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@,17258232@

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。