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 Mail class
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 controller
@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 sight
<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 have realized that this line
mail.setText(mail.getText());
null always comes, something very rare since I write a message in the Textearea. All the other fields arrive perfectly and if I forcefully pass a String to my setText method, the mail arrives correctly with that String, but obviously I want the user to send an email with the message they want.
Best regards.
after a while I was able to solve it with @abrahamhs hint :D It turns out that when submitting the form by POST I was forgetting to use the @Valid tag and to pass my Mail object through there. So with my sendContactEmail method of my controller as follows everything works
The rest of the code remains the same.
I don't know very well spring. But I think your problem is in the controller, here:
mail
It seems to me that it has to be injected as well and with the data that comes from the view, since when creating it withnew
va empty (a new object is created) and the error that you getNo MimeMessage content
is because you try to send an empty mail. Possibly with this I inject it:but since I don't know spring very well, I don't know if I also pass the values correctly.