VelocityEngineUtils.mergeTemplateIntoString encoding to use for email generation - java

With Spring 3.0.5, I can generate emails by calling VelocityEngineUtils.mergeTemplateIntoString(VelocityEngine velocityEngine, String templateLocation, Map model). This method generates a string that is fully compliant with MIME mail.
With Spring 3.2.11, that method is unsupported: one must use VelocityEngineUtils.mergeTemplate(VelocityEngine velocityEngine, String templateLocation, String encoding, Map model, Writer writer) with an additional encoding parameter.
Being a mailing context, I tried org.apache.commons.codec.CharEncoding.US_ASCII (which equals to US-ASCII) but it totally break my mails. All clients I tried (Outlook web mail, Outlook 2013, Windows 8's Mail) don't see subject and display all the MIME parts like the following:
------=_Part_0_1846461141.1413184892265
Content-Type: multipart/related; boundary="----=_Part_1_249771572.1413184892276"
------=_Part_1_249771572.1413184892276
Content-Type: multipart/alternative; boundary="----=_Part_2_861087694.1413184892311"
------=_Part_2_861087694.1413184892311
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
(Look at UTF-8!)
The question is: what is the correct way to invoke mergeTemplateIntoString with encoding argument?

Related

How to fix different multipart boundary implementations?

I created a file upload functionality in Jersey.
#POST
#Path("/import")
#Consumes(MediaType.MULTIPART_FORM_DATA)
public static Response importFile(
#FormDataParam("file") InputStream fileInputStream,
#FormDataParam("file") FormDataContentDisposition fileMetaData,
When I send a file as multipart formdata from the browser (Firefox and Chrome) using Fetch API or a jQuery POST, Jersey responds with a org.jvnet.mimepull.MIMEParsingException: Missing start boundary.
This is apparently not due to the fact that e.g. Chrome uses a case-sensitive boundary, as others have had, since the issue happens in Firefox as well, which uses a numeric boundary.
It seems that in my case, the browser sends the boundary with two additional dashes before each boundary in the body and after the last one (which seems to be correct according to the multipart specification https://www.w3.org/Protocols/rfc1341/7_2_Multipart.html), but Jersey (2.25.1) / Mimepull (1.9.14) does not accept that.
Content-Type multipart/form-data; boundary=
---------------------------23978830417520517351040096390
-----------------------------23978830417520517351040096390
Content-Disposition: form-data; name="file"; filename="testdata.csv"
Content-Type: text/csv
<snip>
-----------------------------23978830417520517351040096390--
On the contrary, when I send my request using Postman, it encodes the boundary in the body without those two additional dashes and Jersey accepts the request.
POST /myapi/import HTTP/1.1
Content-Length: 708
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="file"; filename="testdata.csv"
Content-Type: text/csv
(data)
----WebKitFormBoundary7MA4YWxkTrZu0gW
<snip>
----WebKitFormBoundary7MA4YWxkTrZu0gW
What is the correct behaviour in this case and how is it possible to implement the file upload in a way that different clients can use it?
Is there an alternative to using multipart implementation for file uploads?

How to decode an attachment chunked filename in javamail?

I am trying to figure out how to manage an attachment of a mail that has the filename chunked. I am on java 1.6 with javamail 1.5.1 and my code can manage all kind of mails but when I receive one from a specific adress(i can't talk with those guys) it doesn't import some attachments properly. When I open those atachments(assuming that this one's name is filename without extension.extension) with notepad I find this:
Content-Type: application/octet-stream;
name*0="filename without extension"; name*1=.extension
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename*0="filename without extension"; filename*1=.extension
before the base64 content. I tried to search for similar problems but I have the exact filename but it's splitted and the BodyPart.getFilename() method returns null. I was thinking about taking the whole header and work on that.
This should be supported by your version of Javamail.
System.setProperty("mail.mime.decodeparameters", "true");
For more info, search for "RFC 2231" on the package documentation of javax.mail.internet:
https://docs.oracle.com/javaee/6/api/javax/mail/internet/package-summary.html

Cannot consume content type - even with correct #Consumes

I'm working on a REST resource that takes in a key and data (value) . I'd like to be robust so this data can be anything. It could range from a string to a file.
To handle this, I have the PUT REST method which is at the end of the post. Nothing fancy, just trying to PUT basic data. If I send data that is a string it works no problem.
However, if I try to send a file across, I get this error: org.jboss.resteasy.spi.UnsupportedMediaTypeException: Cannot consume content type. Here's why this doesn't make sense to me:
If I change the #Consumes from application/octet-stream to #Consumes("multipart/form-data"), I can send a file across just fine. When examining the file that's stored I see something like this:
------WebKitFormBoundaryfuQalizBHtg1BiLJ
Content-Disposition: form-data; name="fileUpload1"; filename="uploadedFile.extension"
Content-Type: application/octet-stream
/* file information here */
------WebKitFormBoundaryfuQalizBHtg1BiLJ--
Notice that the Content-Type is supposedly application/octet-stream which is what I tried consuming before but got the Cannot consume content type exception. I have no idea what would be causing this. I'm using the Advanced Rest Client extension for Chrome to send my request and it looks something like this: https://i.imgur.com/KvKCIkl.jpg
Any thoughts?
#PUT
#Path("{key}")
#Consumes("application/octet-stream")
public Response addData(
final #PathParam("key") String key,
final InputStream data) {
final Service service = new Service();
try {
service.addData(key, data);
} finally {
IOUtils.closeQuietly(data
}
return Response.status(204).build();
}
A multipart/form-data message contains a series of parts divided by a boundary. Every part has its own Content-Type. So in your example Content-Type: application/octet-stream is the Content-Type of one (and probably the only one) part but the Content-Type of the whole message is multipart/form-data. A full message with multiple parts could look like this:
Content-Type: multipart/form-data; boundary=WebKitFormBoundaryfuQalizBHtg1BiLJ
------WebKitFormBoundaryfuQalizBHtg1BiLJ
Content-Disposition: form-data; name="fileUpload1"; filename="uploadedFile.extension"
Content-Type: application/octet-stream
/* file information here */
------WebKitFormBoundaryfuQalizBHtg1BiLJ--
------WebKitFormBoundaryfuQalizBHtg1BiLJ
Content-Type: text/plain
/* some text here */
------WebKitFormBoundaryfuQalizBHtg1BiLJ--

eps attachment parsing with javamail

Is any special handling required to parse the eps attachment in the eml using javamail
I dont have any issue other types of attachments. only eps attachment gives problem
When i send the eps attachment using outlook 2010 it converts encoding from base64 to quoted-printable.
Below is the header of the eps attachment i am trying to parse.
Content-Type: application/postscript;
name="LOGO.eps"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
filename="LOGO.eps"
The attachment is extracted. But when i open it with document viewer it says loading forever
JavaMail tries to guess the best Content-Transfer-Encoding for the data by looking at the actual data. If JavaMail guesses that the data is text, it's likely to use quoted-printable. In some cases, the guess can be wrong, in which case you'll need to specify the encoding yourself. If you're using JavaMail 1.5, there's a new attachFile method that makes this easier.

My Chinese characters change to question marks in an email

I retrieve a value from a DB field which looks like two boxes [][] in the sql/developer.
When I copy and paste that value to outlook (new message window) and send it to myself I can see the correct Chinese characters in the email 测试.
However If I do that via java code, meaning retrieve the value from db and send the email from java I get two ?? question marks in the resulting email in outlook.
The original setting in java code was:
Content-Type: text/plain;
I also tried to send the email with these settings:
Content-Type: text/plain; charset=UTF-8
Content-Type: text/html; charset=UTF-8
Nothing helped. Any ideas?
You also need to set your connection encoding to be utf-8
jdbc:mysql://localhost/some_db?characterEncoding=UTF-8
Try changing it in your control panel
Control Panel -> Regional and Language Options

Categories

Resources