龙空技术网

java邮件发送

清风微拂尘 179

前言:

而今咱们对“java如何实现发送邮件”都比较讲究,姐妹们都需要剖析一些“java如何实现发送邮件”的相关知识。那么小编在网摘上网罗了一些有关“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;

}

标签: #java如何实现发送邮件