C# MailKit HTML email received as plain text - c#

I have a complex email template that contains many div, section and other HTML elements. The HTML template has reference to CSS(uploaded to server). I am using the below code to send HTML email via MailKit :
var message = new MimeMessage();
var bodyBuilder = new BodyBuilder();
bodyBuilder.HtmlBody = File.ReadAllText(pathToHtmlFIle);
message.Body = bodyBuilder.ToMessageBody();
client.Send(message);
But the client only receives everything in plain-text, no colors, no formatting. Is this the expected result ?

I think you need to use either inline CSS or CSS embedded in the head section. Since most webmail clients block links to external stylesheets, it is rare to see this method employed in an email.

After struggling a lot with this topic, I finally figure out the error was the double quotes inside the html...Use notepad++ to replace (quick launched with ctrl+f) all " for '. Holy remedy, after only receiving plain/text I finally received the text/html.
PD: Do not use bootstrap nor try to link an external source 'cause mail clients block external css providers. Use strictly style attributes for all tags.

Related

EWS Managed API 2.0 message encoding

I have a question bout the encoding of email messages when sending via EWS Managed API 2.
I haven't been able to find any clear answer to this on the MSDN pages so I'll try you guys.
When using the standard .NET SmtpClient I can set the encoding of both the body and the subject attribute (I need utf-8) - this doesn't seem to be the case when using EWS Managed API 2.
Or, is it in fact the MimeContent attribute of the EmailMessage that is used for this and if so, how do I do the same thing for the subject of the email message?
For the body - is this the correct way to do it?
EmailMessage.MimeContent =
new MimeContent("utf-8", Encoding.UTF8.GetBytes("<body text html or plain text>"));
And then I don't set the EmailMessage.Body attribute or?
Never mind, it appears that I don't have the specify utf-8. I have now tried to set the body attribute with both text and HTML that included special characters and in both cases the result looked correct in the mail.
EmailMessage.Body = new MessageBody(BodyType.HTML, "<html content>");
EmailMessage.Body = new MessageBody(BodyType.Text, "plain text content>");

Sending mail with embedded image from WCF service

I have a WCF service which sends email to my clients. That email body is html. So it has CSS styles and embedded image. but now the problem is that the image src has to be given as the full address. So is there any option to expose the image also as part of the wcf service and use that full address.
I dont want to use likedResource option because i am using a open source email templating engine
Town Crier – An open-source e-mail templating engine for .NET
http://thecodedecanter.wordpress.com/2010/07/19/town-crier-an-open-source-e-mail-templating-engine-for-net/
this engine needs images to be given as a full address.
Any better solution would be greatly appreciated.
Thanks,
Alagesan.
If the images are static and not too large, you can include them in the message itself using LinkedResources property of an AlternateView from the message.
AlternateView html = message.AlternateViews.FirstOrDefault(v => v.ContentType.Name == "text/html");
if (html != null)
{
LinkeResource img = new LinkedResource(imgFileName, imgMimeType);
img.ContentId = imgContentName;
html.AddLinkedResources.Add(img);
}
The img.ContentId is then referenced in the message using "cid:ContentId" URL syntax. So if you gave the image a ContentId of "header.jpg" then you can reference it in the HTML email using: <img src="cid:header.jpg" />
Handy for inserting small logos and such however.

Html IFrame tag is not being interpret in email body

