gpt4 book ai didi

java - Spring Boot带附件的邮件发送

转载 作者:行者123 更新时间:2023-12-01 23:20:41 25 4
gpt4 key购买 nike

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.support.StandardMultipartHttpServletRequest$StandardMultipartFile”的值转换为所需类型“org.springframework.web.multipart.commons.CommonsMultipartFile”:没有匹配找到编辑器或转换策略

换句话说,尝试更改 Controller 方法 sendEmailWithAtachment 的签名:

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

关于java - Spring Boot带附件的邮件发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58329314/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com