Why are my icalendar invitations not processed by the outlook sniffer? - c#

Invitations generated by my ASP.net application, sent as email with .ics attachment to Outlook 2010, are not being processed by the sniffer. As such, they are not appearing as tentative in the calendar, and are not available in the preview pane. The .ics attachment appears to be valid and can be opened in outlook by double clicking. The same invitations sent to Gmail are processed no worries. I have ruled out a number of accepted solutions to the same problem...
Outlook is correctly configured, and processes Gmail invitations no problem
The message is sent as Content-Type: multipart/mixed, with the attachment as text/calendar.
DTEND follows DTSTART !
The invitation includes an organizer and an attendee.
The most obvious difference between my invitations and Gmails' is the absence of a DKIM signature, but others have succeeded without this. More generally, has anyone found any microsoft documentation about the operation, logging or troubleshooting of the sniffer?
This is my .ics attachment.
BEGIN:VCALENDAR
VERSION:2.0
CALSCALE:GREGORIAN
METHOD:REQUEST
BEGIN:VEVENT
DTSTART:20140617T083644Z
DTEND:20140617T093644Z
DTSTAMP:20140617T083647Z
ORGANIZER;CN=sby#dimo-gestion.fr:mailto:sby#dimo-gestion.fr
ATTENDEE;CUTYPE=INDIVIDUAL;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;RSVP=
FALSE;CN=bbsimonbb#gmail.com;X-NUM-GUESTS=0:mailto:bbsimonbb#gmail.com
CREATED:20140617T083647Z
DESCRIPTION:Description of flying to Sligo
LAST-MODIFIED:20140617T083647Z
LOCATION:
SEQUENCE:0
STATUS:CONFIRMED
SUMMARY:Fly to Sligo
TRANSP:OPAQUE
UID:20140617T083647Z
END:VEVENT
END:VCALENDAR
The property X-MS-OLK-FORCEINSPECTOROPEN, specified here, hasn't helped.
My file passes the three iCalendar validators mentionned in this question
My god the internet is clogging up with folk who can't get their invitations into Outlook.
Here, here, and here.
The consensus seems to be that you need to add "; method=REQUEST" after the content type in the header of the calendar MIME part. Trouble is, the .net System.Net.Mail library doesn't seem to offer low level access to set this line. The hunt continues.

Ok I've cracked it. The solution that's worked for me is the combination of the two suggestions here. The text/calendar part must be the only part of the message, and method=REQUEST must be present in the Content-Type header.
To achieve this in .net, you can use AlternateViews as follows...
MailMessage msg = new MailMessage();
msg.From = new MailAddress("gonzo#work");
msg.To.Add("gonzo#home");
System.Net.Mime.ContentType mimeType = new System.Net.Mime.ContentType("text/calendar; method=REQUEST");
AlternateView icalView = AlternateView.CreateAlternateViewFromString(icalendarString, mimeType);
icalView.TransferEncoding = TransferEncoding.SevenBit;
msg.AlternateViews.Add(icalView);
client.Send(msg);
The nice bit is that, in the absence of a body, attachments or other alternate views, .net is clever enough to construct a mail with just one part. Using an alternateView remains necessary, because it's the only way to control the Content-type header. This trick could be useful for anyone else who wants to set the Content-Type of a simple single-part mail in .net. The resulting mail, then, looks like this...
MIME-Version: 1.0
From: gonzo#work
To: gonzo#home
Subject: Fly to Sligo
Content-Type: text/calendar; method=REQUEST
Content-Transfer-Encoding: 7bit
BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//www.notilus.com//Dimo Gestion Notilus//FR
CALSCALE:GREGORIAN
METHOD:REQUEST
X-MS-OLK-FORCEINSPECTOROPEN:TRUE
BEGIN:VEVENT
DTSTART:20140619T080132Z
DTEND:20140619T090132Z
DTSTAMP:20140619T080132Z
ORGANIZER;CN=gonzo#work:mailto:gonzo#work
ATTENDEE;CUTYPE=INDIVIDUAL;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;RSVP=
FALSE;CN=gonzo#home;X-NUM-GUESTS=0:mailto:gonzo#home
CREATED:20140619T080132Z
DESCRIPTION:Description of flying to Sligo
X-ALT-DESC;FMTTYPE=text/html:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//E
N">\n<html>\n<body>\n<table border="1"><tr><td>\n<b>HTML</b> Description o
f flying to Sligo\n</td></tr><tr><td>\n<ul><li>HTML has certain advantages
</li></ul>\n</td></tr></table>\n</body>\n</html>
LAST-MODIFIED:20140619T080132Z
LOCATION:
SEQUENCE:0
STATUS:CONFIRMED
SUMMARY:Fly to Sligo
TRANSP:OPAQUE
UID:20140619T080132Z
END:VEVENT
END:VCALENDAR
A big thank you to gmail, for effortlessly constructing a working example, and for the marvelous "show original" option. As discussed above, google somehow manages to have a much more complicated message processed correctly, but you need to be a google programmer to figure that out.

