I am trying to send an email from my web app but I have a very strange error.
org.springframework.mail.MailSendException: Failed messages: javax.mail.MessagingException: IOException while sending message;
nested exception is: java.io.IOException: No MimeMessage content
This is my class Mail
public class Mail {
private String from;
private String subject;
private String replyTo;
private String text;
private String to;
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getReplyTo() {
return replyTo;
}
public void setReplyTo(String replyTo) {
this.replyTo = replyTo;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getTo() {
return to;
}
public void setTo(String to) {
this.to = to;
}
public Mail(String from, String subject, String replyTo, String text) {
this.from = from;
this.subject = subject;
this.replyTo = replyTo;
this.text = text;
}
public Mail() {
}
}
My EmailService
@Service
public class EmailService {
@Autowired
private JavaMailSender emailSender;
public void sendSimpleMessage(final Mail mail) {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(mail.getFrom());
message.setSubject(mail.getSubject());
message.setReplyTo(mail.getReplyTo());
message.setTo(mail.getTo());
message.setText(mail.getText());
emailSender.send(message);
}
}
My driver
@Controller
public class ContactController {
private static Logger LOG = LoggerFactory.getLogger(ContactController.class);
@Autowired
EmailService emailService;
@RequestMapping(value= "contact", method = RequestMethod.GET)
public ModelAndView contactPage() {
Mail mail = new Mail();
ModelAndView mav = new ModelAndView();
mav.setViewName(Constants.CONTACT);
mav.addObject("mail", mail);
mav.addObject(Constants.TITLE, "Contact");
return mav;
}
@RequestMapping(value="/contact", method = RequestMethod.POST)
public String sendContactEmail(Model model, RedirectAttributes ra) {
try {
Mail mail = new Mail();
mail.setFrom(mail.getFrom());
mail.setSubject(mail.getSubject());
mail.setReplyTo(mail.getReplyTo());
mail.setText(mail.getText());
mail.setTo("[email protected]");
emailService.sendSimpleMessage(mail);
LOG.info("Email Successfully Sended");
ra.addFlashAttribute("success", 1);
return "redirect:/contact";
} catch (MailSendException e) {
ra.addFlashAttribute("error", 1);
LOG.error("ERROR SENDING EMAIL", e);
return "redirect:/contact";
}
}
}
And my view
<form th:action="@{/contact}" th:object="${mail}" method="POST">
<div class="form-group row">
<label for="inputName"
class="col-sm-2 col-form-label text-right text-sm-center text">
<i class="fas fa-user"></i>
</label>
<div class="col-sm-10">
<input th:field="*{from}" type="text" class="form-control"
id="inputName" autocomplete="off" placeholder="Name">
</div>
</div>
<div class="form-group row">
<label for="inputName"
class="col-sm-2 col-form-label text-right text-sm-center"> <i
class="fas fa-pencil-alt"></i>
</label>
<div class="col-sm-10">
<input th:field="*{subject}" type="text" class="form-control"
id="inputName" autocomplete="off" placeholder="Subject">
</div>
</div>
<div class="form-group row">
<label for="inputEmail"
class="col-sm-2 col-form-label text-right text-sm-center"> <i
class="fas fa-at"></i>
</label>
<div class="col-sm-10">
<input th:field="*{replyTo}" type="email" class="form-control"
id="inputEmail" autocomplete="off" placeholder="Email">
</div>
</div>
<div class="form-group row">
<label for="inputMessage"
class="col-sm-2 col-form-label text-right text-sm-center"> <i
class="fas fa-envelope"></i>
</label>
<div class="col-sm-10">
<textarea th:field="*{text}" cols="30" rows="10" class="form-control"
id="inputMessage" placeholder="Message">
</textarea>
</div>
</div>
<button type="submit" class="btn btn-contact-7technology w-100">
<span>SEND MESSAGE</span>
</button>
</form>
Debugging the code I've found out that this line
mail.setText(mail.getText());
Always comes null, something very strange since I write a message in the Textearea. All the other fields arrive perfectly and if I setText my method String to force the mail arrives correctly with that String, but obviously I want the user to send an email with the message you want.
Best regards.