I need to send a html file(which contains a iframe) inside a email body.The html file is working fine in a browser and playing a video.But when i send it inside email body,iframe tag is not getting interpreted so does not show in the body.
This is html file.
<b>Aman</b>
<iframe height="390" frameborder="0" width="640"
src="http://www.youtube.com/embed/Sf5T5KjMpJU?wmode=transparent"
title="YouTube video player"></iframe>
Email body only displaying a "Aman" in bold.This is C# code.
StreamReader reader = File.OpenText("C:\\Users\\Girish\\Desktop\\amrit\\Jeff_Project\\indeex.html");
string getemail = textbox_email.Text;
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.To.Add(getemail);
message.Subject = "Hello";
message.From = new System.Net.Mail.MailAddress("sendingemail");
//message.Body = "This is message body";
message.IsBodyHtml = true;
message.Body = reader.ReadToEnd();
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("smtp.gmail.com");
smtp.Credentials = new System.Net.NetworkCredential("sendingemail", "password");
smtp.EnableSsl = true;
smtp.Send(message);
Response.Write("Sent");
Why iframe is not being interpret?Am i missing anything?
Please help and provide solutions.
Thanks in advance.
emails doesn't support objects tags inside them.
read this
I even tried to send myself a youtube video from youtube, and even them not embedding the video in side the email body.
Instead of trying to embed the video as a link (like youtube does)
most email clients only support very basic html. to be safest, we have to generate our newsletter content using table layouts and just simple a, span, and img tags for content.
if you try to use divs for layouts, outlook clients will barf when trying to render them. this is because outlook uses microsoft word to render html documents. as a general rule, we always test layouts in microsoft outlook because that client tends to be the lowest common denominator. if it looks good in outlook, it'll generally look good everywhere else.

C# HTML E-Mail - Alternate View

I am having an issue testing alternate views while sending HTML E-Mails. I am creating sending the mail message as plain text, but I include an HTML alternate view. I have tried using several e-mail clients, but I have been unable to see the plain text version. My main concern is that someone who does use a plain text only client won't see it correctly. See below for code:
MailMessage message = new MailMessage();
message.To.Add("email");
message.From = new MailAddress("fromaddress");
message.Subject = "subject"
//Plain text version of e-mail
message.Body = _formattedPlainText;
message.IsBodyHtml = false;
AlternateView htmlView = CreateHTMLView();
message.AlternateViews.Add(htmlView);
//message.AlternateViews.Add(plainTextView);
smtp.Send(message);
private AlternateView CreateHTMLView()
{
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(_formattedHTML, null, System.Net.Mime.MediaTypeNames.Text.Html);
//Code for adding embedded images...
return htmlView;
}
Is there reason to believe the plain text version isn't being received or are there any clients you know that definitely can only receive plain text e-mails?
Thanks in advance
UPDATE:
You can force plain text in GMAIL by pressing the options arrow and selecting the option "Message Text Garbled?"
Is there reason to believe the plain text version isn't being received
or are there any clients you know that definitely can only receive
plain text e-mails?
No, and PINE.
You could use the mail command in any *nix OS to read email. It will only display email in text format.
UPDATE
A guy here, claims that GMAIL ONLY displays emails in plain text if given the option. I find that an awkward default choice from Google if that's still the case.
Through some experimentation I have had best results this way:
Set up two alternate views, one HTML and one plain text.
Then, leave the MailMessage.Body alone (default/undefined) and
leave the MailMessage.IsBodyHtml at default. My code segment:
myMessage.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(HtmlMessage, new System.Net.Mime.ContentType("text/html")));
myMessage.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(TextMessage, new System.Net.Mime.ContentType("text/plain")));
// myMessage.Body = HtmlMessage;
// myMessage.IsBodyHtml = true;
myClient.UseDefaultCredentials = false;
NetworkCredential credentials = new NetworkCredential("xxxx","xxxxx");
myClient.Credentials = credentials;
myClient.DeliveryMethod = SmtpDeliveryMethod.Network;
I have tested this with messages sent to Windows Outlook clients and to Mac users, and it seems to work just fine. The email client software chooses the view. I still need to test how this works for clients such as webmail apps. Your mileage may vary.
Bob

How can I lower the spam score of my email message?

