概念
JavaMail,顾名思义,提供给开发者处理电子邮件相关的编程接口。它是Sun发布的用来处理email的API。它可以方便地执行一些常用的邮件传输。我们可以基于JavaMail开发出类似于Microsoft Outlook的应用程序。
应用场景
一般在系统的注册模块,当用户填入注册信息的邮箱时,点击保存。系统根据用户的信息会自动给用户发送一封邮件,上面有用户的基本信息和注意事项,也可以用此方法实现用户的激活。
代码实现
普通方式一
1.首先引入javaMail的mail坐标即jar包
jar包:mail:1.4.1
坐标:
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.4</version>
</dependency>
2.测试代码实现
首先引入junit 测试包
package mail.test;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;
import org.junit.Test;
public class Mail01Test {
@Test
public void testJavaMail() throws Exception{
//1.设置邮件的一些信息
Properties props = new Properties();
//发送邮件的服务器地址
props.put("mail.smtp.host", "smtp.163.com");// stmp.qq.com smtp.sina.com
props.put("mail.smtp.auth", "true");
//2.创建Session对象
Session session =Session.getInstance(props);
//3.创建出MimeMessage,邮件的消息对象
MimeMessage message = new MimeMessage(session);
//4.设置发件人
Address fromAddr = new InternetAddress("发件人邮箱");
message.setFrom(fromAddr);
//5.设置收件人
Address toAddr = new InternetAddress("收件人邮箱");
message.setRecipient(RecipientType.TO, toAddr);
//6.设置邮件的主题
message.setSubject("项目进展顺序");
//7.设置邮件的正文
message.setText("项目进展顺序,所有兄弟们都非常努力,老板今天可以请吃饭");
message.saveChanges();//保存更新
//8.得到火箭
Transport transport = session.getTransport("smtp");
transport.connect("smtp.163.com", "发件人邮箱", "发件人密码");//设置了火箭的发射地址
transport.sendMessage(message, message.getAllRecipients());//发送具体内容及接收人
transport.close();
}
}
普通方式二
方式二可以带附件和图片
1.测试代码:
package mail.test;
import java.io.File;
import javax.mail.internet.MimeMessage;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
public class Mail03Test {
@Test
public void testJavaMail() throws Exception{
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-mail.xml");
//得到发送器
JavaMailSender mailSender = (JavaMailSender) ac.getBean("mailSender");
//得到一个MimeMessage对象
MimeMessage message = mailSender.createMimeMessage();
//产生出一个MimeMessageHelper helper
MimeMessageHelper helper = new MimeMessageHelper(message, true);//工具类本质是操作message消息 true代表可以带附件,图片
//3.使用helper工具类,设置邮件的发送者,接收者,主题,正文
helper.setFrom("发送邮箱");
helper.setTo("接收邮箱");
helper.setSubject("发送图片和附件");
helper.setText("<html><head></head><body><h1>hello!!spring image html mail</h1><a href=http://www.baidu.com>baidu</a><img src='cid:image' /></body></html>", true);
//指定cid的取值
FileSystemResource imgResource = new FileSystemResource(new File("E:/01分配权限原理分析.png"));
helper.addInline("image", imgResource);
//带附件
FileSystemResource fileResource = new FileSystemResource(new File("E:/javamail1_4_4.zip"));
helper.addAttachment("javamail1_4_4.zip", fileResource);
//发送
mailSender.send(message);
}
}
2.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<description>JavaMail的配置文件</description>
<!-- 加载mail.properties配置文件 -->
<context:property-placeholder location="classpath:mail.properties"/>
<!-- 简单消息对象创建 -->
<bean id="mailMessage" class="org.springframework.mail.SimpleMailMessage">
<property name="from" value="${mail.from}"></property>
</bean>
<!-- 2.创建发送器 -->
<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="UTF-8"></property>
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.debug">true</prop>
<prop key="mail.smtp.timeout">0</prop>
</props>
</property>
</bean>
</beans>
2.mail.properties文件:
mail.host=smtp.163.com
mail.username=@前面的用户名
mail.password=密码
mail.from=发件人邮箱全拼