sitijaafar
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?
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);
try including a separate Part in the email containing plain text
ASKER
I'm not really clear.
ASKER
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>
<html>
<body>
ITEM A</br>
ITEM B</br>
ITEM C</br>
</body>
</html>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
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);
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Do you means for html set it as
htmlPart.setContent(messag eTxt, "text/html");
and for non-html
plainPart.setContent(plain Txt, "text/plain");
htmlPart.setContent(messag
and for non-html
plainPart.setContent(plain
yes
ASKER
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(plain Txt, "text/plain");
thats why I mentioned plainPart.setContent(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
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
ASKER
Right now I understand that for html use ---> htmlPart.setContent(messag eTxt, "text/html");
for plain text use ---> plainPart.setContent(plain Txt, "text/plain");
In the application the sender can insert the content anything So, based on the content it will go wheter (htmlPart.setContent(messa geTxt, "text/html");) or (plainPart.setContent(plai nTxt, "text/plain"); )
for example
[1] -- if(content plaintext) {(plainPart.setContent(pla inTxt, "text/plain"); )}
[2] -- if(content html) {(htmlPart.setContent(mess ageTxt, "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.
for plain text use ---> plainPart.setContent(plain
In the application the sender can insert the content anything So, based on the content it will go wheter (htmlPart.setContent(messa
for example
[1] -- if(content plaintext) {(plainPart.setContent(pla
[2] -- if(content html) {(htmlPart.setContent(mess
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>>and another small suggestion
may be small but critical in most cases if it is intended for wide audiances!
may be small but critical in most cases if it is intended for wide audiances!
ASKER
yup, I'm already done for multipart. That means, it not possible for handle both plaintext and html.
ASKER
hurmm, but I must do for both.
ASKER
I got an idea, so make the condition based on the content structure.
ASKER
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);
}
}
something like that should work, not foolproof of course
ASKER
Good feedbacks