Solved

Send UTF-8 Email "apache commons"

Posted on 2011-09-29
8
291 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
Comment Utility
>>The email is not encoded properly

In what way?
0
 

Author Comment

by:Web_Admin
Comment Utility
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
Comment Utility
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
Comment Utility
btw, it looks like your template is still in template form somewhat (as opposed to having had its placeholders filled out)
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:Web_Admin
Comment Utility
@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
Comment Utility
CEHJ: Yes I was testing and send the template as it is and it has arabic content too
0
 

Author Comment

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

Expert Comment

by:CEHJ
Comment Utility
:)
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

771 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

12 Experts available now in Live!

Get 1:1 Help Now