How to send web page in email body with css - c#

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

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.

SmtpClient and MailMessage - How to format for anchor tags?

I have an application that requires a user confirm their email upon registering, so I've created some functionality to send the user an email, with a link, where they can click on the link and then confirm their email.
In order to do this I'm using both MailMessage and SmtpClient classes to send the email.
Currently, my code is thus:
public ActionResult Send() {
MailMessage email = new MailMessage {
Subject = "Confirm Email",
Body = "<a href='http://www.google.com'>Google</a>",
IsBodyHtml = true
};
SmtpClient smtp = new SmtpClient();
smtp.Send(email);
return RedirectToAction("Index", "Email");
}
Now upon researching I found that the IsBodyHtml flag will help if I'm trying to send an email that has HTML contained within, although upon testing, the emails I'm receiving are not formatting anchor tags properly.
Any idea what I may be doing wrong, or how differently I can go about formatting my message body?
Thanks!
So, I made a goof here. Seems that the messages I was trying to send myself were going to Outlook's Junk Mail folder, which strips out the href value and displays it in the e-mail.
If I right click on the email and select Display HTML then the link displays as expected.
Oops!

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

C# MailMessage AlternateViews displaying HTML tags

I am using MailMessage in C# to send out a HTML email. The code I am using is as follows
MailMessage msg = new MailMessage();
AlternateView htmlView = AlternateView.CreateAlternateViewFromString("<B>Test Message</B>", null, "text/html");
msg.AlternateViews.Add(htmlView);
I use the Alternate view because I need to attach files and embed images in the email body. When I send out the email to my gmail account, I see the HTML tags displayed in the inbox. Clicking on the message to view the actual email gets rid of the tags. How do I ensure that the tags do not display in the inbox?
Thanks
I solved the issue. I am posting the solution as an answer to my own question to help others who may run into the same issue
My code had
MailMessage msg = new MailMessage();
msg.Body = "<B>Test Message</B>";
AlternateView htmlView = AlternateView.CreateAlternateViewFromString("<B>Test Message</B>", null, "text/html");
msg.AlternateViews.Add(htmlView);
The second line of code needed to be removed since I was specifying both a body and an alternate view which was causing problems.
Does the message itself need to be marked around isBodyHtml ?
msg.IsBodyHtml = true;

Categories

Resources