No MimeMessage content when sending a SimpleMailMessage

1

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.

    
asked by Lucas. D 25.05.2018 в 19:29
source

2 answers

2

After a while I was able to solve it with the track of @abrahamhs: D It turns out that when I sent the form by POST I was forgetting to use the @Valid tag and passing my Mail object there. So with my sendContactEmail method of my controller in the following way everything works

@RequestMapping(value="/contact", method = RequestMethod.POST)
    public String sendContactEmail(@Valid Mail email, Model model, RedirectAttributes ra) {

        try {
            email.setFrom(email.getFrom());
            email.setSubject(email.getSubject());
            email.setReplyTo(email.getReplyTo());
            email.setTo("[email protected]");
            email.setContactMessage(email.getContactMessage());
            emailService.sendSimpleMessage(email);

            ra.addFlashAttribute("success", 1);
            return "redirect:/contact";
        } catch (MailSendException e) {
            ra.addFlashAttribute("error", 1);
            LOG.error("ERROR SENDING EMAIL", e);
            return "redirect:/contact";
        }
    }

The rest of the code remains the same.

    
answered by 25.05.2018 / 22:59
source
0

I do not know spring very well. But I think your problem is in the controler, here:

Mail mail = new Mail();
mail.setFrom(mail.getFrom());
mail.setSubject(mail.getSubject());
mail.setReplyTo(mail.getReplyTo());
mail.setText(mail.getText());

mail it seems that it has to be injected as well and with the data that comes from the view, since when creating it with new it goes empty (a new object is created) and the error that comes out No MimeMessage content It's because you try to send an empty email. Possibly with this I inject it:

@autowired
Mail mail;

but since I do not know very well spring, I do not know if it also passes the values correctly.

    
answered by 25.05.2018 в 20:07