Ø SMTP 的全称是“Simple Mail Transfer Protocol”,即简单邮件传输协议(25号端口)。
Ø POP3是Post Office Protocol 3的简称,即邮局协议的第3个版本,是TCP/IP协议族中的一员(默认端口是110)。
Ø IMAP全称是Internet Mail Access Protocol,即交互式邮件存取协议,它是跟POP3类似邮件访问标准协议之一
1.1 IMAP和POP3有什么区别?
1.2 POP3/IMAP、SMTP之间的区别和联系
简单地说,SMTP管“发”,POP3/IMAP管“收”。
举个例子,你坐在电脑边用mail client写完邮件,点击‘发送’。这时你的mail client会发消息给邮件服务器上的SMTP service。这时有两种情况:
1. 如果邮件的收信人也是处于同一个domain,比如从给163的邮箱,SMTP service只需要转给local的POP3 Service即可
2. 如果邮件收信人是另外的domain,比如
其实一般每个提供电子邮件服务的网站都有自己的SMTP和POP服务器地址,在你登录你的电子邮件网站时仔细找找肯定能发现它们,如果找不到还可以查看一下网站的“帮助”。
1. JavaMail的准备工作
1.1 申请邮箱
进入后开通POP3/SMTP服务
1.1 JavaMail 依赖
javax.mail
1.4.4
1.1 测试
public class JavaMailTest {
@Test
public void test() throws Exception {
//设置发送者的配置信息 Properties properties = new Properties();
//发送者的协议 properties.put("mail.smtp.host","smtp.126.com");
properties.put("mail.smtp.auth","true");
Session session = Session.getInstance(properties);
//设置调试信息在控制台打印出来 session.setDebug(true);
//信息体 MimeMessage message = new MimeMessage(session);
//发信者 Address address = new InternetAddress("guangweima@126.com");
message.setFrom(address);
//收信者 Address toAddress = new InternetAddress("361499689@qq.com");
/*** 设置收件人地址(可以增加多个收件人、抄送、密送)* MimeMessage.RecipientType.TO:发送* MimeMessage.RecipientType.CC:抄送* MimeMessage.RecipientType.BCC:密送*/
message.setRecipient(MimeMessage.RecipientType.TO,toAddress);
//主题 message.setSubject("米西米西");
//正文 message.setText("哈喽啊,饭已ok啦,下来米西吧!!!");
message.saveChanges();
//发送邮件 Transport transport = session.getTransport("smtp");
//登录 transport.connect("guangweima@126.com", "361499beyond");
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
}
1.3 JavaMail的三个核心类
1. 4 Spring整合javaMail
1.提取相关属性变量
mail.host=smtp.qq.com //邮箱服务器mail.username=//登录的邮箱mail.password=//邮箱的授权码mail.from=//设置从那个邮箱发送
2.整合文件的编写
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsdhttp://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.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
${mail.from}
true
true
0
3.测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext-mail.xml")
public class TestMail {
@Autowired
private MailSender mailSender;
@Autowired
private SimpleMailMessage simpleMailMessage;
@Test
public void test() {
for (int i = 0; i < 30; i++) {
simpleMailMessage.setSubject("主题");
simpleMailMessage.setText("发送的信息");
simpleMailMessage.setTo("发送的邮箱地址");
mailSender.send(simpleMailMessage);//发送信息}
}
}