我正在尝试使用Spring Boot发送电子邮件。我有3个文件如下
MailSender.java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
public class MailSender
{
@Autowired
private static JavaMailSender mailSender;
public static void main(String[] args)
{
sendemail();
}
public static void sendemail() {
MimeMessage mail = mailSender.createMimeMessage();
try {
MimeMessageHelper helper = new MimeMessageHelper(mail, true);
helper.setTo("[email protected]");
//helper.setReplyTo("someone@localhost");
helper.setFrom("[email protected]");
helper.setSubject("Lorem ipsum");
helper.setText("Lorem ipsum dolor sit amet [...]");
} catch (MessagingException e) {
e.printStackTrace();
} finally {}
mailSender.send(mail);
//return helper;
}
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>Example</groupId>
<artifactId>SendingEmail</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.2.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.5</version>
</dependency>
</dependencies>
</project>
和 application.properties
spring.mail.host=smtp.gmail.com
spring.mail.username=*****
spring.mail.password=*****
spring.mail.properties.mail.smtp.auth = true
spring.mail.properties.mail.smtp.socketFactory.port = 25
spring.mail.properties.mail.smtp.socketFactory.class = javax.net.ssl.SSLSocketFactory
spring.mail.properties.mail.smtp.socketFactory.fallback = false
spring.mail.properties.mail.smtp.ssl.enable=true
因此,现在当我尝试运行MailSender类时,它在行上给我一个错误“线程“main”中的异常java.lang.NullPointerException” MimeMessage mail = mailSender.createMimeMessage();
我知道mailSender属性为null,因为它无法从application.properties文件获取属性。
最佳答案
您也许可以寻求Spring-Boot-Email-Tools项目中提供的现成解决方案。
它还提供了有用的功能,例如计划,对模板引擎的支持以及REDIS的持久性。
如果您需要发送纯文本电子邮件,则只需导入核心模块
<dependency>
<groupId>it.ozimov</groupId>
<artifactId>spring-boot-email-core</artifactId>
<version>0.5.0</version>
</dependency>
注释主要如下:
package com.myapplication;
@SpringBootApplication
@EnableEmailTools
public class MainApplication {
public static void main(final String... args) {
}
}
在
application.yml
(o properties
)中提供配置,并在您的组件类中自动连接EmailService
,例如@Autowired
public EmailService emailService;
public void sendEmailWithoutTemplating(){
final Email email = EmailImpl.builder()
.from(new InternetAddress("[email protected]", "Marco Tullio Cicerone "))
.to(Lists.newArrayList(new InternetAddress("[email protected]", "Pomponius Attĭcus")))
.subject("Laelius de amicitia")
.body("Firmamentum autem stabilitatis constantiaeque eius, quam in amicitia quaerimus, fides est.")
.encoding(Charset.forName("UTF-8")).build();
emailService.send(email);
}
在项目的GitHub存储库中可以找到更多示例。另外,LinkedIn上还有this article。