gpt4 book ai didi

java - 你如何发送带有中文字符的Javamail?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:06:06 25 4
gpt4 key购买 nike

我已经编写了 servlet 类和发送邮件类来发送 Javamail。英文消息没问题,但现在我也想发中文了。我假设您在 MimeMessage 的 setSubject 和 setText 方法中添加了 UTF-8。我这样做了,但收件箱中的所有汉字都已损坏。

电子邮件消息来自提交表单中的用户输入。

这是我的 servlet:

package com.lsp.web;

import com.lsp.service.Mailer;
import org.springframework.beans.factory.annotation.Autowired;

import javax.mail.MessagingException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "contact", urlPatterns = {"/contact"})
public class ContactServlet extends SpringInjectedServlet {
@Autowired
private Mailer mailer;

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.getRequestDispatcher("contact.jsp").forward(req, resp);
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String customerEmail = req.getParameter("email");
String subject = req.getParameter("subject");
String body = req.getParameter("message");
String name = req.getParameter("name");

body = "YOU HAVE A MESSAGE FROM " + name + "<" + customerEmail +">.\n" + body;

String error = null;
String succMess = null;

try {
javax.mail.internet.InternetAddress ia = new javax.mail.internet.InternetAddress(customerEmail);
ia.validate();
mailer.send(customerEmail, subject, body);
succMess = "你的消息已成功发送";
req.setAttribute("succMessage", succMess);
req.getRequestDispatcher("sent.jsp").forward(req, resp);

} catch (javax.mail.internet.AddressException ae) {
error = "您指出的邮箱地址不存在";
req.setAttribute("errorMessage", error);
req.getRequestDispatcher("contact.jsp").forward(req, resp);
}
catch (MessagingException mex) {
error = "发送失败";
req.setAttribute("errorMessage", error);
req.getRequestDispatcher("contact.jsp").forward(req, resp);
}
}
}

Mailer 类(发送邮件):

package com.lsp.service;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

@Component
public class Mailer {
private static final Logger log = Logger.getLogger(Mailer.class);

public Mailer() {
log.info("I'm newed.");
}

public void send(String custMail, String subject, String body) {
String from = "jshao745@gmail.com";
String to = "jshao745@gmail.com";

Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");


//body = "YOU HAVE A MESSEGE FROM " + custMail + "." + body;

Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("jshao745@gmail.com","SOMEPASSWORD");//change accordingly
}
});


try{
// Create a default MimeMessage object.
MimeMessage message = new MimeMessage(session);

// Set From: header field of the header.
message.setFrom(new InternetAddress(from));

// Set To: header field of the header.
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));

message.setHeader("Content-Type", "text/plain; charset=UTF-8");

// Set Subject: header field
message.setSubject(subject, "UTF-8");

// Send the actual HTML message, as big as you like
message.setText(body, "UTF-8");

// Send message
Transport.send(message);
System.out.println("Sent message successfully....");
}catch (MessagingException mex) {
mex.printStackTrace();
}
}
}

如果有人能帮助我,我将不胜感激。

谢谢。

最佳答案

首先,确保您的 Java String 对象包含正确的 Unicode 字符。如果您没有正确地进行输入字符转换,您最终可能会在 String 对象中产生垃圾,这当然意味着电子邮件中也会出现垃圾。

此外,您不需要显式设置 Content-Type header ,setText 方法会为您完成。

您可能还想阅读 common JavaMail mistakes 的列表.

关于java - 你如何发送带有中文字符的Javamail?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21296155/

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