如何使用javamail发送包含unicode字符的电子邮件?

nnsrf1az  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(291)

我正在为重置密码页编写代码(使用jsp/servlet)。这是我的密码。
emailutility类

  1. public class EmailUtility {
  2. public static void sendEmail(String host, String port, String socketFactoryClass, String auth,
  3. final String senderEmail, String senderName, final String password,
  4. String recipientEmail, String subject, String message) throws MessagingException, UnsupportedEncodingException {
  5. //Get properties object
  6. Properties props = new Properties();
  7. props.put("mail.smtp.host", host);
  8. props.put("mail.smtp.socketFactory.port", port);
  9. props.put("mail.smtp.socketFactory.class", socketFactoryClass);
  10. props.put("mail.smtp.auth", auth);
  11. props.put("mail.smtp.port", port);
  12. //Get Session
  13. Authenticator authenticator = new javax.mail.Authenticator()
  14. {
  15. protected PasswordAuthentication getPasswordAuthentication() {
  16. return new PasswordAuthentication(senderEmail, password);
  17. }
  18. };
  19. Session session = Session.getDefaultInstance(props, authenticator);
  20. //Create a new e-mail message
  21. Message msg = new MimeMessage(session);
  22. msg.setHeader("Content-Type", "text/plain; charset=UTF-8");
  23. msg.setFrom(new InternetAddress(senderEmail, senderName));
  24. msg.addRecipient(Message.RecipientType.TO, new InternetAddress(recipientEmail));
  25. msg.setSubject(subject);
  26. msg.setSentDate(new Date());
  27. msg.setText(message);
  28. //Send the e-mail
  29. Transport.send(msg);
  30. }
  31. }

我的servlet控制密码重置

  1. @WebServlet(name = "ResetPasswordController", urlPatterns = {"/ResetPassword"})
  2. public class ResetPasswordController extends HttpServlet {
  3. private static final long serialVersionUID = 1L;
  4. private String host;
  5. private String port;
  6. private String socketFactoryClass;
  7. private String auth;
  8. private String email;
  9. private String name;
  10. private String pass;
  11. public void init() {
  12. // reads SMTP server setting from web.xml file
  13. ServletContext context = getServletContext();
  14. host = context.getInitParameter("host");
  15. port = context.getInitParameter("port");
  16. socketFactoryClass = context.getInitParameter("socketFactoryClass");
  17. auth = context.getInitParameter("auth");
  18. email = context.getInitParameter("email");
  19. name = context.getInitParameter("name");
  20. pass = context.getInitParameter("pass");
  21. }
  22. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  23. String recipient = request.getParameter("reset-email");
  24. String subject = "Mật khẩu của bạn đã được đặt lại";
  25. CustomerServices customerServices = new CustomerServices();
  26. String newPassword = customerServices.resetCustomerPassword(recipient);
  27. String content = "Xin chào, đây là mật khẩu mới của bạn đã được hệ thống tạo ra ngẫu nhiên: " + newPassword;
  28. content += "\nChú ý: vì lí do bảo mật, bạn phải đổi mật khẩu ngay sau khi đăng nhập.";
  29. content += "\nĐội ngũ hỗ trợ UNIFOOD";
  30. String message = "";
  31. try
  32. {
  33. EmailUtility.sendEmail(host, port, socketFactoryClass, auth, email, name, pass,
  34. recipient, subject, content);
  35. message = "Mật khẩu của bạn đã thay đổi, hãy kiểm tra email của bạn!";
  36. }
  37. catch (Exception ex)
  38. {
  39. ex.printStackTrace();
  40. message = "Có lỗi xảy ra: " + ex.getMessage();
  41. }
  42. finally
  43. {
  44. request.setAttribute("message", message);
  45. request.getRequestDispatcher("/message.jsp").forward(request, response);
  46. }
  47. }
  48. protected void doGet (HttpServletRequest request, HttpServletResponse response) throws
  49. ServletException, IOException {
  50. String url = "/reset-password.jsp";
  51. request.getRequestDispatcher(url).forward(request, response);
  52. }
  53. }

我遇到了一个问题,所有通过这种方式发送的电子邮件不能包含所有unicode字符。某些字符被替换为 ? ,正如您在这张图片中看到的,emailsent(1)
在上面的emailutility类中,我尝试使用以下命令 msg.setHeader("Content-Type", "text/plain; charset=UTF-8"); 按此页上一篇文章的建议修复此错误,但它不起作用。我也试过用 setText() 有两个论点 setText(message, "UTF-8") 但是没有 setText() 接受2个参数的函数。
然而,奇怪的是,当我写 main() emailutility类中的函数类似于 sendEmail() 函数并在控制台中运行代码,

  1. public static void main(String[] args) throws MessagingException, UnsupportedEncodingException {
  2. String host = "smtp.gmail.com";
  3. String port = "465";
  4. String socketFactoryClass = "javax.net.ssl.SSLSocketFactory";
  5. String auth = "true";
  6. String senderEmail = "mySenderEmail";
  7. String password = "myPassword";
  8. String senderName = "UNIFOOD-SUPPORT";
  9. String recipientEmail = "myRecipientEmail";
  10. String subject = "Mật khẩu của bạn đã được đặt lại";
  11. String message = "Xin chào, đây là mật khẩu mới của bạn đã được hệ thống tạo ra ngẫu nhiên: ";
  12. message += "\nVì lí do bảo mật, bạn phải đổi mật khẩu ngay sau khi đăng nhập.";
  13. message += "\nĐội ngũ hỗ trợ UNIFOOD";
  14. //Get properties object
  15. Properties props = new Properties();
  16. props.put("mail.smtp.host", host);
  17. props.put("mail.smtp.socketFactory.port", port);
  18. props.put("mail.smtp.socketFactory.class", socketFactoryClass);
  19. props.put("mail.smtp.auth", auth);
  20. props.put("mail.smtp.port", port);
  21. //Get Session
  22. Authenticator authenticator = new javax.mail.Authenticator()
  23. {
  24. protected PasswordAuthentication getPasswordAuthentication() {
  25. return new PasswordAuthentication(senderEmail, password);
  26. }
  27. };
  28. Session session = Session.getDefaultInstance(props, authenticator);
  29. //Create a new e-mail message
  30. Message msg = new MimeMessage(session);
  31. msg.setHeader("Content-Type", "text/plain; charset=UTF-8");
  32. msg.setFrom(new InternetAddress(senderEmail, senderName));
  33. msg.addRecipient(Message.RecipientType.TO, new InternetAddress(recipientEmail));
  34. msg.setSubject(subject);
  35. msg.setSentDate(new Date());
  36. msg.setText(message);
  37. //Send the e-mail
  38. Transport.send(msg);
  39. System.out.println("Done");
  40. }

我收到的电子邮件没有遗漏任何unicode字符,正如您在这张图片emailsent(2)中看到的那样
请帮我修正这个错误,非常感谢!

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题