This quite likely has to do with your message MIME structure. You may have to put the icalendar stream in a multipart/alternative (see https://www.rfc-editor.org/rfc/rfc6047#section-4.2 ), or worse, a multipart/mixed containing a multipart/alternative to accomodate all clients.

Related

Send a Email with an Image inside the string html body [duplicate]

I'm trying to send a multipart/related html email with embedded gif images. This email is generated using Oracle PL/SQL. My attempts have failed, with the image showing up as a red X (in Outlook 2007 and yahoo mail)
I've been sending html emails for some time, but my requirements are now to use several gif images in the email. I can store these on one of our web servers and just link to them, but many users email clients will not show them automatically and will need to either change settings or manually download them for each email.
So, my thoughts are to embed the image. My questions are:
What am I doing wrong here?
Is the embedding approach the correct one?
Any other options if I need to use more and more images? Attachments won't work, as the images are typically logos and icons that won't make sense out of the context of the message. Also, some elements of the email are links into an online system, so generating a static PDF and attaching won't work (to my knowledge anyway).
snippet:
MIME-Version: 1.0
To: me#gmail.com
BCC: me#yahoo.com
From: email#yahoo.com
Subject: Test
Reply-To: email#yahoo.com
Content-Type: multipart/related; boundary="a1b2c3d4e3f2g1"
--a1b2c3d4e3f2g1
content-type: text/html;
<html>
<head><title>My title</title></head>
<body>
<div style="font-size:11pt;font-family:Calibri;">
<p><IMG SRC="cid:my_logo" alt="Logo"></p>
... more html here ...
</div></body></html>
--a1b2c3d4e3f2g1
Content-Type: image/gif;
Content-ID:<my_logo>
Content-Transfer-Encoding: base64
Content-Disposition: inline
[base64 image data here]
--a1b2c3d4e3f2g1--
Many thanks.
BTW: Yes, I have verified that the base64 data is correct, as I can embed the image in the html itself (using same algo use for creating header data) and see image in Firefox/IE.
I should also note that this is NOT for spam, the emails are sent to specific clients who are expecting it daily. The content is data-driven, and not adverts.
Try to insert it directly, this way you can insert multiple images at various locations in the email.
<img src="data:image/jpg;base64,{{base64-data-string here}}" />
And to make this post usefully for others to:
If you don't have a base64-data string, create one easily at:
http://www.motobit.com/util/base64-decoder-encoder.asp from a image file.
Email source code looks something like this, but i really cant tell you what that boundary thing is for:
To: email#email.de
Subject: ...
Content-Type: multipart/related;
boundary="------------090303020209010600070908"
This is a multi-part message in MIME format.
--------------090303020209010600070908
Content-Type: text/html; charset=ISO-8859-15
Content-Transfer-Encoding: 7bit
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-15">
</head>
<body bgcolor="#ffffff" text="#000000">
<img src="cid:part1.06090408.01060107" alt="">
</body>
</html>
--------------090303020209010600070908
Content-Type: image/png;
name="moz-screenshot.png"
Content-Transfer-Encoding: base64
Content-ID: <part1.06090408.01060107>
Content-Disposition: inline;
filename="moz-screenshot.png"
[base64 image data here]
--------------090303020209010600070908--
//EDIT: Oh, i just realize if you insert the first code snippet from my post to write an email with thunderbird, thunderbird automatically changes the html code to look pretty much the same as the second code in my post.
The other solution is attaching the image as attachment and then referencing it html code using cid.
HTML Code:
<html>
<head>
</head>
<body>
<img width=100 height=100 id="1" src="cid:Logo.jpg">
</body>
</html>
C# Code:
EmailMessage email = new EmailMessage(service);
email.Subject = "Email with Image";
email.Body = new MessageBody(BodyType.HTML, html);
email.ToRecipients.Add("abc#xyz.com");
string file = #"C:\Users\acv\Pictures\Logo.jpg";
email.Attachments.AddFileAttachment("Logo.jpg", file);
email.Attachments[0].IsInline = true;
email.Attachments[0].ContentId = "Logo.jpg";
email.SendAndSaveCopy();
I don't find any of the answers here useful, so I am providing my solution.
The problem is that you are using multipart/related as the content type which is not good in this case. I am using multipart/mixed and inside it multipart/alternative (it works on most clients).
The message structure should be as follows:
[Headers]
Content-type:multipart/mixed; boundary="boundary1"
--boundary1
Content-type:multipart/alternative; boundary="boundary2"
--boundary2
Content-Type: text/html; charset=ISO-8859-15
Content-Transfer-Encoding: 7bit
[HTML code with a href="cid:..."]
--boundary2
Content-Type: image/png;
name="moz-screenshot.png"
Content-Transfer-Encoding: base64
Content-ID: <part1.06090408.01060107>
Content-Disposition: inline; filename="moz-screenshot.png"
[base64 image data here]
--boundary2--
--boundary1--
Then it will work
If it does not work, you may try one of these tools that convert the image to an HTML table (beware the size of your image though):
http://stylecampaign.com/blog/2009/12/bypass-image-blocking-by-converting-images-to-html/
http://neil.fraser.name/software/img2html/
I know this is an old post, but the current answers dont address the fact that outlook and many other email providers dont support inline images or CID images. The most effective way to place images in emails is to host it online and place a link to it in the email. For small email lists a public dropbox works fine. This also keeps the email size down.
Using Base64 to embed images in html is awesome. Nonetheless, please notice that base64 strings can make your email size big.
Therefore,
1) If you have many images, uploading your images to a server and loading those images from the server can make your email size smaller. (You can get a lot of free services via Google)
2) If there are just a few images in your mail, using base64 strings is definitely an awesome option.
Besides the choices provided by existing answers, you can also use a command to generate a base64 string on linux:
base64 test.jpg
For those who couldnt get one of these solutions working:
Send inline image in email
Following the steps laid out in the solution offered by #T30 i was able to get my inline image to display without being blocked by outlook (previous methods it was blocked). If you are using exchange like we are then also when doing:
service = new ExchangeService(ExchangeVersion);
service.AutodiscoverUrl("email#domain.com");
SmtpClient smtp = new SmtpClient(service.Url.Host);
you will need to pass it your exchange service url host. Other than that following this solution should allow you to easily send embedded imgages.
It may be of interest that both Outlook and Outlook Express can generate these multipart image email formats, if you insert the image files using the Insert / Picture menu function.
Obviously the email type must be set to HTML (not plain text).
Any other method (e.g. drag/drop, or any command-line invocation) results in the image(s) being sent as an attachment.
If you then send such an email to yourself, you can see how it is formatted! :)
FWIW, I am looking for a standalone windows executable which does inline images from the command line mode, but there seem to be none. It's a path which many have gone up... One can do it with say Outlook Express, by passing it an appropriately formatted .eml file.
You need 3 boundaries for inline images to be fully compliant.
Everything goes inside the multipart/mixed.
Then use the multipart/related to contain your multipart/alternative and your image attachment headers.
Lastly, include your downloadable attachments inside the last boundary of multipart/mixed.
There's actually a very good blog post that lists pro's and cons of three different approaches to this problem by Martyn Davies. You can read it at https://sendgrid.com/blog/embedding-images-emails-facts/.
I'd like to add a fourth approach using CSS background images.
Add
<div id="myImage"></div>
to your e-mail body and a css class like:
#myImage {
background-image: url('data:image/png;base64,iVBOR...[some more encoding]...rkggg==');
width: [the-actual-image-width];
height: [the-actual-image-height];
}
The following is working code with two ways of achieving this:
using System;
using Outlook = Microsoft.Office.Interop.Outlook;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
Method1();
Method2();
}
public static void Method1()
{
Outlook.Application outlookApp = new Outlook.Application();
Outlook.MailItem mailItem = outlookApp.CreateItem(Outlook.OlItemType.olMailItem);
mailItem.Subject = "This is the subject";
mailItem.To = "john#example.com";
string imageSrc = "D:\\Temp\\test.jpg"; // Change path as needed
var attachments = mailItem.Attachments;
var attachment = attachments.Add(imageSrc);
attachment.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x370E001F", "image/jpeg");
attachment.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001F", "myident"); // Image identifier found in the HTML code right after cid. Can be anything.
mailItem.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/id/{00062008-0000-0000-C000-000000000046}/8514000B", true);
// Set body format to HTML
mailItem.BodyFormat = Outlook.OlBodyFormat.olFormatHTML;
string msgHTMLBody = "<html><head></head><body>Hello,<br><br>This is a working example of embedding an image unsing C#:<br><br><img align=\"baseline\" border=\"1\" hspace=\"0\" src=\"cid:myident\" width=\"\" 600=\"\" hold=\" /> \"></img><br><br>Regards,<br>Tarik Hoshan</body></html>";
mailItem.HTMLBody = msgHTMLBody;
mailItem.Send();
}
public static void Method2()
{
// Create the Outlook application.
Outlook.Application outlookApp = new Outlook.Application();
Outlook.MailItem mailItem = (Outlook.MailItem)outlookApp.CreateItem(Outlook.OlItemType.olMailItem);
//Add an attachment.
String attachmentDisplayName = "MyAttachment";
// Attach the file to be embedded
string imageSrc = "D:\\Temp\\test.jpg"; // Change path as needed
Outlook.Attachment oAttach = mailItem.Attachments.Add(imageSrc, Outlook.OlAttachmentType.olByValue, null, attachmentDisplayName);
mailItem.Subject = "Sending an embedded image";
string imageContentid = "someimage.jpg"; // Content ID can be anything. It is referenced in the HTML body
oAttach.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001E", imageContentid);
mailItem.HTMLBody = String.Format(
"<body>Hello,<br><br>This is an example of an embedded image:<br><br><img src=\"cid:{0}\"><br><br>Regards,<br>Tarik</body>",
imageContentid);
// Add recipient
Outlook.Recipient recipient = mailItem.Recipients.Add("john#example.com");
recipient.Resolve();
// Send.
mailItem.Send();
}
}
}
One additional hint to Pavel Perna's post which helped me very much (cannot comment with my reputation, that's why I post this as answer): In some versions of Microsoft Exchange, the inline contents disposition is removed (see this post by Microsoft). The image is simply not part in the mail the user sees in Outlook. As a workaround, use "Content-Disposition: attachement" instead. Outlook 2016 won't show images as attachement that are used in the mail message, although they use the "Content-Disposition: attachement".
Try to resolve that with Context.Request:
<img width="150" height="60" src="#($"{Context.Request.Scheme}://{Context.Request.Host}{Context.Request.PathBase}/images/logo.png")" />
In my situation, when I used Content-ID I had that image as an attachment as well, and that was not the best solution.

