我在尝试使用Spring启动电子邮件功能时遇到以下异常:
Caused by: org.springframework.mail.MailSendException: Failed messages: java.lang.NullPointerException
这是我的邮件配置类:
@Configuration
public class MailConfiguration {
private final MailProperties mailProperties;
@Autowired
public MailConfiguration (MailProperties mailProperties) {
this.mailProperties = mailProperties;
}
@Bean
public JavaMailSender javaMailSender() {
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
Properties properties = new Properties();
properties.put("mail.smtp.auth", mailProperties.getSmtp().isAuth());
properties.put("mail.smtp.starttls.enable", mailProperties.getSmtp().isStarttlsEnable());
mailSender.setJavaMailProperties(properties);
mailSender.setHost(mailProperties.getHost());
mailSender.setPort(mailProperties.getPort());
mailSender.setProtocol(mailProperties.getProtocol());
mailSender.setUsername(mailProperties.getUsername());
mailSender.setPassword(mailProperties.getPassword());
return mailSender;
}
}
这是邮件属性类:
@Configuration
@ConfigurationProperties(prefix = "mail", locations = "classpath:mail.properties")
public class MailProperties {
public static class Smtp {
private boolean auth;
private boolean starttlsEnable;
public boolean isAuth () {
return auth;
}
public void setAuth (boolean auth) {
this.auth = auth;
}
public boolean isStarttlsEnable () {
return starttlsEnable;
}
public void setStarttlsEnable (boolean starttlsEnable) {
this.starttlsEnable = starttlsEnable;
}
}
@NotBlank
private String host;
private int port;
private String from;
private String username;
private String password;
private String protocol;
@NotNull
private Smtp smtp;
public String getHost () {
return host;
}
public void setHost (String host) {
this.host = host;
}
public int getPort () {
return port;
}
public void setPort (int port) {
this.port = port;
}
public String getFrom () {
return from;
}
public void setFrom (String from) {
this.from = from;
}
public String getUsername () {
return username;
}
public void setUsername (String username) {
this.username = username;
}
public String getPassword () {
return password;
}
public void setPassword (String password) {
this.password = password;
}
public Smtp getSmtp () {
return smtp;
}
public void setSmtp (Smtp smtp) {
this.smtp = smtp;
}
public String getProtocol () {
return protocol;
}
public void setProtocol (String protocol) {
this.protocol = protocol;
}
}
这是邮件服务类:
public class MailServiceImpl implements MailService {
private final JavaMailSender javaMailSender;
private static final Logger log = LoggerFactory.getLogger(MailServiceImpl.class);
@Autowired
public MailServiceImpl (JavaMailSender javaMailSender) {
this.javaMailSender = javaMailSender;
}
@Override
public void send (String receiver, String sender, String message, String filenameAndLocation) {
SimpleMailMessage msg = new SimpleMailMessage();
msg.setText(receiver);
msg.setFrom(sender);
msg.setText(message);
msg.setSubject("testing subject");
javaMailSender.send(msg);
}
}
我只是试着调用这样的方法:mailService.send(" someone@com.com"," someone@com.com"," test&# 34;,"测试");
答案 0 :(得分:0)
我的邮件服务类不正确,这是正确的:
@Service
public class MailServiceImpl implements MailService {
private final JavaMailSender javaMailSender;
private static final Logger log = LoggerFactory.getLogger(MailServiceImpl.class);
@Autowired
public MailServiceImpl (JavaMailSender javaMailSender) {
this.javaMailSender = javaMailSender;
}
@Override
public void send (String receiver, String sender, String message, String filenameAndLocation) {
MimeMessagePreparator preparator = new MimeMessagePreparator() {
@Override
public void prepare (MimeMessage mimeMessage) throws Exception {
MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage);
messageHelper.setTo(receiver);
messageHelper.setFrom(sender);
messageHelper.setSubject("test subject");
messageHelper.setText(message);
}
};
this.javaMailSender.send(preparator);
}
}