Meaningless Drivel is fun!*
The moose likes Other JSE/JEE APIs and the fly likes Java Mail working but putting in some junk characters Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "Java Mail working but putting in some junk characters" Watch "Java Mail working but putting in some junk characters" New topic
Author

Java Mail working but putting in some junk characters

Janelle Goldsmith
Greenhorn

Joined: Mar 20, 2003
Posts: 12
I am using JavaMail to send emails. These emails are in html.
The emails are received fine but the content is distorted and some junk characters are put in between the content.
The code that I use is -
Properties props = System.getProperties();
props.put("mail.smtp.host", BCAProperty.getProperty("SMTPHost"));
// Get session
Session session = Session.getInstance(props, null);
// Define message
MimeMessage message = new MimeMessage(session);
// message.setFrom(new InternetAddress(from));
message.setFrom(new InternetAddress("update@email.com"));
//message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(BCAProperty.getProperty("EmailAddress")));
message.setSubject("Failed Job List");
message.setContent(sBuffer.toString(), "text/html");
Here sBuffer has the html code which starts from -
<html>.....</html>
In the html code, there is a table and no. of rows are added to the table which depend upon the jobs failed.
The same code works fine for 4 rows. But the same code fails for 5+ rows.
When there are 5+ rows, the output that I see in my email is -
*****************************************************
Received: from machine (machine-name [ipaddress]) by exchange server with SMTP (Microsoft Exchange Internet Mail Service Version 5.5.2656.59)
id QQ2Z5NG1; Tue, 12 Aug 2003 03:33:42 +0200
Message-ID: <3733549.1060652020016.JavaMail.administrator@machine>
From: update@email.com
To: janelle.goldsmith@companyname.com
Subject: Failed Job List
Mime-Version: 1.0
Content-Type: text/html; charset=Cp1252
Content-Transfer-Encoding: quoted-printable
Following are the jobs that failed<html><table border=3D1><tr><td>Scheduler=
Name</td><td>Document Name</td><td>User Name</td><td>Frequency</td><td>Tim=
ing details</td><td>Job Failed At</td><td>Error</td></tr><tr><td>bcaMBO</td=
><td>Document1</td><td>deepali</td><td>Once with Overwrite</td><td>Once</td=
><td>4/15/2003 - 0:42PM</td><td></td></tr><tr><td>bcaMBO</td><td>MBOTest</t=
d><td>deepali</td><td>Hourly with Overwrite</td><td>Start Hour =3D 23 and t=
he end hour =3D 1 ,30minutes after the hour</td><td>4/16/2003 - 3:12PM</td>=
<td></td></tr><tr><td>bcaMBO</td><td>Document1</td><td>deepali</td><td>Dail=
y with Overwrite</td><td>Daily every Tuesday ,Wednesday , every 1 weeks at =
3:11PM</td><td>4/24/2003 - 11:18AM</td><td></td></tr><tr><td>bcaMBO</td><td=
>MBOTest</td><td>deepali</td><td>Hourly with Overwrite</td><td>Start Hour =
=3D 23 and the end hour =3D 1 ,30minutes after the hour</td><td>4/28/2003 -=
4:53PM</td><td></td></tr><tr><td>bcaMBO</td><td>MBOTest</td><td>deepali</t=
d><td>Hourly with Overwrite</td><td>Start Hour =3D 18 and the end hour =3D =
20 ,30minutes after the hour</td><td>4/29/2003 - 10:39AM</td><td></td></tr>=
<tr><td>bcaMBO</td><td>Document1</td><td>deepali</td><td>Weekly with Overwr=
ite</td><td>Every every 1 weeks at6:43PM</td><td>5/5/2003 - 9:11AM</td><td=
></td></tr></table></html>
*******************************************************8
Please let me know what could be wrong when the no. of rows are more than 4.
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
It could be due to the encoding. Most html pages use iso-8859-1 not cp-1252 try changing
message.setContent(sBuffer.toString(), "text/html");
To
message.setContent(new String(sBuffer.toString().getBytes(), "iso-8859-1"), "text/html; charset=\"iso-8859-1\"");
This throws a new checked exception : java.io.UnsupportedEncodingException so you need to declare it to be thrown or catch it.
iso-8859-1 is supported so, the exception will never be thrown unless something gets corrupted with your rt.jar.


I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
Janelle Goldsmith
Greenhorn

Joined: Mar 20, 2003
Posts: 12
Thanks Carl,
Works like magic.
Just one question -
I assumed that the default encoding would be iso-8859-1.
But the one being used was cp-1252. Do you think it is because the default encoding of our mail server might be cp-1252 ? Or the default set by my JavaMail API is cp-1252 ?
Thanks,
janelle
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
Unless you specify a charset, it uses the System default charset encoding. I haven't found an easy way to determine what a Systems default encoding is. Anyone out there know?
Richard Poon
Greenhorn

Joined: Jul 11, 2005
Posts: 1
Hi,

I found this old discussion topic, but I am having similar problem and hope someone can follow me ideas.

My company is trying OpManager, a java based network management program. We configured it to send SMTP alert to the respective monitoring person. After a month's testing, it started sending out alert mails with:
Content-Type: text/plain; charset=Cp1252
Content-Transfer-Encoding: quoted-printable

instead of:
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

As a result, the mail reader program (Outlook) gives out a message as follows and make the original message an attachment in the mail:

"This message uses a character set that is not supported by the Internet Service. To view the original message content, open the attached message. If the text doesn't display correctly, save the attachment to disk, and then open it using a viewer that can display the original character set."

We have no clue as was happens to the OpManager or our mail server. We checked that the mail delivery path is the same as before.

Thank you.
Richard Poon
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Java Mail working but putting in some junk characters
 
Similar Threads
Problem with my jsp compilation/Java mail
FileNotFoundException while sending Mail as attachment using javamail
Trying to send HTML email but coming through as text email
FileNotFoundException while sending mail with attachment using javamail
Able to send mail without host name