2. 分析Spring mail的内部源码
我们写的DummySpringMail类中的sendMsg()方法,将实际要发送的消息通过MailSender的send方法发送了。我们看一下send方法内部的实现:
public void send(SimpleMailMessage simpleMessage) throws MailException {
send(new SimpleMailMessage[] { simpleMessage });
}
public void send(SimpleMailMessage[] simpleMessages) throws MailException {
List<MimeMessage> mimeMessages = new ArrayList<MimeMessage>(simpleMessages.length);
for (SimpleMailMessage simpleMessage : simpleMessages) {
MimeMailMessage message = new MimeMailMessage(createMimeMessage());
simpleMessage.copyTo(message);
mimeMessages.add(message.getMimeMessage());
}
doSend(mimeMessages.toArray(new MimeMessage[mimeMessages.size()]), simpleMessages);
}
我们可以看到,这里将发送的SimpleMailMessage消息转化为了javax.mail.internet.MimeMessage,这正是我们前文提到的。继续看doSend()方法:
protected void doSend(MimeMessage[] mimeMessages, Object[] originalMessages) throws MailException {
Map<Object, Exception> failedMessages = new LinkedHashMap<Object, Exception>();
Transport transport;
try {
transport = getTransport(getSession()); //这里,spring是通过javax.mail.Session的getInstance(Properties props)方法创建的实例。
transport.connect(getHost(), getPort(), getUsername(), getPassword()); //连接到我们配置的邮件服务器。
}
catch (AuthenticationFailedException ex) {
throw new MailAuthenticationException(ex);
}
catch (MessagingException ex) {
// Effectively, all messages failed...
for (int i = 0; i < mimeMessages.length; i++) {
Object original = (originalMessages != null ? originalMessages[i] : mimeMessages[i]);
failedMessages.put(original, ex);
}
throw new MailSendException("Mail server connection failed", ex, failedMessages);
}
try {
for (int i = 0; i < mimeMessages.length; i++) {
MimeMessage mimeMessage = mimeMessages[i];
try {
if (mimeMessage.getSentDate() == null) {
mimeMessage.setSentDate(new Date());
}
String messageId = mimeMessage.getMessageID();
mimeMessage.saveChanges();
if (messageId != null) {
// Preserve explicitly specified message id...
mimeMessage.setHeader(HEADER_MESSAGE_ID, messageId); // 添加消息头部信息
}
transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients()); //这里是发送mail的实际代码
}
catch (MessagingException ex) {
Object original = (originalMessages != null ? originalMessages[i] : mimeMessage);
failedMessages.put(original, ex);
}
}
}
finally {
try {
transport.close();
}
catch (MessagingException ex) {
if (!failedMessages.isEmpty()) {
throw new MailSendException("Failed to close server connection after message failures", ex,
failedMessages);
}
else {
throw new MailSendException("Failed to close server connection after message sending", ex);
}
}
}
if (!failedMessages.isEmpty()) {
throw new MailSendException(failedMessages);
}
}
看一下创建Session实例的org.springframework.mail.javamail.JavaMailSenderImpl类的getSession()方法:
public synchronized Session getSession() {
if (this.session == null) {
this.session = Session.getInstance(this.javaMailProperties);
}
return this.session;
}
这里并没有真正的创建Session实例,而是执行了javax.mail.Session中的getInstance()方法来创建实例的。
public static Session getInstance(Properties props) {
return new Session(props, null);
}
到这里,从建立Session,到解析字符邮件地址为Address类型,去构造消息Message实例,最后通过Transport类的send方法发送消息。spring中的mail发送邮件细节已经全部完毕了。那么,apache中的commons-email,发送邮件的原理也就彼此彼此!
分享到:
相关推荐
springboot 整合spring-boot-starter-mail 发邮件
maven配置了阿里云的仓库,不能下载spring-boot-starter-mail的依赖,从官网下载的,希望对有同样问题的小伙伴有用
spring.mail.default-encoding=UTF-8 #电子邮件地址 spring.mail.host=smtp.126.com #Application spring.application.name=SEND-MAIL #授权密码 spring.mail.password=password #邮箱服务器默认端口 spring.mail....
Spring Boot 集成各种框架 使用案例(spring-boot-rabbitmq、spring-boot-mail、spring-boot-thymeleaf、spring-boot-shiro)
spring-boot-examples-master示例程序,与各种...spring-boot-mail spring-boot-mongodb spring-boot-mybatis spring-boot-rabbitmq spring-boot-redis spring-boot-shiro spring-boot-swagger spring-boot-web
在"spring-boot-mail-master"目录下,除了源代码之外,可能还包括了项目的结构说明、配置文档以及使用示例等内容,这些都可以帮助我们更好地理解和使用这个邮件发送服务。 总的来说,"spring-boot-mail邮件发送服务...
10\spring-integration-mail-4.3.0.RELEASE.jar 11\spring-jdbc-4.3.0.RELEASE.jar 12\spring-messaging-4.3.0.RELEASE.jar 13\spring-orm-4.3.0.RELEASE.jar 14\spring-oxm-4.3.0.RELEASE.jar 15\spring-retry-...
- [spring-boot-mail](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-mail):Spring Boot 和邮件服务 - [spring-boot-mongodb]...
2号包: net.sourceforge.serp net.sourceforge.xslthl org.antlr org.aopalliance javax.activation javax.annotation javax.ejb javax.el javax.faces javax.inject javax.jdo javax.jms javax.mail javax....
Spring Mail是一个Java库,它简化了通过JavaMail API发送电子邮件的过程。本教程将详细讲解如何利用Spring Mail通过Gmail的SSL(安全套接层)协议来发送邮件。 首先,我们需要在项目中引入Spring Mail的依赖。如果...
下面将详细介绍如何在Spring Boot项目中整合Mail并实现简单的邮件发送。 首先,确保在项目中添加了必要的依赖。Spring Boot提供了对JavaMailSender的支持,我们可以在`pom.xml`或`build.gradle`文件中引入`spring-...
2号包: net.sourceforge.serp net.sourceforge.xslthl org.antlr org.aopalliance javax.activation javax.annotation javax.ejb javax.el javax.faces javax.inject javax.jdo javax.jms javax.mail javax....
spring-boot集成实例_mybatis,mail,mysql,mongodb,cassand_spring-boot-integration
2. **内嵌式Web服务器**:Spring Boot支持内嵌Tomcat、Jetty或Undertow等Web服务器,这使得你可以直接运行一个jar文件启动应用,无需额外部署到外部服务器。 3. **起步依赖(Starter POMs)**:Spring Boot提供了多...
javax.mail javax.persistence javax.portlet javax.resource javax.servlet javax.transaction javax.validation javax.xml.bind javax.xml.rpc javax.xml.soap javax.xml.stream javax.xml.ws ...
javax.mail-1.5.2.jar jboss-logging-3.2.1.Final.jar jcl-over-slf4j-1.7.7.jar jedis-2.5.1.jar joda-time-2.6.jar jstl-1.2.jar jtds-1.3.1.jar jul-to-slf4j-1.7.7.jar junit-4.11.jar log4j-1.2.17.jar mail-...
spring-boot-mail:spring boot和邮件服务 spring-boot-mongodb:spring boot和mongodb的使用 spring-boot-multi-mongodb:spring boot和mongodb多数据源的使用 spring-boot-package-war:spring-boot打包成war包...
aop,beans,cache,context,core,dao,ejb,instument,jca,jdbc,jms,jmx,jndi,mail,metadate,mock,orm,remoting,scheduling,scripting,stereotype,test,transcation,ui,util,validation,web 以上数十子模块源码全部为...
spring-boot-mail:spring boot和邮件服务 spring-boot-mongodb:spring boot和mongodb的使用 spring-boot-multi-mongodb:spring boot和mongodb多数据源的使用 spring-boot-package-war:spring-boot打包成war包...
在实际开发中,Spring Boot Starter还提供了许多其他启动器,如`spring-boot-starter-data-jpa`用于数据库操作,`spring-boot-starter-data-rest`用于构建RESTful服务,`spring-boot-starter-mail`用于发送邮件,...