JavaMail是提供给开发人员在应用程序中实现邮件发送和接收功能而提供的一套标准开发类库,支持常用的邮件协议,如SMTP、POP3、IMAP,开发人员使用JavaMail编写邮件程序时,无需考虑底层的通信细节(Socket),JavaMail也提供了能够创建出各种复杂MIME格式的邮件内容的API。使用JavaMail,我们可以实现类似OutLook、FoxMail的软件。
1. JavaMail的基本概述
1.1 邮件开发的相关协议
ØSMTP的全称是“Simple Mail Transfer Protocol”,即简单邮件传输协议(25号端口)。ØPOP3是Post Office Protocol 3的简称,即邮局协议的第3个版本,是TCP/IP协议族中的一员(默认端口是110)。ØIMAP全称是Internet Mail Access Protocol,即交互式邮件存取协议,它是跟POP3类似邮件访问标准协议之一
1.2 IMAP和POP3有什么区别?
POP3协议允许电子邮件客户端下载服务器上的邮件,但是在客户端的操作(如移动邮件、删除邮件、标记已读等),不会反馈到服务器上,比如通过客户端收取了邮箱中的3封邮件并移动到其他文件夹,邮箱服务器上的这些邮件是没有同时被移动的 。IMAP提供webmail 与电子邮件客户端之间的双向通信,客户端的操作都会反馈到服务器上,对邮件进行的操作,服务器上的邮件也会做相应的动作。
1.3POP3/IMAP、SMTP之间的区别和联系
简单地说,SMTP管“发”,POP3/IMAP管“收”。
举个例子,你坐在电脑边用mail client写完邮件,点击‘发送’。这时你的mail client会发消息给邮件服务器上的SMTP service。这时有两种情况:
1. 如果邮件的收信人也是处于同一个domain,比如从发送给163的邮箱,SMTP service只需要转给local的POP3 Service即可2. 如果邮件收信人是另外的domain,比如发送给, SMTP service需要通过询问DNS,找到属于sina的SMTP service的host其实一般每个提供电子邮件服务的网站都有自己的SMTP和POP服务器地址,在你登录你的电子邮件网站时仔细找找肯定能发现它们,如果找不到还可以查看一下网站的“帮助”。
使用javaMail需要你得邮箱申请开通POP3/SMTP服务,sina邮箱为例
在spring中引入需要加入mail.properties配值文件
mail.host=mail.username=你的邮箱账户mail.password=你的邮箱密码mail.from=你的邮箱
接着applicationContext-mail.xml文件
<beans xmlns="/schema/beans"xmlns:context="/schema/context" xmlns:p="/schema/p"xmlns:aop="/schema/aop" xmlns:tx="/schema/tx"xmlns:xsi="/2001/XMLSchema-instance"xsi:schemaLocation="/schema/beans /schema/beans/spring-beans.xsd/schema/context /schema/context/spring-context.xsd/schema/aop /schema/aop/spring-aop.xsd /schema/tx /schema/tx/spring-tx.xsd/schema/util /schema/util/spring-util.xsd"><bean id="mailMessage" class="org.springframework.mail.SimpleMailMessage"><property name="from"><value>${mail.from}</value></property><!-- 查看SimpleMailMessage源码还可以注入标题,内容等 --></bean><!-- 声明JavaMailSenderImpl对象 --><bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"><property name="defaultEncoding" value="UTF-8" /><property name="host" value="${mail.host}" /><property name="username" value="${mail.username}" /><property name="password" value="${mail.password}" /><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>
发送简答得邮件,需要你在spring配置dao读取数据库中读取数据库配置文件上加入
<context:property-placeholder location="classpath:db.properties,classpath:mail.properties" />
发送简单得文件可以测试
import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.mail.MailSender;import org.springframework.mail.SimpleMailMessage;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration("classpath:applicationContext-mail.xml")public class MailSendTest {@Autowiredprivate MailSender mailSender;@Autowiredprivate SimpleMailMessage simpleMailMessage;@Testpublic void SingleMailSend (){simpleMailMessage.setSubject("你好");simpleMailMessage.setText("这个是一个通过Spring框架来发送邮件的小程序");simpleMailMessage.setTo("要发给的邮箱号");mailSender.send(simpleMailMessage);}}
带图片的发送
@Testpublic void ImageMailSend () throws MessagingException {JavaMailSenderImpl sender = (JavaMailSenderImpl)mailSender;MimeMessage mailMessage = sender.createMimeMessage();MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage,true);messageHelper.setFrom("发送的邮箱");messageHelper.setTo("要发送到的邮箱");messageHelper.setSubject("测试邮件中嵌套图片!!");// true 表示启动HTML格式的邮件messageHelper.setText("<html><body><h1>hello!!spring image html mail</h1>"+"<img src=cid:image/></body></html>", true);FileSystemResource img = new FileSystemResource(new File("d://1.png"));messageHelper.addInline("image", img);//跟cid一致sender.send(mailMessage);System.out.println("邮件发送成功...");}