gpt4 book ai didi

java - 如何在JSP、Servlet应用程序中将电子邮件内容转换为html格式?

转载 作者:行者123 更新时间:2023-12-01 21:43:58 26 4
gpt4 key购买 nike

我正在从事一个需要发送电子邮件的项目。电子邮件已经发送,但我需要实现更专业的格式,并且根据我的研究,我可以将 HTML 格式实现为电子邮件。这是必要的,因为我必须输入与项目公司相关的信息(图片公司的)。我尝试使用 msg.SendContent 但它对我不起作用。希望您能指导我。

我将 NetBeans 与 javax.mail 库结合使用:

public class EmailServicio {

public static void enviarEmail(String host, String port,
final String user, final String pass, String destinatario,
String asunto, String mensaje) throws AddressException,
MessagingException {

// sets SMTP server properties
Properties properties = new Properties();
properties.put("mail.smtp.host", host);
properties.put("mail.smtp.port", port);
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "true");

// creates a new session with an authenticator
Authenticator auth = new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, pass);
}
};

Session session = Session.getInstance(properties, auth);

// creates a new e-mail message
Message msg = new MimeMessage(session);

msg.setFrom(new InternetAddress(user));
InternetAddress[] toAddresses = {new InternetAddress(destinatario)};
msg.setRecipients(Message.RecipientType.TO, toAddresses);
msg.setSubject(asunto);
msg.setContent("<h1>Maipo Grande, lider en exportación</h1>", "text/html");
msg.setSentDate(new Date());
msg.setText(mensaje);

// sends the e-mail
Transport.send(msg);
}
}

Servlet 代码:

public class ServletContacto extends HttpServlet {

private String host;
private String port;
private String user;
private String pass;

public void init() {
// reads SMTP server setting from web.xml file
ServletContext context = getServletContext();
host = context.getInitParameter("host");
port = context.getInitParameter("port");
user = context.getInitParameter("user");
pass = context.getInitParameter("pass");
}

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
UsuarioServicio usua = new UsuarioServicio();
String url = request.getRequestURI();

if ("/maipoGrande/Contacto".equals(url)) {
request.setAttribute("titulo", "Formulario Contacto");
HttpSession session = request.getSession(true);
if (session.getAttribute("usuario") == null) {
response.sendRedirect(request.getContextPath() + "/Login");
} else {
getServletContext().getRequestDispatcher("/contacto.jsp").forward(request, response);
}
}
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
String url = request.getRequestURI();
if ("/maipoGrande/Contacto".equals(url)) {
String destinatario = "atencion.maipogrande@gmail.com";
String asunto = request.getParameter("txtAsunto");
String mensaje = request.getParameter("txtMensaje");
String mensajeRespuesta = "";
try {
EmailServicio.enviarEmail(host, port, user, pass, destinatario, asunto,
mensaje);
mensajeRespuesta = "Su correo fue enviado exitosamente";
} catch (Exception ex) {
ex.printStackTrace();
mensajeRespuesta = "Se ha encontrado un error: " + ex.getMessage();
} finally {
request.setAttribute("Mensaje", mensajeRespuesta);
getServletContext().getRequestDispatcher("/resultado.jsp").forward(
request, response);
}
}
}
}

我希望在发送的消息中显示h1(测试)。

最佳答案

虽然您没有具体说明您的问题是什么,但可能是您正在调用 msg.setText(mensaje);调用msg.setContent("<h1>Maipo Grande, lider en exportación</h1>", "text/html");后.

您调用msg.setContent()会将 MIME 类型设置为 "text/html",这正是您想要的。但随后调用msg.setText()会将 MIME 类型重置为“text/plain”,这不是您发送 HTML 电子邮件时想要的类型。

解决方案只是删除对 msg.setText() 的调用。然后您将发送一封 HTML 电子邮件。当然你还需要修改传递给msg.setContent()的消息内容对于您的应用程序的电子邮件,但这只是一个实现细节。

参见the Javadoc for the interface javax.mail.Part ,由类 javax.mail.Message 实现,了解更多信息setContent()setText() .

另一个相关点是它看起来像你的 EmailServicio.enviarEmail()方法几乎是直接复制main()类的方法SendHTMLEmail在教程“JavaMail API - Sending an HTML Email”中,除了调用setText()您添加的。

值得首先验证您是否可以成功运行其简单 Java 应用程序的实现。如果有任何问题需要解决,调试 Java 应用程序比调试 servlet 容易得多。一旦 HTML 电子邮件应用程序正常运行,您就可以将工作代码移植到 Web 应用程序。

关于java - 如何在JSP、Servlet应用程序中将电子邮件内容转换为html格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58783710/

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