Java发送Email邮件及SpringBoot集成
一:普通方式发送
1.导包
<!--Java MAil 发送邮件API-->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>javax.mail-api</artifactId>
<version>1.6.1</version>
</dependency>
<!-- 真正的实现库 -->
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.1</version>
</dependency>
2.代码
- 实例:通过一个已知的163邮箱发送给他人邮件
public static void main(String[] args) throws MessagingException {
// 1.创建一个程序与邮件服务器会话对象 Session
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "SMTP");
props.setProperty("mail.smtp.host", "smtp.163.com");
props.setProperty("mail.smtp.port", "25");
// 指定验证为true
props.setProperty("mail.smtp.auth", "true");
props.setProperty("mail.smtp.timeout", "1000");
// 验证账号及密码,密码对应邮箱授权码(163密码可行,qq必须授权码)
Authenticator auth = new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("xxxx@163.com", "xxxxx");
}
};
Session session = Session.getInstance(props, auth);
// 2.创建一个Message,它相当于是邮件内容
Message message = new MimeMessage(session);
// 设置发送者
message.setFrom(new InternetAddress("xxx同上xxx@163.com"));
// 设置发送方式与接收者
message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress("xx10086xx@qq.com"));
// 设置主题
message.setSubject("邮件发送测试");
// 设置内容
message.setContent("Hello!", "text/html;charset=utf-8");
// 3.创建 Transport用于将邮件发送
Transport.send(message);
}
二:SpringBoot的JavaMailSenderImpl发送
1.在resources下新建mailConfig.properties
mailConfig.properties
#服务器
mailHost=smtp.163.com
#端口号
mailPort=25
#邮箱账号(使用者改成自己的)
mailUsername=xxxxxx@163.com
#邮箱密码(使用者改成自己的)
mailPassword=xxxxx
#时间延迟
mailTimeout=25000
2.新建一个类去读这个配置文件的内容
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
* Create by yster@foxmail.com 2018/5/28/028 21:00
*/
public class MailConfig {
private static final String PROPERTIES_DEFAULT = "mailConfig.properties";
public static String host;
public static Integer port;
public static String userName;
public static String passWord;
public static String emailForm;
public static String timeout;
public static String personal;
public static Properties properties;
static{
init();
}
/**
* 初始化
*/
private static void init() {
properties = new Properties();
try{
InputStream inputStream = MailConfig.class.getClassLoader().getResourceAsStream(PROPERTIES_DEFAULT);
properties.load(inputStream);
inputStream.close();
host = properties.getProperty("mailHost");
port = Integer.parseInt(properties.getProperty("mailPort"));
userName = properties.getProperty("mailUsername");
passWord = properties.getProperty("mailPassword");
emailForm = properties.getProperty("mailUsername");
timeout = properties.getProperty("mailTimeout");
personal = "来自星星的我";//发送人
} catch(IOException e){
e.printStackTrace();
}
}
}
3.以上只是准备阶段,下面开始邮件编码
import cn.zyzpp.config.MailConfig;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
/**
* Create by yster@foxmail.com 2018/5/28/028 21:05
*/
public class MailUtil {
private static final String HOST = MailConfig.host;
private static final Integer PORT = MailConfig.port;
private static final String USERNAME = MailConfig.userName;
private static final String PASSWORD = MailConfig.passWord;
private static final String emailForm = MailConfig.emailForm;
private static final String timeout = MailConfig.timeout;
private static final String personal = MailConfig.personal;
private static JavaMailSenderImpl mailSender = createMailSender();
/**
* 邮件发送器
*
* @return 配置好的工具
*/
private static JavaMailSenderImpl createMailSender() {
JavaMailSenderImpl sender = new JavaMailSenderImpl();
sender.setHost(HOST);
sender.setPort(PORT);
sender.setUsername(USERNAME);
sender.setPassword(PASSWORD);
sender.setDefaultEncoding("Utf-8");
Properties p = new Properties();
p.setProperty("mail.smtp.timeout", timeout);
p.setProperty("mail.smtp.auth", "false");
sender.setJavaMailProperties(p);
return sender;
}
/**
* 发送邮件
*
* @param to 接受人
* @param subject 主题
* @param html 发送内容
* @throws MessagingException 异常
* @throws UnsupportedEncodingException 异常
*/
public static void sendMail(String to, String subject, String html) throws MessagingException,UnsupportedEncodingException {
MimeMessage mimeMessage = mailSender.createMimeMessage();
// 设置utf-8或GBK编码,否则邮件会有乱码
MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
messageHelper.setFrom(emailForm, personal);
messageHelper.setTo(to);
messageHelper.setSubject(subject);
messageHelper.setText(html, true);
mailSender.send(mimeMessage);
}
4.开始发送一封邮件
public static void main(String[] args){
try {
MailUtil.sendMail("xxxx@qq.com", "标题", "内容");
} catch (MessagingException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
欢迎关注博主!
Java发送Email邮件及SpringBoot集成的更多相关文章
- 用java发送email邮件例子
package com.hzk.mail; import java.net.MalformedURLException; import java.net.URL; import java.text.S ...
- java mail Received fatal alert: handshake_failure java 无法发送邮件问题 java 发送qq邮件(含源码)
java 无法发送邮件问题 java 发送qq邮件 报错:java mail Received fatal alert: handshake_failure (使用ssl) javax.mail.M ...
- java发送email一般步骤
java发送email一般步骤 一.引入javamail的jar包: 二.创建一个测试类,实现将要发送的邮件内容写入到计算机本地,查看是否能够将内容写入: public static void mai ...
- Java发送email的端口问题
Could not connect to SMTP host: smtp.***.com, port: 465, response: -1 使用Java发送email 的端口问题.一般使用25端口即可 ...
- C#发送Email邮件(实例:QQ邮箱和Gmail邮箱)
下面用到的邮件账号和密码都不是真实的,需要测试就换成自己的邮件账号. 需要引用: using System.Net.Mail; using System.Text; using System.Net; ...
- [转]C#发送Email邮件 (实例:QQ邮箱和Gmail邮箱)
下面用到的邮件账号和密码都不是真实的,需要测试就换成自己的邮件账号. 需要引用:using System.Net.Mail;using System.Text;using System.Net; 程序 ...
- 【转】C#发送Email邮件
转自:http://hi.baidu.com/bluesky_cn/item/8bb060ace834c53f020a4df2 下面用到的邮件账号和密码都不是真实的,需要测试就换成自己的邮件账号. 需 ...
- java发送email
package com.assess.util; import java.io.File; import java.util.ArrayList; import java.util.List; imp ...
- 【工具】java发送验证码邮件
文章目录 前言 配置邮箱服务器 代码实现 发送随机验证码与验证 后记 前言 要实现 可以设置格式,附件,抄送等功能,就跟真人操控邮箱发送邮件一样的功能,或许比较难,博主没研究,博主暂时没用到那些功能, ...
随机推荐
- Android 打包混淆
将项目改成Module //项目build.gradle的applicationId注释掉 修改apply plugin: 'com.android.library' 打包混淆脚本 //在项目的bui ...
- Testlink1.9.17使用方法(第五章 测试用例管理)
第五章 测试用例管理 QQ交流群:585499566 TestLink支持的测试用例的管理包含二层:分别为新建测试用例集(Test Suites).创建测试用例(Test Cases).可以把测试用例 ...
- wap2app(七)-- 长按保存图片
用Hbuilder打包网站,在打包好的app中是无法像网站那样直接使用长按图片保存的功能的,需要在网站里对来自wap2app进行单独处理,接下来介绍一下如何在Hbuilder打包后的app里实现长按图 ...
- jQuery 实现文字不停闪烁效果
使用jQuery实现的小效果:文字不停地闪烁. var flag = true; var text= $('#blink').text(); // blink是需要闪烁的元素id function b ...
- 使用VSTS的Git进行版本控制(七)——管理仓库
使用VSTS的Git进行版本控制(七)--管理仓库 在团队项目中创建Git repo管理项目的源代码.每个Git repo都有自己的权限和分支,可以与项目中的其他工作隔离开来. 任务1:从web门户创 ...
- linux hadoop2.x快速安装
........ http://blog.csdn.net/se7en_q/article/details/47258007
- C#编辑EXE使用的appSettings节点的Config文件
/// <summary> /// 保存配置文件的设定 /// </summary> /// <param name="Key"></pa ...
- Linux的notifier机制的应用
在linux内核系统中,各个模块.子系统之间是相互独立的.Linux内核可以通过通知链机制来获取由其它模块或子系统产生的它感兴趣的某些事件. notifier_block结构体在include/lin ...
- c/c++赋值函数(重载=号运算符)
c/c++赋值函数(重载=号运算符) 首先c++里的各种运算符都是用函数实现的,比如=,就等号函数. 所以当用=给一个对象赋值的时候,实际调用的是=号所对应的=号函数. 分析下面的代码 #includ ...
- CentOS7 Python2 和Python3 共存(Python3安装)【转】
1.查看是否已经安装Python CentOS 7.2 默认安装了python2.7.5 因为一些命令要用它比如yum 它使用的是python2.7.5. 使用 python -V 命令查看一下是否安 ...