我从org.springframework.mail包中使用SimpleMailMessage发送邮件,但是在接收方显示发件人的地址,我想设置他的名字,这将使用什么方法?SimpleMailMessage类中是否有可用的方法?我正在使用spring与冬眠。
码
public void sendmail(){
String emailSubject = "Test Message";
String emailBody = "For testing";
SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
simpleMailMessage.setTo("[email protected]");
simpleMailMessage.setCc("[email protected]");
simpleMailMessage.setSubject(emailSubject);
simpleMailMessage.setText(emailBody);
mailSender.send(simpleMailMessage);
}
最佳答案
使用Spring的MimeMessageHelper设置发件人名称和地址。
MimeMessagePreparator mailMessage = mimeMessage -> {
MimeMessageHelper message = new MimeMessageHelper(
mimeMessage, true, "UTF-8");
message.setFrom(senderEmail, senderName);
[...]
};
mailSender.send(mailMessage);
另请参阅我的代码段in this answer。