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.
Related
I write a Java 8 code that sends e-mails using SimpleJavaMail ( https://www.simplejavamail.org ) and I am using an HTML content that has embedded images e.g. <img src="cid:ji">
I add the image via currentEmailBuilder.withEmbeddedImage("ji" , new FileDataSource("smiley.png"))
After that, the e-mail is delivered but the image is not inline, instead comes as an attachment.
This is because FileDataSource does not understand the Mime Type of the png file (although it is a proper png file) and reports it by default as application/octet-stream.
Furthermore when the attached image is e.g. JPG , then the mime type is properly detected but it is attached with an unexpected CID. Instead of the one that I use in the template (e.g. ji) it comes with the file extension along (!!) i.e. ji.jpg and thus the CID does not match the template so the image again does not appear properly.
I must be doing something really wrong!
I employed a workaround by creating a class of my own named ExtendedFileDataSource that reports correct MimeType and CID but it is still very strange that I have searched a lot and nobody seems to have this or a similar problem with SimpleJavaMail.
Can you help me out so that I can get rid of the extra class?
Thank you in advance for your time!!
Panagiotis
In case you need to see the code that embeds the images is:
for (Map.Entry<String, String> entry : images.entrySet()) {
// I use a TreeMap that maps the file path to the CID (name)
final String path = entry.getKey();
final String name = entry.getValue();
// this call created the file from a custom service that I created to handle the filesyste
final File file = FilesService.api.fileFromDbNPath(account.getDatabase(), path);
if (!file.exists()) {
throw new ApiException(ApiErrorCodes.MailInexistentImage, "database", account.getDatabase(), "path", path);
}
mailBuilder.withEmbeddedImage(name, new FileDataSource(file));
}
and the resulting e-mail has these two parts (among others):
------=_Part_2_1209984735.1634128409176
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<p><b>Na =CF=84=CE=BF=CF=82 =CE=BF =CE=B1=CF=81=CF=87=CE=B7=CE=B3=CF=8C=CF=
=82:</b> =CE=9B=CE=BF=CF=8D=CE=BB=CE=B7=CF=82 =CE=A0=CE=B9=CE=BB=CE=BF=CF=
=8D=CE=BB=CE=B7=CF=82 =CE=BC=CE=B5 =CF=84o =CE=BC=CE=AD=CE=B9=CE=BB!</p><p>=
=CE=96=CE=97=CE=A4=CE=A9!!</p>
<hr>
<p><img src=3D"cid:efydpepxon"><br><img src=3D"cid:ji"><br><img src=3D"cid:=
testimage"></p>
<hr>
<h1>Fin</h1>
------=_Part_2_1209984735.1634128409176--
------=_Part_1_380493606.1634128409176
Content-Type: application/octet-stream; filename=testimage.png; name=testimage.png
Content-Transfer-Encoding: base64
Content-Disposition: inline; filename=testimage.png
Content-ID: <testimage.png>
iVBORw0KGgoAAAANSUhEUgAAAQAAAAEACAMAAABrrFhUAAAC/VBMVEUXFBAiHRYrJRxOMQMzLihV
OQIOhRhEOi1eQ...............................7IwbTAwASAIPHaQDihBgQ4fUhBRQdCOx
HPaGyXeFOSEfP0LX3UwUgE+ZfgmslckQ8LdK3ik6zMM/0KX1yGoJ++AlMbAfc4Oe7wEA2lSmadFe
khgCOHdJbIL/UGaT97paY2E/5gb/H72WgFosmUw1AAAAAElFTkSuQmCC
------=_Part_1_380493606.1634128409176
Content-Type: image/jpeg; filename=ji.jpg; name=ji.jpg
Content-Transfer-Encoding: base64
Content-Disposition: inline; filename=ji.jpg
Content-ID: <ji.jpg>
/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAUEBAQEAwUEBAQGBQUGCA0ICAcHCBALDAkNExAUExIQ
EhIUFx0ZFBYcFhISGiMaHB4fISEhFBkkJyQgJh0gISD/2wBDAQUGBggHCA8ICA8gFRIVICAgICAg
ICAgICAgICAgI...........................NFFABmjNFFABmjNFFABmjNFFABmjNFFABmjN
FFABmjNFFABmjNFFABmjNFFABmjNFFABmjNFFABmjNFFABmjNFFABmjNFFABmjNFFADGVmORIy+w
x/UUUUUAf//Z
------=_Part_1_380493606.1634128409176
Your mail source code show you the id Content-ID: <testimage.png> & Content-ID: <ji.jpg>. That means the id in your html file/code img has to be the same --> <img src"cid:testimage.png"/> & <img src"cid:ji.jpg"/>.
I had exactly the same issue, and never find an answer until I do it myself, so I hope my answer will help someone.
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.
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.
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--
.
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