File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/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
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "Java Mail" Watch "Java Mail" New topic
Author

Java Mail

Monika Sach
Greenhorn

Joined: Mar 07, 2007
Posts: 2
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

Joined: Jul 17, 2004
Posts: 356
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


Kai Witte's business website Kai Witte's private homepage
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18650
    
    8

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
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

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
Greenhorn

Joined: Apr 14, 2007
Posts: 3
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java Mail