i have a mailer class that works fine with IE when i run the application locally, but after deploying it on a server it keeps sending gobbledygook and unreadable characters, i dont see where the problem is, everything is utf8, here is my code :
public static void sendHTMLEmail(String to, String subject, String body)
throws EmailException {
HtmlEmail email = new HtmlEmail();
email.setSmtpPort(25);
email.setAuthenticator(new DefaultAuthenticator("myMail","myPass"));
email.setDebug(false);
email.setHostName("smtp.gmail.com");
email.setFrom("webmail@mysite.com","Webmail@mysite");
email.setCharset("UTF-8");
email.setSubject(subject);
// --set Body--
String HTMLBody ="<html xmlns='http://www.w3.org/1999/xhtml'>";
HTMLBody += "<head><meta http-equiv='Content-Type' content='text/html; charset=utf-8' /></head>";
HTMLBody += "<body><div dir='rtl'>";
HTMLBody += body;
HTMLBody += "</div></body></html>";
// -----------
email.setHtmlMsg(HTMLBody);
email.setTextMsg("Your email client does not support HTML messages");
email.addTo(to);
email.setTLS(true);
email.send();
}
and here are my libraries :
import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailException;
import org.apache开发者_StackOverflow社区.commons.mail.HtmlEmail;
import org.apache.commons.mail.SimpleEmail;
thnx for your time
I'll assume that the String body
method argument is actually the user-supplied data which has been entered in some <input>
or <textarea>
and submitted by a <form method="post">
in a JSP page.
The data will be submitted using the charset as is been specified in the Content-Type
header of the page containing the form. If the charset is absent in the Content-Type
header, then the browser will simply make a best guess and MSIE is generally not that smart as others, it'll just grab the client platform default encoding.
You need to ensure of the following three things to get it all straight:
Ensure that the HTTP response containing the
<form>
is been sent withcharset=UTF-8
in theContent-Type
header. You can achieve this by adding the following line to the top of the JSP responsible for generating the response:<%@page pageEncoding="UTF-8" %>
This not only sets the response encoding to UTF-8, but also implicitly sets the
Content-Type
header totext/html;charset=UTF-8
.Ensure that the servlet which processes the form submit processes the input data in the obtained HTTP request with the same character encoding. You can achieve this by adding the following line before you get any information from the request, such as
getParameter()
.request.setCharacterEncoding("UTF-8");
A more convenient way would be to drop that line in some
Filter
which is been mapped on an URL pattern of interest, so that you don't need to copypaste the line over all servlets.Ensure that you do not use the
accept-charset
attribute of the<form>
. MSIE has serious bugs with this.
精彩评论