aspose file tools*
The moose likes Other JSE/JEE APIs and the fly likes Java Mail Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "Java Mail" Watch "Java Mail" New topic

Java Mail

Monika Sach

Joined: Mar 07, 2007
Posts: 2

I am trying to send emails with 1)plain text and 2)html .

My problem is while sending html emails if I have some special character say (ź , this character gets screwed up. This character is what the user inputs in a jsp, so is generated dynamically. With plain text its fine.

I use the below code for plain text it:
Properties props = System.getProperties();

// -- Attaching to default Session, or we could start a new one --

props.put("", smtpServer);
Session session = Session.getDefaultInstance(props, null);

// -- Create a new message --
Message msg = new MimeMessage(session);

// -- Set the FROM and TO fields --
msg.setFrom(new InternetAddress(from));
InternetAddress.parse(to, false));

// -- We could include CC recipients too --
// if (cc != null)
// msg.setRecipients(Message.RecipientType.CC
// ,InternetAddress.parse(cc, false));

// -- Set the subject and body text --

// -- Send the message --

For HTML I just comment

and instead of
I use
msg.setDataHandler(new DataHandler(new ByteArrayDataSource(strMessageBody,"text/html")));

Am I doing something wrong for HTML? Whats the correct way for it

Kai Witte
Ranch Hand

Joined: Jul 17, 2004
Posts: 354

you could change the content type of the HTML email, or you could replace all such characters with the "and double-cross number semicolon" notation.


Kai Witte's business website Kai Witte's private homepage Mock exam / preparation kit reviews
Paul Clapham

Joined: Oct 14, 2005
Posts: 18150

There are two steps in your process:

1. Get data from HTML text box into Java program.
2. Send data via e-mail.

You are trying to test both of these steps in a single test. That makes things harder than they need to be. Test them separately.
Neha Deshmukh
Ranch Hand

Joined: Apr 04, 2007
Posts: 30
I am facing similar kind of problem.
In my application i am sending mail through application in which subject as well as content of the mail contains '�'( this special character), to outlook express.
In outlook express I got garbage value in subject line while the content shows proper �.
So can you please help me solving this problem ASAP?
Paul Sturrock

Joined: Apr 14, 2004
Posts: 10336

Have you set the content type for your text? Notice that MimeMessage has mehtods setSubject(String, String) and setText(String, String). Pass UTF-8 as the second parameter and you should be OK.

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
haifa tl

Joined: Apr 14, 2007
Posts: 3

I am looking for help : I developed a java code for sending an automatic email to users when they register.

My code is working with but not with yahoo !!
can any body help me to fix the cause ?

here is my code

public void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {

PrintWriter out = response.getWriter();

* just for debug

//String host = "";
String d_email = "",
d_password = "pwd",
d_host = "",
d_port = "465",
m_to = "",
m_subject = "Registration Confirmation ",
m_text = "You have been registered -- If you want to receive updates via email or sms please Login";

Properties props = new Properties();
props.put("mail.smtp.user", "my-email");
props.put("", d_host);
props.put("mail.smtp.port", d_port);
props.put("mail.smtp.auth", "true");
//props.put("mail.smtp.debug", "true");
props.put("mail.smtp.socketFactory.port", d_port);
props.put("mail.smtp.socketFactory.class", "");
props.put("mail.smtp.socketFactory.fallback", "false");

SecurityManager security = System.getSecurityManager();

Authenticator auth = new SMTPAuthenticator();
Session session = Session.getInstance(props, auth);

MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(d_email));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(m_to));
catch (Exception mex)
private class SMTPAuthenticator extends javax.mail.Authenticator
public PasswordAuthentication getPasswordAuthentication()
return new PasswordAuthentication("", "pwd");

static public String stack2string(Exception e) {
try {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
return "------\r\n" + sw.toString() + "------\r\n";
catch(Exception e2) {
return "bad stack2string";

And I am sure about mu username and password but when I run I always have this error:

Sending failed;
nested exception is:
class javax.mail.AuthenticationFailedException

please help
I agree. Here's the link:
subject: Java Mail
Similar Threads
Java Mail API not connecting me from my tomcat server
JavaMail - 554 Relay rejected for policy reasons.
use Linux sendmail from Java
How to send email in "HTML" using SMTPMessage class ???
javax.mail.internet.AddressException: Missing local name in string