Solved

Send UTF-8 Email "apache commons"

Posted on 2011-09-29
8
309 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
null output 3 35
How do I remove an object from a 3 40
Java syntax, or is it Selenium 6 30
Need Help! Getting a syntax error and don't understand why 3 31
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

792 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