Solved

Send UTF-8 Email "apache commons"

Posted on 2011-09-29
8
296 Views
Last Modified: 2012-05-12
Dear Experts,

I have this method that send an email that has arabic text html email using apache.
The email is not encoded properly

please this is urgent , it is in production ><

Thaaaaanks



public boolean sendEmail(Action email,Form form,Vector formData) throws SendFailedException,
	 MessagingException,javax.mail.internet.AddressException,Exception{


 
 boolean status=false;
 String subject = formatEmail(email.getParam1(),form,formData);
 String emailTemplate = formatEmail(email.getParam2(),form,formData);
 String from = formatEmail(email.getParam3(),form,formData);
 String to = formatEmail(email.getParam4(),form,formData);
 
  Config conf = new Config("uaeu_server_config.prop");
  String host= conf.get("mail_server"); //"195.229.162.3";
  int port =Integer.parseInt(conf.get("mail_port"));
  String[] recipients = to.split(",");
  InternetAddress[] addressTo = new javax.mail.internet.InternetAddress[recipients.length];
  for (int i = 0; i < recipients.length; i++){
	  System.out.println();
    addressTo[i] = new javax.mail.internet.InternetAddress(recipients[i]);}
  Collection toCollection = Arrays.asList(addressTo); 
  HtmlEmail htmlEmail = new HtmlEmail();
  htmlEmail.setHostName(host);
  htmlEmail.setTo(toCollection);
  htmlEmail.setFrom(from);
  htmlEmail.setSubject(subject);
  htmlEmail.setHtmlMsg((new StringBuilder()).append("<html><head><META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=UTF-8" + "\"></head><body>").append(emailTemplate).append("</body></html>").toString());
  //set the alternative message
 htmlEmail.setTextMsg("Your email client does not support HTML messages");
 //send the email
 htmlEmail.send();




return true;
 
 }

Open in new window

0
Comment
Question by:Web_Admin
  • 4
  • 4
8 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 36814460
>>The email is not encoded properly

In what way?
0
 

Author Comment

by:Web_Admin
ID: 36816705
see below text i copied from the email -.-
House Transfer Request
Personal Information 	المعلومات الشخصية
Banner ID : 	$BANNERID$ 	رقم الملف (البانر ) :
Requester Name: 	$REQUESTERNAME$ 	اسم مقدم الطلب :
College/Dept.: 	$DIVISIONNAME$& - $DEPARTMENTNAME$ 	كلية / إدارة :
Position: 	$POSITION$ 	الوظيفة :
Marital Status: 	$MARITALSTATUS$ 	الحالة الاجتماعيØ

Open in new window

0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 36817809
Could you please (preferably in your code temporarily) write 'emailTemplate' to a file and attach it to this post. Try not to touch it otherwise after doing so. Something like
Writer out = new OutputStreamWriter(new FileOutputStream("ee.txt"), "UTF-8");
out.write(emailTemplate);
out.close();

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36817823
btw, it looks like your template is still in template form somewhat (as opposed to having had its placeholders filled out)
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:Web_Admin
ID: 36817976
@CEHJ : I will try to write the template string to file , btw i printed the template string in the jsp before pass it to metod , in jsp it displays perfectly
0
 

Author Comment

by:Web_Admin
ID: 36898503
CEHJ: Yes I was testing and send the template as it is and it has arabic content too
0
 

Author Comment

by:Web_Admin
ID: 36898515
Thanks , it works now and I don't know the reason o.O
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36899087
:)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now