Java Spring启动电子邮件发送及附件

Java Spring启动电子邮件发送及附件,java,spring,spring-boot,spring-mvc,email-attachments,Java,Spring,Spring Boot,Spring Mvc,Email Attachments,出现意外错误(类型=内部服务器错误,状态=500)。 未能将类型为“org.springframework.web.multipart.support.StandardMultipartTTpServletRequest$StandardMultipartFile”的值转换为所需类型“org.springframework.web.multipart.commons.CommonsMultipartFile”;嵌套异常为java.lang.IllegalStateException:无法将类型为

出现意外错误(类型=内部服务器错误,状态=500)。 未能将类型为“org.springframework.web.multipart.support.StandardMultipartTTpServletRequest$StandardMultipartFile”的值转换为所需类型“org.springframework.web.multipart.commons.CommonsMultipartFile”;嵌套异常为java.lang.IllegalStateException:无法将类型为“org.springframework.web.multipart.support.StandardMultipartttpServletRequest$StandardMultipartFile”的值转换为所需类型“org.springframework.web.multipart.commons.CommonsMultipartFile”:找不到匹配的编辑器或转换策略

我的jsp页面


My 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>

公共类联系人{
@NotNull(message=“Name不能为空。”)
私有字符串名称;
@NotNull(message=“Name不能为空。”)
私人电话;
@NotNull(message=“电子邮件不能为空。”)
私人字符串电子邮件;
@NotNull(message=“Subject不能为空。”)
私有字符串主题;
@NotNull(message=“注释不能为空。”)
私有字符串注释;
私有多部分文件附件;
//吸气剂
//二传手
}
My 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>
@RequestMapping(value=“/sendAttachmentEmail”,consumes=“multipart/form data”,method=RequestMethod.POST) public model和view sendmailwithattachment(HttpServletRequest请求,final@RequestParam(“附件”)commonmultipartfile attachFile)抛出消息异常{ 试一试{ ModelAndView mav=新ModelAndView(“成功”); log.info(“Spring启动-发送附件电子邮件…”); //读取表单输入 最终字符串email=request.getParameter(“mailTo”); 最终字符串phone=request.getParameter(“phone”); 最终字符串名称=request.getParameter(“名称”); 最终字符串subject=request.getParameter(“subject”); 最终字符串注释=request.getParameter(“注释”); 日志信息(姓名+电话+电子邮件+主题+评论); 如果((attachFile!=null)&&(attachFile.getSize()>0)&(!attachFile.equals(“”)){ log.info(“文件名===”+attachFile.getOriginalFilename()); }否则{ log.info(“文件名===”+attachFile.getOriginalFilename()+“”+attachFile); } 触点=新触点(); contact.setName(name); 联系人:setPhone(电话); 联系人:setEmail(email); 联系人:setSubject(主题); contact.setComment(comment); mav.addObject(“name”,contact.getName()); log.info(“Sening附件电子邮件…”); emailService.sendAttachmentEmail(联系人,附件文件); log.info(“完成…”); 返回mav; }捕获(例外e){ log.error(例如getMessage()); 返回新模型和视图(“附件电子邮件”); } }
My EmailServiceImpl.java


@凌驾
public void sendAttachmentEmail(联系人,CommonMultipartFile attachfile)引发MessaginException{
emailSender.send(新的MIMessagePreparator(){
public void prepare(MimeMessage MimeMessage)引发异常{
MimeMessage message=emailSender.createMimeMessage();
mimessagehelper=new mimessagehelper(message,true,“UTF-8”);//启用多部分标志!
String content=“嗨,”+contact.getName()+“感谢您联系我们。PFB附件。
”; setSubject(contact.getSubject()); setText(内容+“注释:”+contact.getComment(),true); setTo(contact.getEmail()); setFrom(env.getProperty(“spring.mail.username”); //确定是否有文件上载。如果有,请将其附加到客户端电子邮件 如果((attachfile!=null)&&(attachfile.getSize()>0)&(!attachfile.equals(“”)){ System.out.println(“\n附件名称?=”+attachfile.getOriginalFilename()+“\n”); helper.addAttachment(attachfile.getOriginalFilename(),新InputStreamSource(){ 公共InputStream getInputStream()引发IOException{ 返回attachfile.getInputStream(); } }); }否则{ System.out.println(“用户未选择任何附件。正在发送文本电子邮件”); } } }); }
提交按钮后,我收到上述错误,请提供帮助。

关键在于:无法将类型“org.springframework.web.multipart.support.StandardMultipartTTpServletRequest$StandardMultipartFile”的值转换为所需类型“org.springframework.web.multipart.commons.CommonsMultipartFile”:找不到匹配的编辑器或转换策略

换句话说,尝试更改控制器方法的签名
sendmailwithatachment

public ModelAndView sendEmailWithAttachment(HttpServletRequest request, final @RequestParam("attachment") StandardMultipartFile attachFile)

在控制器方法中使用
org.springframework.web.multipart.MultipartFile
而不是commonmultipartfile您好,正如您所建议的,我在控制器方法中使用org.springframework.web.multipart.MultipartFile而不是commonmultipartfile,但现在我遇到了这个错误“失败的邮件:javax.mail.SendFailedException:没有收件人地址”,我交叉检查了我的电子邮件地址,现在它是正确的。如何解决此错误?检查contact.getEmail()中的实际内容。…我假设它必须为空?实际上…而不是使用
MimeMessage message=emailSender.createMimeMessage())
。尝试使用在prepare方法中传递的mimeMessage…
mimeMessage mimeMessage
是的,非常感谢,先生…您好,我在春季没有得到标准的multipartfile类。您能告诉我它属于哪个包吗?我在我的一个项目中发现了类似的控制器。它可以工作。签名看起来像:public@RResponseBody响应上载(@RequestParam(“file”)MultipartFile文件,HttpServletRequest request)。MultipartFile是org.springframework.web.multipart.MultipartFile
<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>
public ModelAndView sendEmailWithAttachment(HttpServletRequest request, final @RequestParam("attachment") StandardMultipartFile attachFile)