1
我得到下面提到的異常,而試圖採用彈簧啓動電子郵件功能:org.springframework.mail.MailSendException:失敗的信息:顯示java.lang.NullPointerException
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( 「[email protected]」, 「[email protected]」 , 「測試一下」);
請加滿的堆棧跟蹤(或原因行號部分) –
感謝,但我已經發現了這個問題。 –