Sending multipart email in ASP.NET with System.Net.Mail - received email has no body

I'm very confused about a problem with sending multipart emails in ASP.NET. I'm using code which I'm sure has worked before, but the received email appears to have no body.
The key bit of code is this:
message.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(plainBody, null, MediaTypeNames.Text.Plain));
message.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(htmlBody, null, MediaTypeNames.Text.Html));
I can verify that plainBody is valid text and htmlBody is valid HTML. Apart from this I'm creating a very simple MailMessage with subject, from and to. I'm not setting any other properties of MailMessage and I'm sending with the standard System.Net.Mail.SmtpClient using Google credentials I've used many times before.
When the email arrives it appears to be completely empty.
If I replace the above two lines with the following (no other changes) then the HTML email arrives correctly.
message.Body = htmlBody;
message.IsBodyHtml = true;
So what could be the cause of my empty emails? I've tried simplifying the HTML right down to a single word wrapped in <b> tags so it's not related to the body content and, as I say, I've used similar code many times before with no problems. I've tested receipt of the emails in GMail and Office 365, both with same result.
Suggestions very welcome.
Why don't you break that piece of the code into two objects.
AlternateView alternate = AlternateView.CreateAlternateViewFromString(body, mimeType);
message.AlternateViews.Add(alternate);
Put a break point in the second line and see whats in the alternate object. Maybe that might give you a little more insight into what's going on in your code.
You cannot set the mail with two different bodies. Since one of the parts is HTML, you need to treat all mail as HTML, just appending the plaintext normally - since the plaintext has no HTML encode inside, the text will appear as plaintext.