I am sending a new logon and password to a user, however when I do on a test version of our site on the internet the Spam score is 4.6 by spam assassin. Which means it gets trapped.
The Email is HTML (so the marketing dept have their nice fonts and colours) with a linked image.
The MailMessage() object does not appear to give me a lot of control over the output format of the message.
What measures could I take to lower the spam score?
I am sending using this:
/* send an email */
MailMessage msg = new MailMessage();
msg.IsBodyHtml = true;
//msg.BodyEncoding = Encoding.UTF8;
msg.To.Add(new MailAddress(sToEmail));
msg.From = new MailAddress(sFromEmail);
msg.Subject = sEmailSubject;
msg.Body = sEmailTemplate;
try
{
client.Send(msg);
}
The spam score is this:
X-Spam-Score: 4.6 (++++)
X-Spam-Report: Spam detection software report (4.6 points):
pts rule name description
---- ---------------------- --------------------------------------------------
1.8 HTML_IMAGE_ONLY_20 BODY: HTML: images with 1600-2000 bytes of words
0.0 HTML_MESSAGE BODY: HTML included in message
1.7 MIME_HTML_ONLY BODY: Message only has text/html MIME parts
1.1 HTML_MIME_NO_HTML_TAG HTML-only message, but there is no HTML tag
0.1 RDNS_NONE Delivered to trusted network by a host with no rDNS
Two solutions:
Add more content, so that the <img> is not the main part of the email - loads more content in clean text without tags. (I know it looks lame, but copyright notices, unsubscribe instructions and registration rules make a really good text padding) Add a text-only version in a new mime part. Send a properly constructed HTML which actually contains the <html> tag.
Smack marketing people with a clue-by-four many times and send text emails in text only - as $DEITY intended.
Using the AlternateView class, you can specify a text/plain body and provide an alternate html body for the marketing boys. Even if the text part only says that you should have an html enable reader, the spam filter will drop 1.8 points.
Then if you start the HTML message with a proper tag (just take a full html page), you will drop 2.8 poins.
You can also include LinkedResources so you can send the image without showing attachments, much nicer.
It's already telling you what to do, but I'll spell it out for you:
Include more text or less images.
Nothing you can do here if you want HTML. It's not weighted on the default SpamAssassin install anyway, though.
Add in a text version of the content in addition to the HTML version.
Add in the missing <html> tag
Set up reverse DNS for your outgoing mail server's IP.
Steps 3 and 4 are probably the most important to do. 1 is out of your control (marketing is in control of that). 5 would help, but it's rated fairly low.
It seems like it doesn't matter how you send the message that effects the spam score, but what the message contains.
Try different versions of the message contents and see what else can change.
1.8 points seems to be from the images. Take out the images.
How are you creating the HTML? I would look at all those factors before I would look at changing how the message is sent, because that is not a factor in spam.
1.1 HTML_MIME_NO_HTML_TAG HTML-only message, but there is no HTML tag
Well for one, you need an HTML tag around your HTML message. If you make your HTML validate, it seems like it'd lower the score a bit. That'd knock you down to 3.5 points.
Don't forget a nice friendly name in your from address too, that's making our emails get caught up in filters.
email from:
J Random Hacker <jrandomhacker#example.com>
is better than jrandomhacker#example.com
If you've simply got an html link to a picture, then it looks like spam, and people who's email clients block images by default (most online ones do) won't be able to see your message.
Rather than have one big image, try breaking it up and use html tables to lay it out. Also, make sure you set the alt attribute on the img tags.
The other thing, apart from the spam assasin score, to look at, is making sure you've set up Sender Policy Framework for the domain from which you're sending the emails. Some online email providers do not score spam on content at all, rather they use SPF and user's "reporting spam", so make sure you get this set up and working correctly before you do large broadcasts.
You might also choose to use a service such as the excellent campaign monitor instead of writing your own broadcast client. They can guide you through the process of setting up the DNS entries required for SPF, and also provide tracking of people opening the email and following links within the email.
You are answering your own question. By not sending "images with 1600-2000 bytes of words", "HTML included in message", "Message only has text/html MIME parts" or "HTML-only message, but there is no HTML tag" you will substract spam points from the formula and hence the result will be lower.
An (inferior) alternative is to ask the user to whitelist you.
I don't know much about Spam Assasin, but I've used Return Path in the past. They give a rather comprehensive view of the aspects of an email that make it look like spam.
I don't work for Return Path, btw :)
Nothing you can do here if you want HTML. It's not weighted on the default SpamAssassin install anyway, though.
Add in a text version of the content in addition to the HTML version.
Add in the missing tag
alter sol to above : Do HTML encode base64 method , not expose html headers in content can significant reduce level of spam score via filters. :)

Categories

Resources