org.apache.commons.mail attachment file name encoding - java

I'm using apache commons mail for sending e-mails with attachments.
My attachment file content is in hebrew and I can see it when I open the file , my problem is when the attachment file name is in hebrew I can't see the name I see ??? instead. (the content I still see o.k).
this is my code:
String attachment_file_name = "קובץ מס 1";
HtmlEmail email = new HtmlEmail();
email.setHostName(smtp_server);
email.addTo(to_email;
email.setFrom(from_email , "XXXXXXX");
email.setSubject(subject);
email.setCharset("UTF-8");
email.setHtmlMsg(body);
email.attach(new ByteArrayDataSource(attachment_file_.toByteArray(), "application/pdf"),
attachment_file_name ,
"attachment pdf",
EmailAttachment.ATTACHMENT);
email.send();
what do I need to do inorder to see the file name in hebrew (in the correct encoding) ?
Thank's In Advance.

I believe you have to encode it.
Javamail, the core library requires this

Set the System property "mail.mime.encodeparameters" to "true".

Related

How to fetch the conversation id from a .msg file using MAPIMessage

I'm trying to parse a .msg file. How do I get the conversation id?
I'm using org.apache.poi.hsmf.MAPIMessage
The structure of the MSG file format is described in the MSDN library, see [MS-OXMSG]: Outlook Item (.msg) File Format.
It is stored in the 0x0F030102 property - you can see if it is set in a particular MSG file in OutlookSpy (I am its author - click OpenIMsgOnIStg button).

Receive Email address instead of Name with POI from *.msg-File [Java]

i wrote a small java programm which extract the names,email address, subject, bodytext from a *.msg by using POI 3.15 and writes it to an excel sheet.
By reading the MAPIMessage API Documentation i saw:
getDisplayFrom() --> Gets the display value of the "FROM" line of the outlook message This is not the actual address that was sent from but the formated display of the user name.
Now i would like to get the email address from sender instead of his stored nickname.
Just btw - for receiving the Emailaddress of all "to"-persons you can use getRecipientEmailAddress().
Any suggestions how to deal with it?
thanks in advance
Edit:
I just noticed you can use the first element of getHeaders() to get the Return-Path - which is the emailaddress of "from". kinda dirty way ... so my question is still up to be answered ;)
I don't know in previous versions, but in 3.17 you can get it from main chunks.
MAPIMessage msg = new MAPIMessage("email.msg");
Chunks mainChunks = msg.getMainChunks();
StringChunk emailFromChunk = mainChunks.getEmailFromChunk();
String emailFrom = emailFromChunk.getValue();

sending an Arabic text in email

I have a problem sending an Arabic text in email with java language.
this is my message in properties file:
mail.send=تجربة
I use this syntax in jave :
ResourceBundle dq_resource = ResourceBundle.getBundle("nls.myfile_ar");
String text= dq_resource.getString("mail.send")
but when I received email U have this text : اÙ?Ù?Ù?ضÙ?ع
I try also in java with this code :
String text= new String(dq_resource.getString("mail.send").getBytes(),Charset.forName("UTF-8"));
but I have this text in mail :
ا�?�?�?ض�?ع
You need to set a header for the mail, something like
message.setHeader("Content-Type", "text/plain; charset=UTF-8");
setHeader is a method of the Message class that allows you to set a header.
The easiest solution could be using strings normally ( without getBytes method ) changing the default encoding in your workspace for example eclipse.
Windows-->Preferences-->General-->workspace-->Text file encoding
Also you can try to convert UTF-8 to UTF-16 .

How do i send HTML email containing dynamic link via JAVA

I am trying to send a dynamic link via email with the following code.
Message messageSSL = new MimeMessage(session);
int hash=1000;
String content="click here";
messageSSL.setContent(content, "text/html");
However, i have failed to generate a dynamic link.The output in the mail is in the plain text format.
Output (In the mail):
click here
Even though, the following code works and generates a link called "click here".
String content="click here";
Thanks!!
I think the problem is with backward slash. We should be using forward slash in urls. Please change and try it.
The Apache Commons Email library has some useful classes that take care of the low-level details for stuff like making HTML email work properly. Check it out:
http://commons.apache.org/proper/commons-email/
can you please enclose the link by html tag and try once.
String content="<html><body>click here </body></html>";
i am using same library and working fine for me.
please check below thread
How Can I put a HTML link Inside an email body?

Java mail PDF attachment not working

I am generating a PDF and trying to attach it to a mail as well as download it from browser using java. Download from browser works fine, but attaching to mail is where I am facing an issue. The file is attached. Attachment name and size of the file are intact. The problem is when I open the PDF from mail attachment, it shows nothing. correct number of pages with no content. When I attach the file downloaded from browser by hardcoding, it works fine. So I suppose the problem is not with the PDF generation. I tried opening both(one downloaded from browser and the other downloaded from mail) the files using comparing tool beyond compare. The one downloaded from mail shows conversion error. When I open with notepad++, both show different encoding. I not very familiar with these encoding thing. I suppose it is something to do with encoding.
I also observed that the content in mail download is same as the one at PDF generation. But the one at browser download is different.
An excerpt of what I get on browser download is as below(The content is too large to paste)
%PDF-1.4
%âãÏÓ
4 0 obj <</Type/XObject/ColorSpace/DeviceRGB/Subtype/Image/BitsPerComponent 8/Width 193/Length 11222/Height 58/Filter/DCTDecode>>stream
ÿØÿà
An excerpt of what I get on mail download is as below
%PDF-1.4
%????
4 0 obj <</Type/XObject/ColorSpace/DeviceRGB/Subtype/Image/BitsPerComponent 8/Width 193/Length 11222/Height 58/Filter/DCTDecode>>stream
????
I am using Spring MimeMessageHelper to send the message. I am using the below method to add attachment
MimeMessageHelper.addAttachment(fileName, new ByteArrayResource(attachmentContent.getBytes()), "application/pdf");
I've also tried another way of attaching but in vain
DataSource dataSource = new ByteArrayDataSource(bytes, "application/pdf");
MimeBodyPart pdfBodyPart = new MimeBodyPart();
pdfBodyPart.addHeader("Content-Type", "application/pdf;charset=UTF-8");
pdfBodyPart.addHeader("Content-disposition", "attachment; filename="+fileName);
pdfBodyPart.setDataHandler(new DataHandler(dataSource));
pdfBodyPart.setFileName(fileName);
mimeMessageHelper.getMimeMultipart().addBodyPart(pdfBodyPart);
Any help would be greatly appreciated. Thanks in advance
I'm not sure if this has anything to do with it but I noticed you're not setting the actual charset in pdfBodyPart.addHeader("Content-Type", "application/pdf;charset");, nor are you calling attachmentContent.getBytes() with a charset as parameter. How is it supposed to know which one you want to use?
What Content-Transfer-Encoding is being used for the attachment in the message you receive? Normally JavaMail will choose an appropriate value, but if document contains an unusual mix of plain text and binary, as your document seems to, JavaMail may not choose the best encoding. You can try adding pdfBodyPart.setHeader("Content-Transfer-Encoding", "base64");
I found out why it was'nt working. It is an encoding issue but nothing to do with MimeMessageHelper. The problem was I generated the PDF to an OutputStream and converted it to String and then converted it into byte array. When I converted to it to String the encoding changed resulting in the issue. So i fixed it by getting byte array from outputStream :)

Categories