Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Mail

 
Monika Sach
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

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("mail.smtp.host", smtpServer);
props.put("mail.mime.charset","UTF-8");
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));
msg.setRecipients(Message.RecipientType.TO,
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 --
msg.setSubject(subject);
msg.setText(body);


// -- Send the message --
Transport.send(msg);

For HTML I just comment
props.put("mail.mime.charset","UTF-8");

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

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

Thanks
M
 
Kai Witte
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello,

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
 
Paul Clapham
Sheriff
Posts: 21107
32
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.
 
Neha Deshmukh
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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
Bartender
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.
 
haifa tl
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

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 smtp.gmail.com 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 {

response.setContentType("text/plain");
PrintWriter out = response.getWriter();

/**
* just for debug
*/

//String host = "smtp.jc.ae/email";
String d_email = "my-email@yahoo.com",
d_password = "pwd",
d_host = "smtp.mail.yahoo.com",
d_port = "465",
m_to = "email@yahoo.com",
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("mail.smtp.host", d_host);
props.put("mail.smtp.port", d_port);
props.put("mail.smtp.starttls.enable","true");
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", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");

SecurityManager security = System.getSecurityManager();

try
{
Authenticator auth = new SMTPAuthenticator();
Session session = Session.getInstance(props, auth);
//session.setDebug(true);

MimeMessage msg = new MimeMessage(session);
msg.setText(m_text);
msg.setSubject(m_subject);
msg.setFrom(new InternetAddress(d_email));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(m_to));
Transport.send(msg);
}
catch (Exception mex)
{
mex.printStackTrace();
out.println(mex.getMessage());
out.println(stack2string(mex));
}
}
private class SMTPAuthenticator extends javax.mail.Authenticator
{
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication("my-email@yahoo.com", "pwd");
}
}

static public String stack2string(Exception e) {
try {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic