1

i am able to send and receive emails through my application, but its jus plain text content. I want to send full formatted content decorated with css and div or tables. How to do it? I am using asp.net 3.5 VS2010.

public void Send(string To, string Subject, string Body)
{
    System.Net.Mail.MailMessage m = new System.Net.Mail.MailMessage("mymail@gmail.com", To);
    m.Subject = Subject;
    m.Body = Body;
    m.IsBodyHtml = true;
    m.From = new MailAddress("mymail@gmail.com");

    m.To.Add(new MailAddress(To));
    SmtpClient smtp = new SmtpClient();
    smtp.Host = "198.208.0.***";


    NetworkCredential authinfo = new NetworkCredential("mymail@gmail.com", "password");
    smtp.UseDefaultCredentials = false;
    smtp.Credentials = authinfo;
    smtp.Send(m);
}

calling this function like
Send("mynewmail@gmail.com", "testmail", "new test body"); this is my function.

Sushant
  • 391
  • 12
  • 28

4 Answers4

6

The answer is here : http://social.msdn.microsoft.com/Forums/en/netfxnetcom/thread/c65502fa-955e-4fa1-b409-238bbb677df7

I think the main thing you're missing is the MIME Content Type on the LinkedResource. myMagic.css:

   body {
      background-color: rgb(240,240,240);
      font-family: Verdana,Arial,Helvetica,Sans-serif;
      font-size: 10pt;
   }
   h2 {
      background-color: rgb(255,255,128);
      color: rgb(255,0,0);
   }
   p {
      background-color: rgb(0,0,255);
      color: rgb(0,255,0);
      font-style: italic;
   }

Program.cs:

using System.Net.Mail;
using System.Net.Mime;

namespace MailMessageHTML
{
    class Program
    {

        private static MailMessage ConstructMessage(string from, string to, string subject)
        {
            const string textPlainContent =@"You need a HTML-capable mail agent to read this message.";
            const string textHtmlContent =@"<html><head><link rel='stylesheet' type='text/css' href='cid:myMagicStyle' />
</head>
<body>
<h2>Hello world!</h2>
<p>This is a test HTML e-mail message.</p>
</body>
</html>
";

   MailMessage result = new MailMessage(from, to, subject, textPlainContent);
   LinkedResource cssResource = new LinkedResource("myMagic.css", "text/css");
   //NOTE: Message encoding adds the surrounding <> on this Id cssResource.ContentId =myMagicStyle";
   cssResource.TransferEncoding = TransferEncoding.SevenBit;

      AlternateView htmlBody = AlternateView.CreateAlternateViewFromString( textHtmlContent , new ContentType("text/html"));
            htmlBody.TransferEncoding = TransferEncoding.SevenBit;
            htmlBody.LinkedResources.Add(cssResource);

            result.AlternateViews.Add(htmlBody);

            return result;
        }

        static void Main(string[] args)
        {
            MailMessage foo = ConstructMessage(
        "sender@foo.com"
                ,"recipient@bar.com"
                , "Test HTML message with style."
                );
            SmtpClient sender = new SmtpClient();
            sender.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
            sender.PickupDirectoryLocation = @"...\MailMessageHTML\bin\Debug\";
            sender.Send(foo);
        }
    }
}

NOTE 1: I've specified TransferEncoding.SevenBit on the message parts above. You wouldn't normally do this in a production environment - I've only done it here so you can read the resultant .eml file with Notepad to see what was generated.

NOTE 2: A lot of mail agents won't honour stylesheets linked in message parts. A more-reliable way might be to use inline styles (i.e.: inline ... blocks within the HTML message body).

Good luck,

This answer got from : http://social.msdn.microsoft.com/Forums/en/netfxnetcom/thread/c65502fa-955e-4fa1-b409-238bbb677df7

lboshuizen
  • 2,746
  • 17
  • 20
Bashar Abu Shamaa
  • 1,998
  • 2
  • 21
  • 36
0

If you don't need dynamic content (if/else or foreach) or model binding. I think put your template into a text file and use string.Format() is sufficient.

Otherwise, you'll need a template engine. This question may be a good reference for you.

Community
  • 1
  • 1
Chris Li
  • 3,715
  • 3
  • 29
  • 31
0

Have a look at MvcMailer on github

It uses a viewengine to render emails so you have all the benefits of viewmodels and templating.

lboshuizen
  • 2,746
  • 17
  • 20
0

Have a look at http://www.campaignmonitor.com/css/ which shows how to apply CSS to emails.

Also, use inline CSS.. And are you setting emailMessage.IsBodyHtml = true; ?

Thomas
  • 1,563
  • 3
  • 17
  • 37