admin管理员组文章数量:1559742
邮箱实体类
public class EmailMailInfo {
//邮箱服务器 如smtp.163
private String host ;
//用户邮箱 如**@163
private String formName ;
//用户授权码 不是用户名密码 可以自行查看相关邮件服务器怎么查看
private String formPassword ;
//消息回复邮箱
private String replayAddress ;
//发送地址
private String toAddress ;
//发送主题
private String subject ;
//发送内容
private String content ;
//端口号
private String port;
//多人邮件
private InternetAddress[] emailMany;
public String getPort() {
return port;
}
public void setPort(String port) {
this.port = port;
}
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public String getFormName() {
return formName;
}
public void setFormName(String formName) {
this.formName = formName;
}
public String getFormPassword() {
return formPassword;
}
public void setFormPassword(String formPassword) {
this.formPassword = formPassword;
}
public String getReplayAddress() {
return replayAddress;
}
public void setReplayAddress(String replayAddress) {
this.replayAddress = replayAddress;
}
public String getToAddress() {
return toAddress;
}
public void setToAddress(String toAddress) {
this.toAddress = toAddress;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public InternetAddress[] getEmailMany() {
return emailMany;
}
public void setEmailMany(InternetAddress[] emailMany) {
this.emailMany = emailMany;
}
}
具体业务实现
/**
* 多个账号邮箱的公共方法
* @param emailList
* @return
*/
private static InternetAddress[] addressMails(List emailList) throws AddressException {
List list = new ArrayList();//不能使用string类型的类型,这样只能发送一个收件人
//多个接收账号
for (int i = 0; i < emailList.size(); i++) {
list.add(new InternetAddress(emailList.get(i).toString()));
}
return (InternetAddress[]) list.toArray(new InternetAddress[list.size()]);
}
/**
* 发送多人邮件
* @throws Exception
*/
private void SendMoreEmail() throws Exception {
List<String> noticeList = new ArrayList<>();
noticeList.add("163@163");
noticeList.add("164@163");
EmailMailInfo info = new EmailMailInfo();
info.setSubject("主题");
info.setContent("这里是邮件内容啊啊啊啊啊啊啊啊啊啊啊啊啊");
info.setEmailMany(addressMails(noticeList));//多人邮件
EmailSendUtil.sendHtmlMail(info);
}
/**
* 单人邮箱
*/
public static void main(String[] args) {
String email = "1578855@qq";
String title = "用户";
String content = "您好:我是邮箱";
EmailMailInfo info = new EmailMailInfo();
info.setToAddress(email);
info.setSubject(title);
info.setContent(content);
try {
EmailSendUtil.sendHtmlMail(info);
} catch (Exception e) {
System.out.print("'" + title + "'的邮件发送失败!");
e.printStackTrace();
}
}
发送邮件工具类
import java.security.GeneralSecurityException;
import java.util.Date;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
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;
import com.sun.mail.util.MailSSLSocketFactory;
public class EmailSendUtil {
private final static String host = "smtp.exmail.qq"; //腾讯企业邮的服务器
private final static String formName = "邮箱";//发送人邮箱
private final static String password = "密码"; //授权码
private final static String replayAddress = "邮箱"; //发送人邮箱
private final static String port = "465"; //端口号
//private final static String port = "587"; //你的邮箱
public static void sendHtmlMail(EmailMailInfo info)throws Exception{
info.setHost(host);
info.setFormName(formName);
info.setFormPassword(password); //网易邮箱的授权码~不一定是密码
info.setReplayAddress(replayAddress);
info.setPort(port);
Message message = getMessage(info);
// MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象
Multipart mainPart = new MimeMultipart();
// 创建一个包含HTML内容的MimeBodyPart
BodyPart html = new MimeBodyPart();
// 设置HTML内容
html.setContent(info.getContent(), "text/html; charset=utf-8");
mainPart.addBodyPart(html);
// 将MiniMultipart对象设置为邮件内容
message.setContent(mainPart);
Transport.send(message);
}
public static void sendTextMail(EmailMailInfo info) throws Exception {
info.setHost(host);
info.setFormName(formName);
info.setFormPassword(password); //网易邮箱的授权码~不一定是密码
info.setReplayAddress(replayAddress);
Message message = getMessage(info);
//消息发送的内容
message.setText(info.getContent());
Transport.send(message);
}
private static Message getMessage(EmailMailInfo info) throws Exception{
final Properties p = System.getProperties() ;
p.setProperty("mail.smtp.host", info.getHost());
p.setProperty("mail.smtp.auth", "true");
p.setProperty("mail.smtp.user", info.getFormName());
p.setProperty("mail.smtp.pass", info.getFormPassword());
p.setProperty("mail.smtp.port", info.getPort());//设置端口
p.setProperty("mail.transport.protocol", "smtps");//
//开启安全协议
MailSSLSocketFactory sf = null;
try {
sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
p.put("mail.smtp.ssl.enable", "true");
p.put("mail.smtp.ssl.socketFactory", sf);
} catch (GeneralSecurityException e1) {
e1.printStackTrace();
}
// 根据邮件会话属性和密码验证器构造一个发送邮件的session
Session session = Session.getDefaultInstance(p, new Authenticator(){
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(p.getProperty("mail.smtp.user"),p.getProperty("mail.smtp.pass"));
}
});
session.setDebug(false);
Message message = new MimeMessage(session);
//消息发送的主题
message.setSubject(info.getSubject());
//接受消息的人
//message.setReplyTo(InternetAddress.parse(info.getReplayAddress()));
//消息的发送者
message.setFrom(new InternetAddress(p.getProperty("mail.smtp.user"),"系统"));
// 创建邮件的接收者地址,并设置到邮件消息中
//单人接收邮件
//message.setRecipient(Message.RecipientType.TO,new InternetAddress(info.getToAddress()));
//多人接收邮件
message.setRecipients(Message.RecipientType.TO, info.getEmailMany());
// 消息发送的时间
message.setSentDate(new Date());
return message ;
}
}
版权声明:本文标题:Java 腾讯邮箱发送邮件工具类(单人+多人) 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/dongtai/1727423713a1113855.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论