推荐使用标题5
1.安装使用易游服务器以及Foxmail
易游服务器傻瓜式安装
Foxmail 向导部分请断网
2.简单的入门案例
- 1.创建mevan项目
- 2.引入坐标
<!-- Javamail -->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.4</version>
</dependency>
- 3 入门邮箱代码
/*
java 邮箱的入门案例
*/
public class MailDemo1 {
public static void main(String[] args) throws Exception{
//1.配置发送邮箱的属性信息
Properties properties = new Properties();
properties.setProperty("mail.smtp.host","localhost");// 设置stmp协议主机(案例:使用我们本机/实际:取邮箱POP3/SMTP服务查找)
properties.setProperty("mail.smtp.auth","true");//设置stmp是否需要认证
//2.使用属性打开一个mail的会话 -->这里的session使用的是javax.mail.Session;
Session session = Session.getInstance(properties);
//3.设置会话为debug模式 ---> 可以不设置 设置后操作打印会更精细
session.setDebug(true);
//4.创建邮件的主体信息对象
MimeMessage mimeMessage = new MimeMessage(session);
//5.写入邮件内容
mimeMessage.setFrom(new InternetAddress("sass_ee88_01@export.com"));//设置发件人
mimeMessage.setSubject("测试邮件");//设置邮件主题
mimeMessage.setText("第一封java发送的邮件");//设置邮件的返送文本内容
/** TO : 发送 正常 一对一 发送 能看到收件人
* CC : 抄送 一对多 很多人都能收到 能看到收件人
* BCC : 密送 看不到收件人
* Message.RecipientType.TO --> .BCC ---> .CC
*/
mimeMessage.setRecipient(Message.RecipientType.TO,new InternetAddress("sass_ee88_02@export.com")); //设置收件人
//6.获取发送器对象
Transport transport = session.getTransport("smtp");//提供使用协议
//7.设置发送人信息(补充发件人信息)
transport.connect("localhost","sass_ee88_01","1234");
//8.发送邮箱 填入发送的内容 收件人对象(此参数为所有的收件人)
transport.sendMessage(mimeMessage,mimeMessage.getAllRecipients());
//9.释放资源
transport.close();
}
}
3.简单邮件代码提取成为工具类
package com.itheima.util;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Map;
import java.util.Properties;
/**
* 抽取工具类
* @author 黑马程序员
* @Company http://www.itheima.com
*/
public class MailUtil {
/**
*
* @param map 收件人地址 key是邮件地址 value是邮件的类型
* @param subject 邮件主题
* @param content 邮件内容
* @throws Exception
*/
public static void sendMail(Map<String,String> map , String subject, String content)throws Exception{
//1.配置发送邮件的属性信息
Properties properties = new Properties();
properties.setProperty("mail.smtp.host","localhost");//设置stmp协议的主机
properties.setProperty("mail.smtp.auth","true");//设置smtp是否需要认证
//2.使用属性打开一个mail的会话
Session session = Session.getInstance(properties);
//3.设置会话是debug模式
session.setDebug(true);
//4.创建邮件的主体信息对象
MimeMessage mimeMessage = new MimeMessage(session);
//5.设置发件人
mimeMessage.setFrom(new InternetAddress("saas_server@export.com"));
//6.设置邮件主题
mimeMessage.setSubject(subject);
//7.设置邮件正文
mimeMessage.setText(content);
//8.设置收件人
for(Map.Entry<String,String> me : map.entrySet()){
String email = me.getKey();//邮件地址
String type = me.getValue();//邮件类型
if("to".equalsIgnoreCase(type)){
//发送
mimeMessage.setRecipient(Message.RecipientType.TO,new InternetAddress(email));//TO:发送 CC:抄送 BCC:密送
}else if("cc".equalsIgnoreCase(type)){
//CC:抄送
mimeMessage.setRecipient(Message.RecipientType.CC,new InternetAddress(email));
}else if("bcc".equalsIgnoreCase(type)){
//BCC:密送
mimeMessage.setRecipient(Message.RecipientType.BCC,new InternetAddress(email));
}
}
//9.获取发送器对象
Transport transport = session.getTransport("smtp");//提供使用的协议
//10.设置发件人的信息
transport.connect("localhost","saas_server","1234");
//11.发送邮件
transport.sendMessage(mimeMessage,mimeMessage.getAllRecipients());
//12.释放资源
transport.close();
}
}
利用工具类 发送邮件
public static void main(String[] args) throws Exception{
Map<String,String> map = new HashMap<>();
map.put("saas_ee88@export.com","to");
map.put("saas_ee88_01@export.com","cc");
map.put("saas_ee88_02@export.com","bcc");
MailUtil.sendMail(map,"测试第二封邮件","使用工具类发送邮件....");
}
4 javax.mail与spring整合
导入俩者坐标
<!-- Javamail -->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.4</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>5.0.4.RELEASE</version>
</dependency>
mail.properties配置文件
#发件箱名称
mail.from=sass_ee88_01@export.com
#指定smtp服务器地址
mail.host=localhost
#指定发件箱的登陆用户名
mail.username=sass_ee88_01
#指定发件箱的登陆密码
mail.password=1234
#指定发送邮箱所用的编码
mail.encoding=UTF-8
#指定发送邮件的所用的协议 不写默认也是smtp
mail.protocol=smtp
applicationContext-mail.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 指定引入其他资源文件.properties文件 -->
<context:property-placeholder location="classpath:mail.properties"/>
<!-- 配置简单邮件消息对象 -->
<bean id="mailMessage" class="org.springframework.mail.SimpleMailMessage">
<!-- 此时我们只需要注入发件箱名称即可。不要注入主题,正文,收件箱等等信息,因为那些信息是不固定的 -->
<property name="from" value="${mail.from}"></property>
</bean>
<!-- 配置邮件发送器 -->
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="${mail.host}"></property>
<property name="username" value="${mail.username}"></property>
<property name="password" value="${mail.password}"></property>
<property name="defaultEncoding" value="${mail.encoding}"></property>
<property name="protocol" value="${mail.protocol}"></property>
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop><!-- 是否需要验证 -->
<prop key="mail.debug">true</prop><!-- 是否需要debug的信息 -->
<prop key="mail.smtp.timeout">0</prop><!-- 设置发送超时时间,以秒为单位。0为永不超时 -->
</props>
</property>
</bean>
</beans>
java运行测试代码
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
/**
* javaMail 和 spring整合的邮件发送
*/
public class MailDemo03 {
public static void main(String[] args) {
//获取spring容器对象
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext-mail.xml");
// 获取消息对象 通过spring提供的
SimpleMailMessage mailMessage = (SimpleMailMessage)context.getBean("mailMessage");
// 准备邮件内容
mailMessage.setSubject("spring和JavaMail整合的邮件发送第一次测试");//设置邮件主题
mailMessage.setText("这是一封spring和JavaMail整合的邮件发送第一次测试"); //设置邮件文本内容
mailMessage.setTo("sass_ee88_02@export.com");//设置收件人信息 ,此处可以多写 , 隔开
// mailMessage.setCc("sass_ee88_02@export.com");
// mailMessage.setBcc("sass_ee88_02@export.com");
// 获取发送器对象 通过spring提供的
JavaMailSenderImpl sender = (JavaMailSenderImpl) context.getBean("mailSender");
//last end 发送邮箱
sender.send(mailMessage);
}
}
5 上面的只是简单的邮件发送 (正常使用还有图片以及附件的发送)
**这里我们就可以使用spring提供的复杂邮件帮助对象了**
/**
* 带附件以及图片的邮件发送
*/
public class MailDeo04 {
public static void main(String[] args)throws Exception {
//1.获取spring容器
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext-mail.xml");
//2.获取发送器对象
JavaMailSenderImpl sender = (JavaMailSenderImpl) context.getBean("mailSender");
//3.使用获取MimeMessage(多用途物联网邮件数据类型)
MimeMessage mimeMessage = sender.createMimeMessage();
//4.创建spring提供的复杂邮件的帮助对象
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);//是否为多部分数据
//5.写入邮件信息
helper.setFrom("sass_ee88_01@export.com");//设置发件人邮箱
helper.setTo("sass_ee88_02@export.com");//设置收件人邮箱
helper.setSubject("我是一封多数据格式的邮件");//设置邮件的主题
String data = "<html><head></head><body><h1>你好</h1> <img src=cid:image ></body></html>" ;
helper.setText(data,true);//设置邮件的正文 是否为html文本内容
//填充 设置 image 图片路径
FileSystemResource resource = new FileSystemResource(new File("E:\\img\\13.jpg"));
//使用流 图片替换 cid 中image图片
helper.addInline("image",resource);
//添加附件 附件名称 附件来源流
helper.addAttachment("1.jpg",resource);
// last end 发送邮件
sender.send(mimeMessage);
}