2

I’m sending mail in asp like this:

    MailMessage msg = new MailMessage();

    msg.From = new MailAddress("from@email", "From Name");
    msg.Subject = "Subject";
    msg.To.Add("to@email");
    msg.BodyEncoding = Encoding.UTF8;

    String plainBody = "Body of plain email";

    AlternateView plainView = AlternateView.CreateAlternateViewFromString(plainBody, Encoding.UTF8, "text/plain");
    msg.AlternateViews.Add(plainView);

    //now create the HTML version
    MailDefinition message = new MailDefinition();
    message.BodyFileName = "~/MailTemplates/template1.htm";
    message.IsBodyHtml = true;
    message.From = "from@email";
    message.Subject = "Subject";

    ListDictionary replacements = new ListDictionary();
    replacements.Add("<%USERNAME%>", "ToUsername");//example of dynamic content for Username
    MailMessage msgHtml = message.CreateMailMessage("to@email", replacements, new LiteralControl  ());

    AlternateView htmlView = AlternateView.CreateAlternateViewFromString(msgHtml.Body, Encoding.UTF8, "text/html");

    LinkedResource imgRes = new LinkedResource(Server.MapPath("~/MailTemplates/images/imgA.jpg"), System.Net.Mime.MediaTypeNames.Image.Jpeg);
    imgRes.ContentId = "imgA";
    imgRes.ContentType.Name = "imgA.jpg";
    imgRes.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;
    htmlView.LinkedResources.Add(imgRes);

    msg.AlternateViews.Add(htmlView);

    //sending prepared email
    SmtpClient smtp = new SmtpClient();//It reads the SMPT params from Web.config
    smtp.Send(msg);

It's ok.

I should change mail format and but how I add html file in new format??

New format:

   SmtpClient oSmtp = new SmtpClient();
        SmtpMail oMail = new SmtpMail("TryIt"); //should be TryIt
        SmtpServer oServer = new SmtpServer("mail.ir");
        oServer.Port = 465;
        oServer.ConnectType = SmtpConnectType.ConnectSSLAuto;

        oMail.From = "";
        oServer.User = "...@...";
        oServer.Password = "123";
        oMail.To = "@";

oMail.Subject = ;
        oMail.HtmlBody =;

        oSmtp.SendMail(oServer, oMail);

I don’t want use html inside code, I’d like design a beautiful html file and send it.

M4N
  • 94,805
  • 45
  • 217
  • 260
NASRIN
  • 475
  • 7
  • 22

1 Answers1

1

You can read the HTML file using the following line of code...and then pass text to your email object.

using system.IO;


string text = System.IO.File.ReadAllText(@"C:\Users\Public\TestFolder\WriteText.html");


oMail.HtmlBody = text;
highwingers
  • 1,649
  • 4
  • 21
  • 39
  • I want pass some parameters to html file such as username. How I pass parameters? – NASRIN Jun 03 '13 at 06:56
  • why cant you just create a Function...... string myFunction(user, pwd) return text; do all the processing withing function and then call it in your email class. oMail.HtmlBody = myFunction("my user name", "my pwd"); – highwingers Jun 03 '13 at 07:03
  • You can create and use a specific expression in your HTML file like (###expression###) and in your C# code you use de String.Replace method to replace the value (text.Replace("###expression###", username)). – Anderson Rissardi Dec 06 '16 at 16:40