Solved

Send UTF-8 Email "apache commons"

Posted on 2011-09-29
8
332 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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
 

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
The viewer will learn how to implement Singleton Design Pattern in Java.
This video teaches viewers about errors in exception handling.

689 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