[英]How to convert the content of email message in html format in JSP, Servlet aplication?
我正在從事一個需要發送 email 的項目。 電子郵件已經發送,但我需要實現更專業的格式,根據我的研究,我可以將 HTML 格式實現為 email。 這是必要的,因為我必須放置與項目公司(形象公司)相關的信息。 我嘗試使用 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);
}
}
小服務程序代碼:
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 email 時想要的。
解決方案只是刪除對msg.setText()
的調用。 然后您將發送 HTML email。 當然,您還需要為應用程序的 email 修改傳遞給msg.setContent()
的消息內容,但這只是一個實現細節。
有關setContent()
和setText()
的更多信息,請參閱javax.mail.Part
接口的 Javadoc,該接口由 class javax.mail.Message
實現。
One other related point is that it looks like your EmailServicio.enviarEmail()
method is almost a direct copy of the main()
method of class SendHTMLEmail
in the tutorial " JavaMail API - Sending an HTML Email ", apart from the call to setText()
你添加的。
值得驗證的是,您可以先成功運行他們的簡單 Java 應用程序的實現。 如果有任何問題需要解決,調試 Java 應用程序要比 servlet 容易得多。 一旦你有 HTML email 應用程序工作,你就可以將你的工作代碼移植到 web 應用程序。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.