@Autowired
private JavaMailSenderImpl mailSender;
/**请求处理并发送email*/
@RequestMapping("/doRequestAndSendMail.shtml")
public String doRequestAndSendMail(ModelMap model,HttpServletRequest request,
HttpServletResponse response) throws Exception{
。。。。。
//发送邮件
String email = request.getParameter("email");
if(null != email )
if(!email.equals(""))
this.sendMail(email, delivery.getFeedbackPath(),request.getParameter("feedback"));
。。。。
}
/**发送邮件*/
private void sendMail(String email,String accessoryPath,String feedback){
JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
MimeMessage mailMessage = senderImpl.createMimeMessage();
try {
//设置utf-8或GBK编码,否则邮件会有乱码
MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage,true,"utf-8");
messageHelper.setTo(email);//接受者
// messageHelper.setFrom("");//发送者
messageHelper.setSubject("华为");//主题
//邮件内容,注意加参数true
/**如果没有填写反馈文字,则使用默认 */
if(null != feedback ){
if(!feedback.equals(""))
messageHelper.setText("<html><head></head><body><h1>"+feedback+"</h1></body>< ml>",true);
}
//邮件嵌入图片
//messageHelper.addInline("a", new File("E:/xiezi.jpg"));
//messageHelper.addInline("b", new File("E:/logo.png"));
//File file=new File("E:/测试中文文件.rar");
if(null != accessoryPath ){
if(!accessoryPath.equals("")){
System.out.println("发送的附件来自于:"+accessoryPath);
File file=new File(accessoryPath);
// 这里的方法调用和插入图片是不同的,使用MimeUtility.encodeWord()来解决附件名称的中文问题
messageHelper.addAttachment(MimeUtility.encodeWord(file.getName()), file);
}
}
mailSender.send(mailMessage);
} catch (MessagingException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}