网站首页 > java教程 正文
/**
* 发送邮件
* @param protocol:邮件传输协议,可空
* @param host:邮件服务器
* @param port:端口,可空
* @param auth:是否验证身份
* @param username:用户
* @param password:密码
* @param enableSsl:是否启用设置SSL加密
* @param fromEmailAddress:发件人
* @param toEmailAddress:收件人
* @param ccEmailAddress:抄送人,可空
* @param bccEmailAddress:密送人,可空
* @param subject:邮件主题
* @param content:邮件内容
* @param contentType:内容格式,可空,HTML格式:text/html
* @param containAttachment:是否包含附件
* @param attachmentPath:附件路径,可空
* @return
*/
public static Map<String, String> sendEmail(String protocol, String host,
String port, boolean auth, final String username,
final String password, boolean enableSsl, String fromEmailAddress,
String[] toEmailAddress, String[] ccEmailAddress,
String[] bccEmailAddress, String subject, String content,
String contentType, boolean containAttachment, String attachmentPath) {
Map<String, String> result = new HashMap<String, String>();
String returnCode = "1"; //返回结果,0异常,1成功
String returnMsg = ""; //返回消息
try {
// 创建属性对象
Properties properties = new Properties();
// 设置邮件传输采用的协议
if(null != protocol && !"".equals(protocol)){
properties.setProperty("mail.transport.protocol", protocol);
}
// 设置邮件服务器
properties.setProperty("mail.smtp.host", host);
// 设置邮件服务器端口
if(null != port && !"".equals(port)){
properties.setProperty("mail.smtp.port", port);
}
// 是否验证身份
properties.put("mail.smtp.auth", auth);
if (enableSsl) {
MailSSLSocketFactory msf = new MailSSLSocketFactory();
msf.setTrustAllHosts(true);
properties.put("mail.smtp.ssl.enable", "true");
properties.put("mail.smtp.ssl.socketFactory", msf);
}
// 获取默认session对象
Session session = null;
if(auth){ //需要验证身份
session = Session.getDefaultInstance(properties, new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
}else{
session = Session.getInstance(properties);
}
// 创建默认的 MimeMessage 对象
MimeMessage message = new MimeMessage(session);
// 设置发件人
message.setFrom(new InternetAddress(fromEmailAddress));
// 设置收件人
if(null != toEmailAddress && toEmailAddress.length > 0){
int len = toEmailAddress.length;
InternetAddress[] toEas = new InternetAddress[len];
for(int i = 0; i < len; i++){
toEas[i] = new InternetAddress(toEmailAddress[i]);
}
message.addRecipients(MimeMessage.RecipientType.TO, toEas);
}
// 设置抄送人
if(null != ccEmailAddress && ccEmailAddress.length > 0){
int len = ccEmailAddress.length;
InternetAddress[] ccEms = new InternetAddress[len];
for(int i = 0; i < len; i++){
ccEms[i] = new InternetAddress(ccEmailAddress[i]);
}
message.setRecipients(MimeMessage.RecipientType.CC, ccEms);
}
// 设置密送人
if(null != bccEmailAddress && bccEmailAddress.length > 0){
int len = bccEmailAddress.length;
InternetAddress[] bccEms = new InternetAddress[len];
for(int i = 0; i < len; i++){
bccEms[i] = new InternetAddress(bccEmailAddress[i]);
}
message.setRecipients(MimeMessage.RecipientType.BCC, bccEms);
}
// 设置邮件主题
message.setSubject(subject);
if(!containAttachment){ //不带附件
// 设置邮件内容
if(null != contentType && !"".equals(contentType)){
message.setContent(content, contentType);
}else{
message.setText(content);
}
}else{
// 创建消息部分
BodyPart messageBodyPart = new MimeBodyPart();
// 设置消息内容
if(null != contentType && !"".equals(contentType)){
messageBodyPart.setContent(content, contentType);
}else{
messageBodyPart.setText(content);
}
// 创建多重消息
Multipart multipart = new MimeMultipart();
// 设置文本消息部分
multipart.addBodyPart(messageBodyPart);
// 附件部分
messageBodyPart = new MimeBodyPart();
File file = new File(attachmentPath);
if(!file.exists()){
throw new Exception("附件不存在!");
}
DataSource source = new FileDataSource(file);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(file.getName());
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
}
// 设置发送时间
message.setSentDate(new Date());
// 发送消息
Transport.send(message);
} catch (Exception e) {
logger.error("邮件发送异常:"+ e);
returnCode = "0";
returnMsg = e.getMessage();
}
result.put("returnCode", returnCode);
result.put("returnMsg", returnMsg);
return result;
}
猜你喜欢
- 2024-09-14 SpringBoot发送简单文本邮件(springboot 发送邮件)
- 2024-09-14 Github 大牛封装 Python 代码,实现自动发送邮件只需三行代码
- 2024-09-14 Java定时发送Email(带Excel附件)(java怎么实现定时任务)
- 2024-09-14 Spring Boot 邮件发送的 5 种姿势
- 2024-09-14 SpringBoot优雅地发送邮件(springboot发送邮件的步骤)
- 2024-09-14 Spring boot 发送邮件及原理(springboot发送outlook邮件)
- 2024-09-14 10分钟搞定Java实现邮箱验证,坦率讲,别被他人割韭菜
- 2024-09-14 25、springboot发送邮件(springboot email)
- 2024-09-14 SpringBoot邮件发送示例(springboot 邮件)
- 2024-09-14 使用spring boot 发送邮件(springboot发送outlook邮件)
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- java反编译工具 (77)
- java反射 (57)
- java接口 (61)
- java随机数 (63)
- java7下载 (59)
- java数据结构 (61)
- java 三目运算符 (65)
- java对象转map (63)
- Java继承 (69)
- java字符串替换 (60)
- 快速排序java (59)
- java并发编程 (58)
- java api文档 (60)
- centos安装java (57)
- java调用webservice接口 (61)
- java深拷贝 (61)
- 工厂模式java (59)
- java代理模式 (59)
- java.lang (57)
- java连接mysql数据库 (67)
- java重载 (68)
- java 循环语句 (66)
- java反序列化 (58)
- java时间函数 (60)
- java是值传递还是引用传递 (62)
本文暂时没有评论,来添加一个吧(●'◡'●)