gpt4 book ai didi

java - org.springframework.mail.MailSendException : Failed messages: java. lang.NullPointerException

转载 作者:太空宇宙 更新时间:2023-11-04 12:05:20 26 4
gpt4 key购买 nike

我在尝试使用 Spring Boot 电子邮件功能时遇到下面提到的异常:

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", "test");

最佳答案

我的邮件服务类别不正确,这是正确的:

@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);
}
}

关于java - org.springframework.mail.MailSendException : Failed messages: java. lang.NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40422469/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com