What is the type you get when you call javax.mail.Message.getInputStream() - jakarta-mail

I am trying to parse an email then save the actual email as a file in the file system for auditing and remove it from the exchange server. (same way like you do email save as in outlook)
So to do that I have found out that I can call
Message.getInputStream()
To retrieve the file bytes. Its working Ok, and I can write the email as a file to the file system.
my question is what is this file type? is it .eml or .msg? or something else?
when looking at its content I see text and not binary data
--_004_MWH_#TRUNCATED#_11namp_
Content-Type: multipart/alternative;
boundary="_000_MWHPR_#TRUNCATED#_1711namp_"
--_000_MWHPR10_#TRUNCATED#_HPR10MB1711namp_
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: base64
getInputStream doesn't say much about the type of data...
https://docs.oracle.com/javaee/7/api/javax/mail/Part.html#getInputStream--

Use getContentType on the message. Mime messages map to .eml on Windows.

You need to use Message.writeTo to save the message to a file.

Related

BizTalk incorrect request (multipart form data)

We need to send a file of xbrl format (a type of xml) to a receiving system. However, the receiving system requires that we are sending some form data as well. Regarding the xbrl file we need to send with it a form data, name="data".
Regarding the second part, we need to send an email to the system via form data. And that is name="email".
(I'll be the first to admit that I really do not understand form data at all so I cant explain much better than this).
I have a custom pipeline and in the encode stage I have put my pipeline component (the entire Execute method is down below) and after that a MIME/SMIME component (standard Microsoft).
Take a look at how the request looks like when it is being sent from Postman (and fully accepted by the receiving system):
Content-Type: multipart/form-data; boundary=--------------------------035085236562027409735938
Content-Length: 299420
----------------------------035085236562027409735938
Content-Disposition: form-data; name="data"; filename="Dk_{5AA9547A-E103-40CD-A2F4-6B77F010E570}.xbrl"
Content-Type: application/xml
<?xml version="1.0" encoding="utf-8"?>
<!- ---- Lots of removed xbrl ---- ->
</xbrli:xbrl>
----------------------------035085236562027409735938
Content-Disposition: form-data; name="email"
Content-Type: text/plain
test#test.com
----------------------------035085236562027409735938--
Now what you see below is what we have managed to achieve this far:
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="_E9E8D52C-4722-488D-9FEC-713446D4A5C4_"
--_E9E8D52C-4722-488D-9FEC-713446D4A5C4_
MIME-Version: 1.0
Content-Type: application/xml; charset="UTF-8"
Content-Transfer-Encoding: 7bit
Content-ID: {E8CA3F1A-F953-4FFB-A054-DEB8466A5D86}
Content-Description: body
<?xml version="1.0" encoding="utf-8"?>
<!- ---- Lots of removed xbrl ---- ->
</xbrli:xbrl>
--_E9E8D52C-4722-488D-9FEC-713446D4A5C4_
MIME-Version: 1.0
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 7bit
Content-ID: {3B35ECAC-990D-458C-8E7E-9C178CA99988}
Content-Description: email
Content-Disposition: attachment; filename="=?utf-8?B?QXR0YWPobWVudDF=?="
test#test.com
--_E9E8D52C-4722-488D-9FEC-713446D4A5C4_--
Now, a quick comparison reveals that we are closing in on the solution. The first important issue must be that the Content-Type (first part below MIME-version: 1.0) is becoming multipart/mixed when we need multipart/form-data.
Second issue is in the Content-Disposition which for both parts does not, at all, look like expected.
Please take a look at my pipeline component. What you see is the entire code from the Execute. Since formDataPart.Data only takes a stream I have a method that translates a string to a stream (MemoryStream).
// Variables
var outMsg = pContext.GetMessageFactory().CreateMessage();
outMsg.Context = PipelineUtil.CloneMessageContext(pInMsg.Context);
outMsg.Context.Promote("ContentType", "http://schemas.microsoft.com/BizTalk/2003/http-properties", "multipart/form-data;");
// Process xbrl file
var msgPart = pContext.GetMessageFactory().CreateMessagePart();
msgPart.PartProperties.Write("FileName", "http://schemas.microsoft.com/BizTalk/2003/mime-properties", "SomeID.xbrl");
msgPart.PartProperties.Write("ContentDescription", "http://schemas.microsoft.com/BizTalk/2003/mime-properties", "xml");
//msgPart.PartProperties.Write("ContentTransferEncoding", "http://schemas.microsoft.com/BizTalk/2003/mime-properties", "7bit");
msgPart.Data = pInMsg.BodyPart.Data;
msgPart.ContentType = "application/xml";
msgPart.Charset = "UTF-8";
outMsg.AddPart("data", msgPart, true);
// Form data (email)
var formDataPart = pContext.GetMessageFactory().CreateMessagePart();
formDataPart.ContentType = "text/plain";
formDataPart.Charset = "utf-8";
formDataPart.PartProperties.Write("ContentType", "http://schemas.microsoft.com/BizTalk/2003/http-properties", "form-data;");
formDataPart.Data = GenerateStreamFromString("test#test.com");
outMsg.AddPart("email", formDataPart, false);
return outMsg;
Does anybody know whow we can achieve this? I can also mention that we are using the MIME/SMIME component after this custom pipeline component.
I really think that the solution from Greg Forsythe would be the best solution since I would have total control over the message but I have no idea how to translate that into actual code.
Greg_suggestion
UPDATE: After so many trials and not coming anywhere we decided to ditch the above solution and go with Gregs solution instead. There is an issue there so I hope some C# guru can help us out. Please take a look at the new question if you are struggling with the form data request issue.
Second attempt here

The parsing of an eml with javamail doesn't recognize properly nested messages

I'm implementing an .eml parser using Javamail 1.5.6, I've started copying from msghow.java a sample provided within javamail.
I'm testing an eml which contains as attachment another eml, this is an extract:
MIME-Version: 1.0
Date: Tue, 30 Apr 2019 16:20:45 +0200
Message-ID: <CA+fLqEW8TUfSxih9DTp2WXa63pS7wf1eZiro_9k1XS4AShN5Zg#mail.gmail.com>
Subject: Message with an eml as attachment
From: a b <ab#gmail.com>
To: cd#pec.cd.it
Content-Type: multipart/mixed; boundary="00000000000057f76c0587c01bc9"
--00000000000057f76c0587c01bc9
Content-Type: multipart/alternative; boundary="00000000000057f7670587c01bc7"
--00000000000057f7670587c01bc7
Content-Type: text/plain; charset="UTF-8"
Hello guys,
this is a simple message from a not certified account, it contains only one
attachment, an eml message
--00000000000057f7670587c01bc7
Content-Type: text/html; charset="UTF-8"
<div dir="ltr">Hello guys,<div><br></div><div>this is a simple message from a not certified account, it contains only one attachment, an eml message</div></div>
--00000000000057f76c0587c01bc9
Content-Type: message/rfc822; name="Cena zerebao.eml"
Content-Disposition: attachment; filename="Cena zerebao.eml"
Content-Transfer-Encoding: base64
X-Attachment-Id: f_jv3vpu760
Content-ID: <f_jv3vpu760>
WC1Ob3Rlcy1JdGVtOiBGcmksIDYgSnVsIDIwMTggMTc6NDA6MDAgKzAyMDA7DQogdHlwZT00MDA7
IG5hbWU9T3JpZ2luYWxNb2RUaW1lDQpYLU5vdGVzLUl0ZW06IE1lbW87DQogbmFtZT1Gb3JtDQpY
LU5vdGVzLUl0ZW06IFN0ZE5vdGVzTHRyMjU7DQo.... and so on
Javamail recognizes that eml but when I get its subject, date, body, attachments and so on, they all are null.
msghow.java itself doesn't see them.
Before javamail I implemented my parser with mime4j and I haven't this problem, but now I would like to parse emls using only javamail if possible
From the javadocs describing the mail.mime.allowencodedmessages property:
The MIME spec does not allow body parts of type message/* to be encoded. The Content-Transfer-Encoding header is ignored in this case. Some versions of Microsoft Outlook will incorrectly encode message attachments. Setting this System property to "true" will cause the Content-Transfer-Encoding header to be honored for message attachments. The default value of this property is false.

Form Data: wrong Content-Type for .p7m files

I need to save a file with correct MimeType for .p7m files (application/pkcs7-mime) via form upload to the server.
In the request I noticed that Content-Type is wrong:
------WebKitFormBoundaryaglEgtBJlb65v7d5
Content-Disposition: form-data; name="file0"; filename="getmymimeplease.p7m"
Content-Type: application/pkcs7
it should be:
Content-Type: application/pkcs7-mime
How is possible that the '-mime' part is missing (or truncated) ?
This is usually controlled by OS and/or Browser. On windows, this is set in the registry, in HKEY_CLASSES_ROOT\.<fileextension>, e.g. HKEY_CLASSES_ROOT\.p7m, in the Field Content Type:
So in the end, this is controlled by the client. So if there are several possible mime types for the same extension, you need to cover that in your server code (accept or decline, convert to your default or not)

not reading the file when it was sent by postman

I have a problem with the sending of „Post” by postman
The request is constructed in this way:
my request
When I add various headers, I receive always the response „no data”.
When I send the file by the website, on which I tried to send the request, I receive always the returnable file – sending exacly the same file which I sent by postman.
The Problem is that when I don't send the file from the website, I receive the request „no file” and even as I send the request from postman without the file, I receive the request „no data”
So how can i send the file ?
request headers
The field containing the file has a name, and it's not "Content-Disposition." Check the web site HTML, see what the name of the field is, and enter it as the name of the File field in Postman. Note that the file field name appears in the request body, not the headers. It will look something like this:
------WebKitFormBoundaryzp81ydREocuBHzYN
Content-Disposition: form-data; name="datafile"; filename="0041e2b.jpg"
Content-Type: image/jpeg
In this case the field name is "datafile."

Getting body part headers with IMAP C-Client

I am using UW IMAP c-client v. 2007e and I couldn't find the following function: I need to retrieve a header of certain body part. I.e., the mail message contains multiple body parts, and one of them is an attachment looking like this:
--_004_57D6D2035A40B8ECEBA59CB9C13F52A1334093MBXC18_
Content-Type: text/plain; name="func.txt"
Content-Description: func.txt
Content-Disposition: attachment; filename="func.txt"; size=604;
creation-date="Thu, 12 Aug 2010 18:41:40 GMT";
modification-date="Thu, 12 Aug 2010 18:41:40 GMT"
Content-Transfer-Encoding: base64
...blah-blah-blah in base64...
Now, I can retrieve the blah-blah part with imap_fetchbody() but I don't see the function that can give me only the headers part (or headers+body, that would be OK too). I can get whole message text, but then I'd have to parse it myself into parts, which I don't want to do. Is there a way I could make c-client retrieve this header for certain part?
You want the MIME section of the part. From the IMAP RFC:
The MIME part specifier refers to the [MIME-IMB] header for
this part.
The c-client function for doing a FETCH is:
long imap_msgdata (MAILSTREAM *stream,unsigned long msgno,char *section,
unsigned long first,unsigned long last,STRINGLIST *lines,
long flags)
So if you want the headers from part 2.3 of message #8 (for instance), you'd call it as:
imap_msgdata(stream, 8, "2.3.MIME", 0, 0, null, FT_PEEK);
(FT_PEEK keeps the message from being marked as \Seen. If you want to mark the message read, pass 0 as the last argument.)

Resources