Incomplete attachments remain attached to the mail - email

I am using mimedefang filtering tool. In the configuration, I strip out all the attachments and forward it to another address. For particular sender, I can see milter changes the header Content-Type from application/pdf and multipart-mixed. In the received email on outlook, when I open the pdf using text editor (it contains content like ("This is a multi-part message in MIME format..." followed by some random numbers "------------=_1525668389-64274-8--").
Can anyone guess why this might be happening?

Multi-part messages (like those with attachments) have their parts divided by a boundary. This boundary is between 1 and 70 characters and must not appear anywhere in the anywhere within the encapsulated parts of the message (between boundaries).
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary=gc0p4Jq0M2Yt08jU534c0p
This is a message with multiple parts in MIME format.
--gc0p4Jq0M2Yt08jU534c0p
Content-Type: text/html; charset=UTF-8
<html><head></head><body>This is the HTML body of the message.</body></html>
--gc0p4Jq0M2Yt08jU534c0p
Content-Type: text/plain
This is the body of the message.
--gc0p4Jq0M2Yt08jU534c0p
Content-Type: application/octet-stream
Content-Transfer-Encoding: base64
PGh0bWw+CiAgPGhlYWQ+CiAgPC9oZWFkPgogIDxib2R5PgogICAgPHA+VGhpcyBpcyB0aGUg
Ym9keSBvZiB0aGUgbWVzc2FnZS48L3A+CiAgPC9ib2R5Pgo8L2h0bWw+Cg==
--gc0p4Jq0M2Yt08jU534c0p--
I suspect that somewhere between mimedefang and your milter configuration, the boundaries are getting mangled or included into the attachment can causing them to be corrupted.

Related

Keycloak Mail Templates: force `Content-Transfer-Encoding: quoted-printable` for text MIME part

I'm using Keycloak 15.0.2. When sending an account verification email, the email that gets sent uses Content-Transfer-Encoding: 7bit for the text portion of the email.
This causes the verification link to be on one line, and violates RFC 2822 by having a line that's very long, causing my emails to be bounced.
The HTML portion of the email is properly encoded with Content-Transfer-Encoding: quoted-printable.
I've been trying to look at the source of Keycloak, but my knowledge of java is too poor to really figure it out. I'm sure somewhere the MIME message gets parsed at which point it decides on a header for each part. But I can't find where.
I have seen messages where the text portion did have the correct encoding. So I assume there's a certain condition somewhere that will force the encoding. But I can't find it.
How can I force Keycloak (or Freemarker, or javax MimeBodyPart) to use quoted-printable?
Example of a MIME output:
Content-Type: multipart/alternative; boundary="----=_Part_2_1488711957.1660016366185"
Date: Tue, 9 Aug 2022 03:39:26 +0000 (GMT)
From: Mails#covle.com
MIME-Version: 1.0
Message-ID: <126146379.3.1660016366188#b02efe4baa19>
Received: from b02efe4baa19 by mailhog.example (MailHog)
id duuNy3ONelpvr8ukUqz7WBJnrtPd0oSw43G2W9w8Ix4=#mailhog.example; Tue, 09 Aug 2022 03:39:26 +0000
Reply-To: Mails#example.com
Return-Path: <Mails#examplecom>
Subject: Verify email
To: asdasd#example.com
------=_Part_2_1488711957.1660016366185
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit
Someone has created a Bluppie account with this email address. If this was you, click the link below to verify your email address
http://localhost:8080/auth/realms/bluppie/login-actions/action-token?key=eyJhbGciOiJIUzI1NiIsInR5cCIgOiAiSldUIiwia2lkIiA6ICIzODYxY2JmMy0wMWYzLTRhMmQtOTg1NC02MmEyYWMyYzhjNzUifQ.eyJleHAiOjE2NjAwMTY2NjYsImlhdCI6MTY2MDAxNjM2NiwianRpIjoiZDVlYjlhODMtMDE0NS00YTBhLTk2M2YtYjBkMjI0ZTA0ZWVkIiwiaXNzIjoiaHR0cDovL2xvY2FsaG9zdDo4MDgwL2F1dGgvcmVhbG1zL2JsdXBwaWUiLCJhdWQiOiJodHRwOi8vbG9jYWxob3N0OjgwODAvYXV0aC9yZWFsbXMvYmx1cHBpZSIsInN1YiI6IjIxOGQ1NzkzLTA0NmYtNDQ4NS04ZmIxLTQ0M2E5NjEyM2FmZiIsInR5cCI6InZlcmlmeS1lbWFpbCIsImF6cCI6ImFjY291bnQiLCJub25jZSI6ImQ1ZWI5YTgzLTAxNDUtNGEwYS05NjNmLWIwZDIyNGUwNGVlZCIsImVtbCI6ImFzZGFzZEBjb3ZsZS5jb20iLCJhc2lkIjoiNmM3ZTk5NGItZTA0ZS00ZTlkLWFkNTQtZjE1MGM4NjcwYzdmLlFfQ244SlY0WFlBLmQ1MzI3MTMwLWIzY2EtNDY4Ny1iZDZkLWViZWFiODAwZTdkMyIsImFzaWQiOiI2YzdlOTk0Yi1lMDRlLTRlOWQtYWQ1NC1mMTUwYzg2NzBjN2YuUV9DbjhKVjRYWUEuZDUzMjcxMzAtYjNjYS00Njg3LWJkNmQtZWJlYWI4MDBlN2QzIn0.yrTUf2tl521Q00IUL-2dWTnugUt_ZeATa3W3IrgoRGM&client_id=account&tab_id=Q_Cn8JV4XYA
[NOTE: The line above is the RFC violation.]
This link will expire within 5 minutes.
If you didn't create this account, just ignore this message.
------=_Part_2_1488711957.1660016366185
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<html>
<body>
<p>Someone has created a Bluppie account with this email address. If this w=
as you, click the link below to verify your email address</p><p><a href=3D"=
http://localhost:8080/auth/realms/bluppie/login-actions/action-token?key=3D=
eyJhbGciOiJIUzI1NiIsInR5cCIgOiAiSldUIiwia2lkIiA6ICIzODYxY2JmMy0wMWYzLTRhMmQ=
tOTg1NC02MmEyYWMyYzhjNzUifQ.eyJleHAiOjE2NjAwMTY2NjYsImlhdCI6MTY2MDAxNjM2Niw=
ianRpIjoiZDVlYjlhODMtMDE0NS00YTBhLTk2M2YtYjBkMjI0ZTA0ZWVkIiwiaXNzIjoiaHR0cD=
ovL2xvY2FsaG9zdDo4MDgwL2F1dGgvcmVhbG1zL2JsdXBwaWUiLCJhdWQiOiJodHRwOi8vbG9jY=
Wxob3N0OjgwODAvYXV0aC9yZWFsbXMvYmx1cHBpZSIsInN1YiI6IjIxOGQ1NzkzLTA0NmYtNDQ4=
NS04ZmIxLTQ0M2E5NjEyM2FmZiIsInR5cCI6InZlcmlmeS1lbWFpbCIsImF6cCI6ImFjY291bnQ=
iLCJub25jZSI6ImQ1ZWI5YTgzLTAxNDUtNGEwYS05NjNmLWIwZDIyNGUwNGVlZCIsImVtbCI6Im=
FzZGFzZEBjb3ZsZS5jb20iLCJhc2lkIjoiNmM3ZTk5NGItZTA0ZS00ZTlkLWFkNTQtZjE1MGM4N=
jcwYzdmLlFfQ244SlY0WFlBLmQ1MzI3MTMwLWIzY2EtNDY4Ny1iZDZkLWViZWFiODAwZTdkMyIs=
ImFzaWQiOiI2YzdlOTk0Yi1lMDRlLTRlOWQtYWQ1NC1mMTUwYzg2NzBjN2YuUV9DbjhKVjRYWUE=
uZDUzMjcxMzAtYjNjYS00Njg3LWJkNmQtZWJlYWI4MDBlN2QzIn0.yrTUf2tl521Q00IUL-2dWT=
nugUt_ZeATa3W3IrgoRGM&client_id=3Daccount&tab_id=3DQ_Cn8JV4XYA" rel=3D"nofo=
llow">Link to e-mail address verification</a></p><p>This link will expire w=
ithin 5 minutes.</p><p>If you didn't create this account, just ignore t=
his message.</p>
</body>
</html>
------=_Part_2_1488711957.1660016366185--
tl;dr: Add any non US-ASCII character to your templates and it will be encoded as quoted-printable.
On some cached page I found some old documentation which seem to explain the logic:
getEncoding
public static String getEncoding(DataSource ds)
Get the Content-Transfer-Encoding that should be applied to the input stream of this DataSource, to make it mail-safe.
The algorithm used here is:
If the DataSource implements EncodingAware, ask it what encoding to use. If it returns non-null, return that value.
If the primary type of this datasource is "text" and if all the bytes in its input stream are US-ASCII, then the encoding is "7bit". If more than half of the bytes are non-US-ASCII, then the encoding is "base64". If less than half of the bytes are non-US-ASCII, then the encoding is "quoted-printable".
If the primary type of this datasource is not "text", then if all the bytes of its input stream are US-ASCII, the encoding is "7bit". If there is even one non-US-ASCII character, the encoding is "base64".
Parameters:
ds - the DataSource
Returns:
the encoding. This is either "7bit", "quoted-printable" or "base64"

Adding attachment into MailMessage direct from MIME string

There are a number of good examples of adding attachments from file, but I was wondering if I could do it in a similar way to the way AlternateView.CreateAlternateViewFromString works.
I am separately using IMAP to get the body_text and body_headers of an email from one address and then wish to send it from another (via SMTP) with some changes made, but the attachment added as faithfully as possible. I am using MailMessage and SmtpClient ( System.Net & System.Net.Mail )
I have written code to extract MIME parts from BODY[TEXT] by their "Content-Type:" tag and happily add plain text the html views this way. I have the attachment in the MIME too (see example below) so it seems like I should be able to easily add the attachment directly from the string I can extract from the MIME with "Content-Type: application/octet-stream". At present, I am only interested in it working with application/octet-stream.
I have this available in parts of my code as a string containing either:
Content-Type: application/octet-stream; name="test_file.abc"
Content-Disposition: attachment; filename="test_file.abc"
Content-Transfer-Encoding: base64
X-Attachment-Id: f_j7datrbn0
YWJjIGZpbGU=
Or
Content-Disposition: attachment; filename="test_file.abc"
Content-Transfer-Encoding: base64
X-Attachment-Id: f_j7datrbn0
YWJjIGZpbGU=
From example:
* 53 FETCH (BODY[TEXT] {614}
--001a1140ae52fcc4690558c101ec
Content-Type: multipart/alternative; boundary="001a1140ae52fcc4630558c101ea"
--001a1140ae52fcc4630558c101ea
Content-Type: text/plain; charset="UTF-8"
Blah blah
--001a1140ae52fcc4630558c101ea
Content-Type: text/html; charset="UTF-8"
<div dir="ltr">Blah blah</div>
--001a1140ae52fcc4630558c101ea--
--001a1140ae52fcc4690558c101ec
Content-Type: application/octet-stream; name="test_file.abc"
Content-Disposition: attachment; filename="test_file.abc"
Content-Transfer-Encoding: base64
X-Attachment-Id: f_j7datrbn0
YWJjIGZpbGU=
--001a1140ae52fcc4690558c101ec--
)
$ OK Success
All of the other aspects of my code work, but is there an easy way to directly add the attachment from the MIME data I have. I am not using MimeKit or any libraries other than standard VS ones.
Thank you for reading this long question.
Not really what I wanted to do, but I achieved what I needed as follows:
string name = find_item_by_key(attachment_content, "filename"); // looks for key="value" and returns value
string attachment_base64 = attachment_content.Substring(attachment_content.LastIndexOf("\r\n\r\n"));
attachment_base64 = attachment_base64.Trim('\r', '\n');
byte[] attachment_bytes = Convert.FromBase64String(attachment_base64);
Attachment att = new Attachment(new System.IO.MemoryStream(attachment_bytes), name);
mail.Attachments.Add(att);
Lots of pointless conversions going on, since the data is already Base64 and mail.Attachments.Add will no doubt be converting it back again.

How to compose multipart MIME mixed message to display it correctly in Outlook

I am using java code to compose and send e-mail messages to MS Outlook 365 accounts.
The message is composed as following:
plain text part;
html part containing only table;
plain text part;
File attachment.
I expect that outlook displays part 1-3 as message body and the last part as attachment. Instead it displays only first part as message itself and puts the rest parts as separate attachments.
I have played with different headers, but result is the same. Is it some feature of MS stack (exchange, outlook, etc.) or I did something wrong?
The raw message is here
Date: Mon, 5 Jun 2017 12:01:47 -0700 (PDT)
From: no-reply#example.com
To: consumer#example.com
Message-ID: <some id>
Subject: Test email
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="----=_Part_0_2350964.1496689307498"
------=_Part_0_2350964.1496689307498
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
Content before table
------=_Part_0_2350964.1496689307498
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
<html><body><table><tr><td>First parameter</td><td>value</td></tr><tr><td>Second row</td><td>42</td></tr></table></body></html>
------=_Part_0_2350964.1496689307498
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
This content should be placed after table
------=_Part_0_2350964.1496689307498
Content-Type: application/octet-stream;
name="report.xls"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="report.xls"
0M8R4KGxGuEAAAAAAAAAAAAAAAAAAAAAOwADAP7/CQAGAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAA
<skipped>
------=_Part_0_2350964.1496689307498--
As a result I see only "Content before table" line, two attachments like "ATT01.html" and "ATT02.txt" and my original attachment "report.xls".
I have found one related topic at some forum saying that this problem should not appear since Outlook 2010 or so, but I can see it in the latest version both in desktop app and the web application.
The workaround solution to put everything in html part requires more work, because text parts are generated by other components.
You are grouping the parts incorrectly. It must be
1. Outer message, multipart/mixed
2. multipart/related
2.a text/plain
2.b text/html
3. attachment.

To send body and attachment both using JCL utility TCPSMTP

Requirement: To send VB file of records length 100 as attachment using TCPSMTP utility with proper message in body without using IEBGENER utility.
I am trying to send email attachment file VB dataset as attachment. Its working either message in attachment or attachment file in body. But both simultaneously not working.
My JCL is:
//IRTCPN15 EXEC PROC=TCPSMTP
//SMTPIN DD DSN=EMAIL.CODE,
// DISP=SHR
// DD DSN=FILE.TOBE.SENTAS.ATTACH.MENT,DISP=SHR
Here, I have used both datasets EMAIL.CODE and FILE.TOBE.SENTAS.ATTACH.MENT of same specification VB 100 record length. I have also tried using boundary demiliter, but still its not working both together.
Dataset EMAIL.CODE contains:
HELO *******
MAIL FROM:<*******>
RCPT TO: <********>;
DATA
FROM: <******>
TO: <*******>;
SUBJECT: subject data
MIME-VERSION: 1.0
CONTENT-TYPE: TEXT/PLAIN
---Mail Body---
CONTENT DISPOSITION: ATTACHMENT; FILENAME=FILE.TXT
Please suggest me how to send this attachment with body. I have used asterisk due to security reasons. Please feel free to ask if any more information is needed.
In the EMAIL.CODE dataset, you're specifying that content-type of your message is text/plain. However, text/plain on its own (which is the default content type anyway) is always going to appear inline.
In order for the text in the message to be seen as an attachment, you need a Content-Disposition header that specifies attachment.
I can see in your question that you have a CONTENT DISPOSITION line, but it's labeled as being part of the message body. In addition to the fact that it needs to be a header, not a part of the body, it also needs to be hyphenated. So you should have CONTENT-DISPOSITION, not CONTENT DISPOSITION.
However, what all of this gets you is a message containing nothing but the attachment, and your question specifies that you want both a message body and an attachment. In order to do that, your Content-type at the top level needs to be multipart/mixed, and the body of the message needs to contain two MIME parts, one specified simply as being text/plain, and the other also text/plain, but with Content-Disposition: attachment.
This example shows the data for a MIME message containing both a text/plain body and a text/plain attachment.
FROM: <sender#example.com>
TO: <receiver#example.com>
Subject: TESTING message with body and attachment.
Mime-Version: 1.0
Content-type: multipart/mixed; boundary=MIME_BOUNDARY
This is the non-MIME body of a multipart message in MIME format.
Unless you are using a genuinely ancient email client or viewing
the raw source of a message, you should never see this paragraph.
--MIME_BOUNDARY
Content-type: text/plain
This is the inline text section of a multipart message
in MIME format. This is what will appear as the body
of your email when using any normal email client.
--MIME_BOUNDARY
Content-type: text/plain
Content-Disposition: attachment; filename=example.txt
This is the plain-text attachment.
--MIME_BOUNDARY--
.

What is the correct content-type and document structure for an email that only contains an text/html part

First off- I'm fixing up an existing open source library. While I know that people SHOULD send a plaintext version of a message when they send an html email, this isn't a best-practices question. If I don't maintain backwards compatibility, they won't accept my patch.
I'm trying to figure out how to best handle situations where ONLY an html email is sent.
The library currently generates this:
MIME-Version: 1.0
Content-Type: text/html;
Hello, World
But every html-only message I've seen in my inbox shows:
MIME-Version: 1.0
Content-Type: multipart/alternative;
boundary="----=_Part_1935495_1732146301.1367384830372"
----=_Part_1935495_1732146301.1367384830372
Content-Type: text/html;
Hello, World
----=_Part_1935495_1732146301.1367384830372--
I can't figure out if this is a best-practice or a requirement
I've been through :
https://www.rfc-editor.org/rfc/rfc2557
http://www.ietf.org/rfc/rfc2854.txt
but couldn't find any information.
Use Content-Type: Multipart/related; type=Text/HTML:
From: user1#example.com
To: user2#example.com
Subject: An example
Mime-Version: 1.0
Content-Base: http://www.example.com
Content-Type: Multipart/related; boundary="boundary-example-1";type=Text/HTML
--boundary-example-1
Content-Type: Text/HTML; charset=ISO-8859-1
Content-Transfer-Encoding: QUOTED-PRINTABLE
... text of the HTML document, which might contain a hyperlink
to the other body part, for example through a statement such as:
<IMG SRC="/images/ie5.gif" ALT="Internet Explorer logo">
Example of a copyright sign encoded with Quoted-Printable: =A9
Example of a copyright sign mapped onto HTML markup: ยจ
References
MIME Hierarchies of Body Parts
MIME Standard Header Fields
Handling Binary Data in XML Documents
Direct Mailer Requirements
Multipart Messages
HTML Threading:
Conventions for use of HTML in email