【发布时间】:2019-10-10 18:47:54
【问题描述】:
Technology: Spring Boot
Hello Developers, Can sombody tell me what is wrong with my code?
Why i'm unable to send attachment in email?
Exception:
出现意外错误(类型=内部服务器错误,状态=500)。 无法将类型“org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile”的值转换为所需类型“org.springframework.web.multipart.commons.CommonsMultipartFile”;嵌套异常是 java.lang.IllegalStateException:无法将类型“org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile”的值转换为所需类型“org.springframework.web.multipart.commons.CommonsMultipartFile”:没有匹配的编辑器或找到转化策略
我的jsp页面
<pre>
<form:form modelAttribute="attachmentEmail" method="POST" action="/email-app/sendAttachmentEmail" cssClass="register-form"
id="register-form" enctype="multipart/form-data">
<div class="form-group">
<label for="fname"><i class="zmdi zmdi-account material-icons-name"></i></label>
<form:input path="name" name="name" id="name" placeholder="Name" required="required" />
<form:errors path="name" cssStyle="color:red;" cssClass="error" />
</div>
<div class="form-group">
<label for="phone"><i class="zmdi zmdi-phone"></i></label>
<form:input path="phone" name="phone" id="phone" placeholder="Phone" required="required" />
<form:errors path="phone" cssStyle="color:red;" cssClass="error" />
</div>
<div class="form-group">
<label for="email"><i class="zmdi zmdi-email"></i></label>
<form:input type="email" path="email" name="email" id="email" placeholder="Email" required="required" />
<form:errors path="email" cssStyle="color:red;" cssClass="error" />
</div>
<div class="form-group">
<label for="subject"><i class="zmdi zmdi-account material-icons-name"></i></label>
<form:input path="subject" name="subject" id="subject" placeholder="Subject" required="required" />
<form:errors path="subject" cssStyle="color:red;" cssClass="error" />
</div>
<div class="form-group">
<form:textarea path="comment" name="comment" id="comment" placeholder="Comment" rows="5" cols="35" required="required" />
<form:errors path="comment" cssStyle="color:red;" cssClass="error" />
</div>
<div class="form-group">
<label for="attachment"><i class="zmdi zmdi-file"></i></label>
<form:input path="attachment" type="file" name="attachment" id="attachment" required="required" />
<form:errors path="attachment" cssStyle="color:red;" cssClass="error" />
</div>
<div class="form-group form-button">
<input type="submit" id="signup" class="form-submit" value="Send" />
</div>
</form:form>
</pre>
我的 Contact.java 和 EmailController.java
<pre>
public class Contact {
@NotNull(message = "Name can't be blank.")
private String name;
@NotNull(message = "Name can't be blank.")
private String phone;
@NotNull(message = "Email can't be blank.")
private String email;
@NotNull(message = "Subject can't be blank.")
private String subject;
@NotNull(message = "Comment can't be blank.")
private String comment;
private CommonsMultipartFile attachment;
//getters
//setters
}
</pre>
My EmailController.java
<pre>
@RequestMapping(value ="/sendAttachmentEmail", consumes = "multipart/form-data", method = RequestMethod.POST)
public ModelAndView sendEmailWithAttachment(HttpServletRequest request, final @RequestParam("attachment") CommonsMultipartFile attachFile) throws MessagingException {
try {
ModelAndView mav =new ModelAndView("success");
log.info("Spring Boot - Sending Attachment Email...");
// reads form input
final String email = request.getParameter("mailTo");
final String phone = request.getParameter("phone");
final String name = request.getParameter("name");
final String subject = request.getParameter("subject");
final String comment = request.getParameter("comment");
log.info(name+" "+phone+" "+email+" "+subject+" "+comment);
if ((attachFile != null) && (attachFile.getSize() > 0) && (!attachFile.equals(""))) {
log.info("FileName====="+attachFile.getOriginalFilename());
} else {
log.info("FileName====="+attachFile.getOriginalFilename()+" "+attachFile);
}
Contact contact = new Contact();
contact.setName(name);
contact.setPhone(phone);
contact.setEmail(email);
contact.setSubject(subject);
contact.setComment(comment);
mav.addObject("name", contact.getName());
log.info("Sening Attachment Email...");
emailService.sendAttachmentEmail(contact, attachFile);
log.info("Done...");
return mav;
} catch (Exception e) {
log.error(e.getMessage());
return new ModelAndView("attachment-email");
}
}
</pre>
我的 EmailServiceImpl.java
<pre>
@Override
public void sendAttachmentEmail(Contact contact, CommonsMultipartFile attachfile) throws MessagingException {
emailSender.send(new MimeMessagePreparator() {
public void prepare(MimeMessage mimeMessage) throws Exception {
MimeMessage message = emailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8"); // Enable the multipart flag!
String content = "Hi, <b>"+contact.getName()+"</b> Thank you for Contacting Us. PFB attachment.<br>";
helper.setSubject(contact.getSubject());
helper.setText(content+" <b>Comment:</b> "+ contact.getComment(), true);
helper.setTo(contact.getEmail());
helper.setFrom(env.getProperty("spring.mail.username"));
// Determine If There Is An File Upload. If Yes, Attach It To The Client Email
if ((attachfile != null) && (attachfile.getSize() > 0) && (!attachfile.equals(""))) {
System.out.println("\nAttachment Name?= " + attachfile.getOriginalFilename() + "\n");
helper.addAttachment(attachfile.getOriginalFilename(), new InputStreamSource() {
public InputStream getInputStream() throws IOException {
return attachfile.getInputStream();
}
});
} else {
System.out.println("No Attachment Is Selected By The User. Sending Text Email.");
}
}
});
}
</pre>
提交按钮后出现上述错误,请帮助。
【问题讨论】:
-
在你的控制器方法中使用
org.springframework.web.multipart.MultipartFile而不是 CommonsMultipartFile -
您好,正如您所建议的,我在控制器方法中使用 org.springframework.web.multipart.MultipartFile 而不是 CommonsMultipartFile 但现在我收到此错误“失败消息:javax.mail.SendFailedException:没有收件人地址”,我交叉检查了我的电子邮件地址,现在它是正确的,如何解决这个错误?
-
检查
contact.getEmail()中的实际内容...我假设它必须是空白的? -
其实...而不是使用
MimeMessage message = emailSender.createMimeMessage();..尝试使用在prepare方法中传递的mimeMessage....MimeMessage mimeMessage -
是的,非常感谢先生......
标签: java spring spring-boot spring-mvc email-attachments