javaX邮件发送

/**
*

* @param mailServerHost 邮件服务器
* @param mailServerPort 端口
* @param validate 是否需要身份验证
* @param userName 用户
* @param password 密码
* @param fromAddress 发送地址
* @param getToAddress 收件地址
* @param getSubject //邮件主题
* @param getContent //邮件内容
* @param filePath //邮件附件路径
* @param fileName //附件名
* @return
*/
def sendMail(mailServerHost ,mailServerPort,validate
,userName,password,fromAddress,getToAddress,getSubject,getContent,filePath,fileName){

/*// 发送邮件的服务器的IP和端口
String mailServerHost = grailsApplication.config.getProperty('smtphost')
String mailServerPort = grailsApplication.config.getProperty('smtpport')
// 邮件发送者的地址
String fromAddress = grailsApplication.config.getProperty('smtpfrom')
// 登陆邮件发送服务器的用户名和密码
String userName = grailsApplication.config.getProperty('smtpuser')
String password = grailsApplication.config.getProperty('smtppswd')
// 是否需要身份验证
boolean validate = grailsApplication.config.getProperty('smtpvalidate')*/

Properties p = new Properties();
p.put("mail.smtp.host", mailServerHost);
p.put("mail.smtp.port", mailServerPort);
p.put("mail.smtp.auth", validate ? "true" : "false");
p.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");


// 判断是否需要身份认证
MyAuthenticator authenticator = null;
//如果需要身份认证,则创建一个密码验证器
if ( validate ) {
authenticator = new MyAuthenticator( userName, password );
}
// 根据邮件会话属性和密码验证器构造一个发送邮件的session
Session sendMailSession = Session.getDefaultInstance(p,authenticator);
try {
// 根据session创建一个邮件消息
Message mailMessage = new MimeMessage(sendMailSession);
// 创建邮件发送者地址
Address from = new InternetAddress(fromAddress);
// 设置邮件消息的发送者
mailMessage.setFrom(from);

// 创建邮件的接收者地址,并设置到邮件消息中
InternetAddress to = new InternetAddress(getToAddress);
mailMessage.setRecipient(Message.RecipientType.TO, to);
// 设置邮件消息的主题
mailMessage.setSubject(getSubject);
// 设置邮件消息发送的时间
mailMessage.setSentDate(new Date());
// MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象
Multipart mainPart = new MimeMultipart();
// 创建一个包含HTML内容的MimeBodyPart
BodyPart html = new MimeBodyPart();
// 设置HTML内容 建立第一部分: 文本正文
html.setContent(getContent, "text/html; charset=utf-8");
mainPart.addBodyPart(html);
// 将MiniMultipart对象设置为邮件内容 建立第二部分:附件
mailMessage.setContent(mainPart);

File tempFile = new File(filePath);
if (tempFile.exists()){
// 建立第二部分:附件
html = new MimeBodyPart();
// 获得附件
DataSource source = new FileDataSource(tempFile);
//设置附件的数据处理器
html.setDataHandler(new DataHandler(source));
// 设置附件文件名
html.setFileName(MimeUtility.encodeText(fileName));
// 加入第二部分
mainPart.addBodyPart(html);
}
/*if(mailInfo.getAttachFileNames() !=null && mailInfo.getAttachFileNames().length>0){
for(int i=0;i<mailInfo.getAttachFileNames().length;i++){
if (!mailInfo.getAttachFileNames()[i].equals("")) {
File tempFile =new File(mailInfo.getAttachFileNames()[i]);
if (tempFile.exists()){
// 建立第二部分:附件
html = new MimeBodyPart();
// 获得附件
DataSource source = new FileDataSource(mailInfo.getAttachFileNames()[i]);
//设置附件的数据处理器
html.setDataHandler(new DataHandler(source));
// 设置附件文件名
String fileNm="";
try {
fileNm=MimeUtility.encodeText(tempFile.getName());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
html.setFileName(fileNm);
// 加入第二部分
mainPart.addBodyPart(html);
}
}
}
}*/

// 发送邮件

Transport.send(mailMessage);
return true;
} catch (MessagingException ex) {
log.error("邮件发送失败", ex)
}
return false;

 

如有雷同、请见谅

posted @ 2018-10-09 11:38  易行舟  阅读(281)  评论(0编辑  收藏  举报