1、当邮件内容是HTML语言时的中文问题
初始化MimeMessageHelper类的时候,设置为“UTF-8”.
JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
MimeMessage mailMessage = senderImpl.createMimeMessage();
MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage, true, "utf-8");
spring的文档里面说MimeMessageHelper设置了encoding,同时对title,text,attach产生作用,如果还是会出现乱码问题,解决办法为:
File file = new File(path);
messageHelper.addAttachment(MimeUtility.encodeWord(file.getName()), file);
项目开发代码:发送邮件实例:
JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
senderImpl.setHost(mailSenderInfo.getHost());// 邮件发送服务器
senderImpl.setUsername(mailSenderInfo.getUserName());
senderImpl.setPassword(mailSenderInfo.getPassword());
Properties prop = new Properties();
prop.put("mail.smtp.auth", mailSenderInfo.getSmtpAuth());// 参数设为true,需服务器进行认证,认证用户名和密码是否正确
prop.put("mail.smtp.timeout",mailSenderInfo.getSmtpTimeout());
senderImpl.setJavaMailProperties(prop);
MimeMessage mailMessage = senderImpl.createMimeMessage();
MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage, true, "utf-8");
messageHelper.setTo(mailSenderInfo.getMailTo());// 收件人
messageHelper.setFrom(mailSenderInfo.getMailFrom());// 发件人
messageHelper.setCc(mailSenderInfo.getMailCc());// 邮件抄送
messageHelper.setSubject(mailSenderInfo.getMailSubject());// 邮件主题
messageHelper.setText(mailSenderInfo.getMailContent(), true);// 邮件内容
if (mailSenderInfo.getMailAttachments() != null) {
for (String key : mailSenderInfo.getMailAttachments().keySet()) {
String path = mailSenderInfo.getMailAttachments().get(key);
File file = new File(path);
messageHelper.addAttachment(MimeUtility.encodeWord(key), file);
}
}
//图片问题暂时不贴了
senderImpl.send(mailMessage);