/**
*

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

如有雷同、请见谅

javaX邮件发送的更多相关文章

  1. java使用javax.mail进行免费的邮件发送

    1. 建议发送方使用阿里云邮箱https://mail.aliyun.com/,阿里云默认是开启个人邮箱pop3.smtp协议的,所以无需在阿里云邮箱里设置,pop3.smtp的密码默认邮箱登录密码, ...

  2. SSH项目里面 忘记密码的邮件发送功能

    package com.xxx.util; import java.util.Date; import java.util.Properties; import javax.mail.Address; ...

  3. java spring 邮件发送

    开发中经常会遇到发送邮件进行用户验证,或者其它推送信息的情况,本文基于spring,完成邮件的发送,主要支持普通文本邮件的发送,html文本邮件的发送,带附件的邮件发送,没有实现群发.多个附件发送等需 ...

  4. Jenkins邮件配置,实现邮件发送策略(可实现每个Job对应不同的发送邮箱)

    前言: 首先,要有一个用来发送的邮箱,首选网易!参考:http://www.cnblogs.com/EasonJim/p/6051636.html,这里我注册了网易的免费企业邮箱. 并且我新建没多个邮 ...

  5. 【Java EE 学习 21 下】【使用java实现邮件发送、邮件验证】

    一.邮件发送 1.邮件发送使用SMTP协议或者IMAP协议,这里使用SMTP协议演示. SMTP协议使用的端口号:25 rfc821详细记载了该协议的相关信息 (1)使用telnet发送邮件(使用12 ...

  6. JAVA邮件发送的简单实现

    JAVA MAIL是利用现有的邮件账户发送邮件的工具,比如说,我在网易注册一个邮箱账户,通过JAVA Mail的操控,我可以不亲自登录网易邮箱,让程序自动的使用网易邮箱发送邮件.这一机制被广泛的用在注 ...

  7. java实现smtp邮件发送

    一.准备工作 首先你需要已一个发送邮箱,一般的邮箱都有SMTP.POP3服务,比如QQ邮箱,登陆QQ邮箱开启SMTP服务,开启是服务器会提示你设置独立密码,这个密码是跟邮箱正常登陆的密码不同的,这个是 ...

  8. 使用spring的邮件发送功能

    使用spring提供的MailSender和JavaMailSender类. 1.邮件对象类 package cn.luxh.app.mail; import java.util.List; impo ...

  9. Java实现多线程邮件发送

    利用java多线程技术配合线程池实现多任务邮件发送. 1.基本邮件发送MailSender package hk.buttonwood.ops.email; import java.io.File; ...

随机推荐

  1. html上传图片(进度条变化)、音乐

    <html> <head> <title>$Title$</title> </head> <link href="css/b ...

  2. ROS编译工作区缺少cv_bridge的问题解决

    cv_bridge是OpenCV与ROS之间的格式转换桥梁,编译工作区时遇到报错(目标不存在),直接将cv_bridge包复制到指定的目录即可. 下载地址:https://github.com/ros ...

  3. 【luogu P2024 食物链】 题解

    题目链接:https://www.luogu.org/problemnew/show/P2024 摘吊打集训队的九日dalao一句话 关于带有多个相对集合的全集,我们可以多开几倍的空间.每一倍的元素表 ...

  4. Services 在多个 controller 中共享数据。

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  5. AngularJS显示一个简单表格

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  6. Linux_vsftpd服务配置

    首先安装Linux 企业版第一张光盘中的vsftpd-2.0.1-5.i386.rpm#rpm –ivh /media/cdrom/RedHat/RPMS/vsftpd-3.0.1-5.i386.rp ...

  7. Flask—06-理解掌握flask数据模型(02)

    数据模型 模型关系 一对多(使用最多) 一:学生(Student) 需要添加反向引用 多:文章(Article) 需要添加外键关联 一对一 一:学生(Student),主表 需要添加反向引用,在一对多 ...

  8. Java程序如何生成Jar 执行文件(1)

    一.用Eclipse生产Jar文件 注意:此方法只能打包简单程序,不包含含有第三方jar包的项目 首先,看一下我的项目的目录结构: 1,项目名字上面点右键,选择Export,在选择java\JAR f ...

  9. 【TOJ 4475】The Coolest Sub-matrix(对角线前缀和)

    描述 Given an N*N matrix, find the coolest square sub-matrix.We define the cool value of the square ma ...

  10. 快速玩转linux(3)

    Linux常用命令 软件操作命令 执行操作 命令 软件包管理器 yum 安装软件 yum install xxx 卸载软件 yum remove xxx 搜索软件 yum search xxx 清除缓 ...