Sending mail with embedded image from WCF service - c#

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.

Related

C# MailKit HTML email received as plain text

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.

Add Email address in to field of Email App using share contract?

I am creating a sample Win8 app and using share contract I am trying to share HTML Content. When the user select email app from the share application option I want to set email address in TO field. How can I do so? Following is my code written to share HTML content:
Code:
DataPackage requestData = request.Data;
requestData.Properties.Title = this.PageViewModel.JobInformationDetail.JobNumber;
requestData.Properties.Description = this.PageViewModel.JobInformationDetail.CustomerSignatureName;
//requestData.SetText("Sample Text");
StorageFile signatureStream = await GetInkManagerStream();
requestData.SetHtmlFormat(Windows.ApplicationModel.DataTransfer.HtmlFormatHelper.CreateHtmlFormat(this.GetMailDescription()));
Following images shows where to set the email address in To field:
You can't do that. We'll have a solution to this until WinRT come up with a standard email DataPackage format to StandardDataFormats and support it in the email app.
I think Microsoft's default Mail should be upgraded. It's entirely depend upon sharing target app to show particular data to particular place. DataPackage's Title property is set as subject and Description property is set as mail body but there's no provsion for to email(s).
If sharing experience is best one then user will user default Mail app otherwise they will go for another mail apps.

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.

How to send web page in email body with css

I am creating a report on an asp.net web page using an html table and asp.net lables. The finished report I have to send by email in the message body. I've done this with the following c# code:
public bool SendEMail(List<string> emailList, string strSubject, string strMessage, bool isHTML)
{
MailMessage msg = new MailMessage();
msg.From = new MailAddress(strFrom);
//emailList is a list of email addresses to be sent to
if (emailList != null && emailList.Count > 0)
foreach (string em in emailList)
{
msg.To.Add(em);
}
else
return false;
msg.Subject = strSubject;
msg.Body = strMessage;
msg.IsBodyHtml = isHTML;
SmtpClient smtp = new SmtpClient(mailServer);
smtp.Credentials = new System.Net.NetworkCredential(userName, usePass);
smtp.Send(msg);
msg.Dispose();
return true;
}
This works well but it only gets styles set within the form itself on each control individually. How can I incorperate css set in the html head or in a style sheet?
Also is it possible to include skins?
Take a look to this chart :
http://www.campaignmonitor.com/css/
I would recommend you to use inline styles instead of adding an external css sheet
styling html emails is a pain in the ass, with each client (gmail/hotmail/outlook/yahoo) applying their own styles to certain high level elements.
a good rule of thumb is to apply inline styles for example:
<span style="display:block; background:red;">blah</span>
have a look at campaign monitor to see which css rules work and litmus if you wish to take the pain out of the testing
This can be done just the same way you'd set the css on a web page. In the message body, you can use a fully formed html document including head tags which can link to an external stylesheet. As long as the css is contained completely within the document, or a full URL is used in the link, it should be fine.
Look at the implementation using AlternateViews, this will help you, if you are dynamically generating email body, with styles.
http://microsoft.com/....alterviews.aspx

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