Accessing headers of body part in multipart mail in Outlook using C#

I am trying to extract the headers of the body part of a multipart mail message in Outlook. The raw message (which I have not been able to get from my code) looks something like this:
Return-Path: ...
Received: ...
From: ...
Content-Type: multipart/signed; boundary="Apple-Mail=_06FDFEBB-366E-4B1E-AA7F-F5DDEC13FD03"; protocol="application/pgp-signature"; micalg=pgp-sha512
Subject: ...
Message-Id: ...
Date: ...
To: ...
Mime-Version: ...
X-Mailer: ...
--Apple-Mail=_06FDFEBB-366E-4B1E-AA7F-F5DDEC13FD03
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
charset=us-ascii
...
--Apple-Mail=_06FDFEBB-366E-4B1E-AA7F-F5DDEC13FD03
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
filename=signature.asc
Content-Type: application/pgp-signature;
name=signature.asc
Content-Description: Message signed with OpenPGP using GPGMail
...
--Apple-Mail=_06FDFEBB-366E-4B1E-AA7F-F5DDEC13FD03--
I have replaced some of the none relevant parts by dots. The headers I am trying to get are the ones under the first boundary. So this is the part I am looking for:
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
charset=us-ascii
However, if I could get the entire part between the boundaries, that would also be fine as I could parse it myself.
So far, I have only been able to get the headers at the top of the message (so from Return-Path until X-Mailer).
I was able to do that using a `PropertyAccessor in the following way:
mailItem.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x007D001E")
In this case mailItem is my Microsoft.Office.Interop.Outlook.MailItem instance.
So, what my question comes down to: How can I get the headers under the first boundary, or any bigger part of the message that contains them?
For signed messages, Outlook preserves the signed message body (with full MIME data) in an attachment called smime.p7m (it's always called smime, even if it's actually PGP/MIME). Unfortunately, Outlook hides this from you, transparently unpacking the signed message and displaying it instead. There's no way, using the Outlook Object Model, to get the actual message body.
However, if you're willing to call MAPI directly (easiest from native code, but can be done from .NET if you're not afraid of some nasty COM interop), you can get the multipart/signed body - both the signature and the complete signed part - as follows:
Starting from the Outlook MailItem, get the MAPIOBJECT property. This is actually a MAPI IMessage. Cast it to an IMAPISecureMessage (.NET will handle this as a QueryInterface behind the scenes). Call GetBaseMessage() on this IMAPISecureMessage (the only documented function), which returns another IMessage. This is the "real" message, the one with the smime.p7m attachment. Unfortunately, there's no way to put this back into OOM, so you have to continue using MAPI. By calling the functions on IMessage, you can get the attachment, then get its data. You'll need to parse the MIME parts, at least enough to get the signed part without its headers, outer boundaries, or of course the signature part. Verify that signed part (without decoding its internal parts, if any, or decoding quoted-printable or anything like that) against the signature blob.
PR_TRANSPORT_MESSAGE_HEADERS property is the only thing you can get. Outlook does not store the full MIME source of the original message.
You can see what is available in OutlookSpy (I am its author) - click IMessage button.

System.Net.Mail creating invalid emails and eml files? Inserting extra dots in host names

It appears that .NET's SmtpClient is creating emails with an extra dot in host names if the dot was to appear at the beginning of a MIME encoded line (e.g. test.com sometimes shows up as test..com). Example code:
[TestMethod]
public void TestEmailIssue()
{
var mail = new System.Net.Mail.MailMessage();
var smtpClient = new System.Net.Mail.SmtpClient();
mail.To.Add("Test#test.com");
mail.Subject = "Test";
mail.From = new System.Net.Mail.MailAddress("test#test.com");
mail.Body = "Hello this is a short test of the issue:"
+" <a href='https://test.com/'>https://test.com/</a>: ";
smtpClient.PickupDirectoryLocation = "C:\\temp\\";
smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.SpecifiedPickupDirectory;
smtpClient.Send(mail);
}
This creates an .eml file that looks like this:
X-Sender: test#test.com
X-Receiver: Test#test.com
MIME-Version: 1.0
From: test#test.com
To: Test#test.com
Date: 6 Jul 2011 15:55:28 -0400
Subject: Test
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: quoted-printable
Hello this is a short test of the issue: https://test=
..com/'>https://test.com/:=20
When sending the file, or opening in Outlook (or any other program), the double dots show up (i.e. test..com). Note that if I remove the extra space (in "is a"), that test.com shows correctly since the dot no longer appears at the beginning of the line.
This causes a problem when trying to send website addresses, and we get calls from clients saying this they cannot click our links.
Has anyone else experienced this? How can we resolve this issue other than writing our own encoding?
This is actually per RFC 2821 (4.5.2 Transparency)
Before sending a line of mail text, the SMTP client checks the first character of the line. If it is a period, one additional period is inserted at the beginning of the line.
.Net is just storing the file in "ready to transmit" mode which means that it doesn't have to monkey with the email before sending, instead it can transmit it as is. Unfortunately this format isn't 100% the same as Outlook Express's EML format apparently. You should be able to set the encoding to UTF-8 (or something similar) and that will kick in Base-64 encoding for you.
mail.BodyEncoding = System.Text.Encoding.UTF8;
In .Net 2.0
X-Sender: test#test.com
X-Receiver: Test#test.com
MIME-Version: 1.0
From: test#test.com
To: Test#test.com
Date: 6 Jul 2011 21:29:04 +0100
Subject: Test
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: quoted-printable
Hello this is a short test of the issue: <a href=3D'https://test.com/'>https://test.com/</a>:=
It looks like it is wrapping the text at a certain character length per line. I vaguely remember there was an issue in .Net 2.0 where by default it doesn't do this which can cause problems with spam filters.
In fact increasing the size of the message gives the following in .Net 4.0:
X-Sender: test#test.com
X-Receiver: Test#test.com
MIME-Version: 1.0
From: test#test.com
To: Test#test.com
Date: 6 Jul 2011 21:34:21 +0100
Subject: Test
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: quoted-printable
Hello this is a short test of the sssssssssssssssssissue: <a hre=
f=3D'https://test.com/'>https://test.com/</a>:=20
Seems like a bug.
A workaround might be to change the BodyEncoding to something other than ASCII.
Looking at .NET 4 source code, maybe what you are experiencing has something to do with MailWriter.WriteAndFold method. Also in MailWriter class there is
static int writerDefaultLineLength = 76
variable, meaning character count per line. Maybe because you removed one extra space character, it started working.

Sending a mail as both HTML and Plain Text in .net

I'm sending mail from my C# Application, using the SmtpClient. Works great, but I have to decide if I want to send the mail as Plain Text or HTML. I wonder, is there a way to send both? I think that's called multipart.
I googled a bit, but most examples essentially did not use SmtpClient but composed the whole SMTP-Body themselves, which is a bit "scary", so I wonder if something is built in the .net Framework 3.0?
If not, is there any really well used/robust Third Party Library for sending e-Mails?
The MSDN Documentation seems to miss one thing though, I had to set the content type manually, but otherwise, it works like a charm :-)
MailMessage msg = new MailMessage(username, nu.email, subject, body);
msg.BodyEncoding = Encoding.UTF8;
msg.SubjectEncoding = Encoding.UTF8;
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(htmlContent);
htmlView.ContentType = new System.Net.Mime.ContentType("text/html");
msg.AlternateViews.Add(htmlView);
What you want to do is use the AlternateViews property on the MailMessage
http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.alternateviews.aspx
Just want to add that you can use defined constants MediaTypeNames.Text.Html and MediaTypeNames.Text.Plain instead of "text/html" and "text/plain", which is always a preferable way. It's in System.Net.Mime namespace.
So in the example above, it would be:
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(htmlContent, null, MediaTypeNames.Text.Html);
I'm just going to put a note here for anyone that's having problems and finds their way to this page - sometimes, Outlook SMTP servers will reconvert outgoing email. If you're seeing your plain-text body vanish entirely, and nothing but base64-encoded attachments, it might be because your server is reencoding the email. Google's SMTP server does not reencode email - try sending through there and see what happens.
On top of using AlternateViews views to add both the html and the plain text view, make sure you are not also setting the body of the Mail Message object.
// do not do this:
var msg = new MailMessage(model.From, model.To);
msg.Body = compiledHtml;
As it will make your email contain the html content in both views, overriding the alternative views.
For the people(like me) who've had the problem of gmail displaying the plaintext part instead of the html part.
Gmail seems to always display the last part in your message.
So if you've added the html part before your plain text part chances are gmail will always show the plain text variant.
To fix this you can simply add the plain text part before your html part.
For anyone who bumped into this issue you might want to check if you have preheader tags in your html.
In my html I've added a tag with a phrase of "Activate your client admin account by clicking the link.".
It seems like gmail is flagging the phrase "clicking the link" after removing it, all my emails that has been sent, are going straight to the inbox.

Categories