mail.jar 发送邮件
1.spring参数注入+util 发送邮件
2.util配置参数+util发送邮件
1.spring参数注入+util 发送邮件
<bean id="mailSender" class="com.midea.ftms.util.MailSender">
<property name="host" value="${mail.smtp.host}"></property>
<property name="auth" value="${mail.smtp.auth}"></property>
<property name="user" value="${mail.user}"></property>
<property name="password" value="${mail.passwd}"></property>
<property name="from" value="${mail.from}"></property>
<property name="remindNum" value="${mail.remindnum}"></property>
<property name="debugModel" value="${mail.debug}"></property>
</bean>
import java.util.Properties; import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage; public class MailSender { private String host;
private String auth;
private String user;
private String password;
private String from;
private Integer remindNum;
private Boolean debugModel; public MailSender() { } /**
* @see 发送邮件基础方法,请遵循使用规则 MailUtil.sendMail
* @param to 邮件接收地址
* @param subject 邮件主题
* @param content 邮件内容
* @throws Exception 调用者处理异常
*/
public void send(String[] to, String subject, String content)
throws Exception {
Properties props = new Properties();
// 指定SMTP服务器
props.put("mail.smtp.host", host);
// 指定是否需要SMTP验证
props.put("mail.smtp.auth", auth);
Session mailSession = Session.getDefaultInstance(props);
// 是否在控制台显示debug信息
mailSession.setDebug(debugModel);
Message message = new MimeMessage(mailSession);
// 发件人
message.setFrom(new InternetAddress(from));
// 收件人
InternetAddress[] addresses = new InternetAddress[to.length];
for (int i = ; i < to.length; i++) {
addresses[i] = new InternetAddress(to[i]);
}
message.setRecipients(Message.RecipientType.TO, addresses);
// 邮件主题
message.setSubject("subject:"+subject);
// 邮件内容(HTML格式)
message.setContent(content, "text/html;charset=GBK");
// 保存设置,让设置生效
message.saveChanges();
// 发送
Transport transport = mailSession.getTransport("smtp");
transport.connect(host, user, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
} public String getHost() {
return host;
} public void setHost(String host) {
this.host = host;
} public String getAuth() {
return auth;
} public void setAuth(String auth) {
this.auth = auth;
} public String getUser() {
return user;
} public void setUser(String user) {
this.user = user;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} public String getFrom() {
return from;
} public void setFrom(String from) {
this.from = from;
} public Integer getRemindNum() {
return remindNum;
} public void setRemindNum(Integer remindNum) {
this.remindNum = remindNum;
} public Boolean getDebugModel() {
return debugModel;
} public void setDebugModel(Boolean debugModel) {
this.debugModel = debugModel;
} }
public class MailUtil {
private static volatile MailSender mailSender;
private MailUtil() {
}
public static MailSender init() {
if (mailSender == null) {
synchronized (MailSender.class) {
if (mailSender == null) {
// mailSender = new MailSender();
mailSender = (MailSender)ContextUtil.getContext().getBean("mailSender");
}
}
}
return mailSender;
}
public static void sendMail(String[] to, String subject, String content)
throws Exception {
MailUtil.init().send(to, subject, content);
}
public static void main(String[] args) {
MailUtil.init().setAuth("true");
MailUtil.init().setDebugModel(true);
MailUtil.init().setFrom("a@b.com");
MailUtil.init().setHost("cd.com.cn");
MailUtil.init().setUser("user");
MailUtil.init().setPassword("passwd");
MailUtil.init().setRemindNum();
try {
MailUtil.sendMail(new String[]{"asdfa@qq.com","134324323@qq.com"}, "测试", "hello yoyo");
} catch (Exception e) {
e.printStackTrace();
}
}
}
2.util配置参数+util发送邮件
import java.util.Properties; import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage; public class MailSendUtil { private String host;
private String auth;
private String user;
private String password;
private String from;
private Integer remindNum;
private Boolean debugModel; private volatile static MailSendUtil mailSendUtil; private MailSendUtil(){
init();
} private void init() {
host = PropertiesUtil.getProperty("mail.smtp.host");
auth = PropertiesUtil.getProperty("mail.smtp.auth");
user = PropertiesUtil.getProperty("mail.user");
password = PropertiesUtil.getProperty("mail.passwd");
from = PropertiesUtil.getProperty("mail.from");
remindNum = Integer.parseInt(PropertiesUtil.getProperty("mail.remindnum"));
debugModel = Boolean.valueOf(PropertiesUtil.getProperty("mail.debug"));
} public static MailSendUtil getInstance() {
if (mailSendUtil == null) {
synchronized (MailSendUtil.class) {
if (mailSendUtil == null) {
return new MailSendUtil();
}
}
}
return mailSendUtil;
} public static void sendMail(String[] to, String subject, String content)
throws Exception {
MailSendUtil.getInstance().send(to, subject, content);
} /**
* @see 发送邮件基础方法,请遵循使用规则 MailUtil.sendMail
* @param to 邮件接收地址
* @param subject 邮件主题
* @param content 邮件内容
* @throws Exception 调用者处理异常
*/
public void send(String[] to, String subject, String content)
throws Exception {
Properties props = new Properties();
// 指定SMTP服务器
props.put("mail.smtp.host", host);
// 指定是否需要SMTP验证
props.put("mail.smtp.auth", auth);
Session mailSession = Session.getDefaultInstance(props);
// 是否在控制台显示debug信息
mailSession.setDebug(debugModel);
Message message = new MimeMessage(mailSession);
// 发件人
message.setFrom(new InternetAddress(from));
// 收件人
InternetAddress[] addresses = new InternetAddress[to.length];
for (int i = ; i < to.length; i++) {
addresses[i] = new InternetAddress(to[i]);
}
message.setRecipients(Message.RecipientType.TO, addresses);
// 邮件主题
message.setSubject("subject:"+subject);
// 邮件内容(HTML格式)
message.setContent(content, "text/html;charset=GBK");
// 保存设置,让设置生效
message.saveChanges();
// 发送
Transport transport = mailSession.getTransport("smtp");
transport.connect(host, user, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
} }
3.遇到发送邮件正常,但是没有主题,也没有收件人的情况,请删除 geronimo-javamail_1.4_spec-1.2.jar
mail.jar 发送邮件的更多相关文章
- Java使用javax.mail.jar发送邮件并同意发送附件
因为Java在开发网页上占有绝大优势.所以作为web端的领军人物,譬如发送短信和发送邮件这些就成了必定,网络安全一再安全我们须要把账号的安全级别提到更高.因此这些对于开发者也就成了必须掌握的技能!我一 ...
- java mail jar冲突
开发环境:jdk1.6.0_25 MyEclipse-8.6 J2EE5 程序编译通过,J2EE5的库里面已经含有javaee.jar文件.里面的javax.mail包下面是jav ...
- java Mail如何发送邮件
1.应用场景:在系统需要发送与用户相关的消息时,而用户不在线,可以采取发送邮件的方式,使用户了解最新的系统情况 或者发送验证码等验证场景 2.实验环境 主要使用mail.jar和activation. ...
- Java 基于mail.jar 和 activation.jar 封装的邮件发送工具类
准备工作 发送邮件需要获得协议和支持! 开启服务 POP3/SMTP 服务 如何开启 POP3/SMTP 服务:https://www.cnblogs.com/pojo/p/14276637.html ...
- Java 使用 mail.jar 实现邮件发送
目录 准备工作 使用到的 jar 包 实现代码 准备工作 要想实现邮件发送, 需要先打开发送邮箱的 POP3/SMTP 服务,打开方式在 设置>帐户 中去打开,打开之后如果是qq邮箱会获得一个授 ...
- Linux下使用mail命令发送邮件
因为需要经常备份网站的数据,所以了解并学习了下linux下如何通过shell来发送邮件,这里以CentOS为例,使用mail命令来进行外部邮件的发送.mail命令的语法如下: Usage: mail ...
- 用linux mail命令发送邮件[Linux]
mail [-s 邮件标题] <<邮件地址1> [邮件地址2] [邮件地址3]> [ < 包含邮件内容的文件路径 ] <-- -f 发送邮件地址> [-F 显 ...
- 浏览器访问php脚本通过sendmail用mail函数发送邮件
前几天做项目遇到这样的一个问题:当某一个结点下有新的文章发表的时候,以邮件的形式通知该结点下的所有用户.这就需要用到邮件发送的功能. 因为项目是php语言做的,所以最简单的方法就是使用php自带的函数 ...
- Ubuntu下使用mail命令发送邮件
Ubuntu下使用mail命令发送邮件 mail命令在Ubuntu下是需要安装的,使用下条命令进行安装: sudo apt-get install heirloom-mailx 接下来输入用户密码,等 ...
随机推荐
- java学习进度
上周日玩的比较high,忘记写进度报告,今天补上.通过这些天的学习,我对java有了初步的认识,java和c有很多的不同和相同之处,然后就是java的代码在记事本里写,等慢慢深入之后又可以用eclip ...
- Spark分布式安装
三台 服务器 n0,n2,n3 centos 6.4 X64 JDK, SCALA 2.11 Hadoop 2.2.0 spark-0.9.1-bin-hadoop2.tgz 说明: 1.所有机器上安 ...
- vux,vue 苹果手机使用position:fixed有问题,如何解决
苹果手机真是各种坑,导致我都想摔手机呀,但没办法,用苹果的人太多,程序员还是继续在坑的路上行走! 上一篇文章介绍了一些组件,就是使用vux可以解决,苹果手机使用position:fixed的问题 给需 ...
- html标签积累
<marquee>滚动标签 <marquee>标签,它是成对出现的标签,首标签<marquee>和尾标签</marquee>之间的内容就是滚动内容.&l ...
- Oracle PL/SQL异常、存储过程和触发器
一.异常 1.处理异常 (1)除数不为0 declare b number; begin b:; exception when zero_divide then dbms_output.put_lin ...
- postgresql install 报错
install.pm could not copy postgres.exe to ... 错误原因:目标文件夹的父目录不存在
- 更改pip源至国内镜像
更改pip源至国内镜像 经常在使用Python的时候需要安装各种模块,而pip是很强大的模块安装工具,但是由于国外官方pypi经常被墙,导致不可用,所以我们最好是将自己使用的pip源更换一下,这样 ...
- daal utils printNumericTable
#=============================================================================== # Copyright 2014-20 ...
- git报错fatal: I don't handle protocol 'https'处理
一.背景说明 今天使用在Cygwin中git clone时报fatal: I don't handle protocol 'https',如下: 以为是Cygwin实现的git有点问题没太在意,换去 ...
- git 从远程拉取代码、推代码的步骤
(注:如果是几个人共同管理项目,并且你的队友在你之前推过代码,那你就需要 git pull 一下,把代码拉到本地,解决一下冲突,再执行以下步骤,将本地代码推到远程仓库.) 第一步:查看当前的git仓库 ...