Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Java Mail  RSS feed

Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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

Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.

Posts: 23697
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!