I send mail using SimpleMailMessage from org.springframework.mail package.But on the receiver side shows sender's address.I want to set his name.What method will use for this?Is there any method that available in SimpleMailMessage Class?I am using spring with hibernate.
code
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);
}
Use Spring's MimeMessageHelper to set sender name and address.
MimeMessagePreparator mailMessage = mimeMessage -> {
MimeMessageHelper message = new MimeMessageHelper(
mimeMessage, true, "UTF-8");
message.setFrom(senderEmail, senderName);
[...]
};
mailSender.send(mailMessage);
See also my code snippet in this answer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With