Link to home
Start Free TrialLog in
Avatar of sitijaafar
sitijaafarFlag for Malaysia

asked on

How to differentiate between html content and non-html content of javamail?

Hi experts,
My application now can send an email. In that application sender can send both html format and non-html format. But right now I'm facing a problem when I want to send a non html format, for example the sender send an email the content like below:

The List

ITEM A
ITEM B
ITEM C

But, the recipient will receive it like this, it supposely like above :

The List ITEM A ITEM B ITEM C

It not receive like the original content.

Please help me..
The below code work for html format but look like not for non-html. What I should do, for differentiate between html and non-html when I want send the content?
To send content here is the code:
 
Multipart mp = new MimeMultipart();	 	   
MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(messageTxt, "text/html"); 			
mp.addBodyPart(htmlPart);				
msg.setContent(mp);

Open in new window

Avatar of Mick Barry
Mick Barry
Flag of Australia image

try including a separate Part in the email containing plain text
Avatar of sitijaafar

ASKER

I'm not really clear.
That's means for send non-html use a different code? But how to differentiate that the content is html or non-html, the content is from the jsp page.
if you are sending as HTML, you need to send the HTML source. for e.g.,

<html>

<body>
ITEM A</br>
ITEM B</br>
ITEM C</br>
</body>
</html>
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
not, ksivananth.  for content below I want send it as non-html.  

The List

ITEM A
ITEM B
ITEM C

Supposely, the app can send both of non-html and html. That means at one time sender can send either html and non-html.  But I think must do something for identify wheter the content is html or non-html. But, I do not how. Previously I use code below for send, it not work for html.




MimeBodyPart mbp = new MimeBodyPart();
mbp.setText(messageTxt);			
Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp);
msg.setContent(mp);

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Do you means for html set it as  
htmlPart.setContent(messageTxt, "text/html");                  

and for non-html
plainPart.setContent(plainTxt, "text/plain");

I will continue tomorrow, I need to go back. Thanks objects ...
>>not, ksivananth.  for content below I want send it as non-html.

thats why I mentioned plainPart.setContent(plainTxt, "text/plain");
ksivananth,

try reading this article.  hopefully, it will give you a better understanding of the differences in formatting content to be sent as HTML, as opposed to text only, emails.

http://www.webcheatsheet.com/PHP/send_email_text_html_attachment.php
Right now I understand that for html use ---> htmlPart.setContent(messageTxt, "text/html");    
for plain text use --->  plainPart.setContent(plainTxt, "text/plain");          

In the application the sender can insert the content anything So, based on the content it will go wheter (htmlPart.setContent(messageTxt, "text/html");) or (plainPart.setContent(plainTxt, "text/plain"); )

for example
[1] -- if(content plaintext) {(plainPart.setContent(plainTxt, "text/plain"); )}
[2] -- if(content html) {(htmlPart.setContent(messageTxt, "text/html");)}

That means:
If the content like below it will go to [1] condtion:

LIST
ITEM A
ITEM B
ITEM C

Meanwhile, when the content like below, it will go to [2] condition:
<html>

<body>
ITEM A</br>
ITEM B</br>
ITEM C</br>
</body>
</html>

but I do not have an idea how to make a condition, can anyone me give an idea how  to do that or is it can be solve with another solution.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>and another small suggestion

may be small but critical in most cases if it is intended for wide audiances!
yup, I'm already done for multipart. That means, it not possible for handle both plaintext and html.
hurmm, but I must do for both.
I got an idea, so make the condition based on the content structure.
here is what i did. Is it anyone have a better solution to solve this? I hope someone can help me, I'm quite new with java.
StringBuilder sb = new StringBuilder(body);
index1=sb.indexOf("<!DOCTYPE");
index2=sb.indexOf("<html");
index3=sb.indexOf("<body");
index4=sb.indexOf("</body>");
index5=sb.indexOf("</html>");
				    
if ((((index1 != -1)||(index2 != -1))&&(index3 != -1)&&(index4 != -1)&&(index5 != -1)) && (((index1 < index2)||(index2 < index3)||(index3 < index4)||(index4 < index5)))) {
 
String newMessage = insertSign(sb,index4);
				    				    MimeMultipart mp = new MimeMultipart("alternative");
MimeBodyPart text = new MimeBodyPart();
MimeBodyPart html = new MimeBodyPart();
						text.setText(newMessage);
						html.setContent(newMessage, "text/html");
mp.addBodyPart(text);
mp.addBodyPart(html);
msg.setContent(mp);
Transport.send(msg);
}
else {
messageTxt = body+"\n \n \n "+signature;
Multipart mp = new MimeMultipart();
MimeBodyPart plainPart = new MimeBodyPart()
plainPart.setContent(messageTxt, "text/plain");                   
mp.addBodyPart(plainPart);  
msg.setContent(mp);
Transport.send(msg);
				         }			          	    
}

Open in new window

something like that should work, not foolproof of course
Good feedbacks