newbie: how to send html mail in 3.5

Discussion in 'ASP .Net' started by Jeff, May 3, 2009.

  1. Jeff

    Jeff Guest

    hi 3.5

    I'm wonder how to send html mail in 3.5 where the body is based on a

    #Question 1:
    Can the templatefile be an .aspx webpage, suppose I have this webpage
    (if possible, then how is it done? just a short example or link and I can
    think of the rest myself)

    #Question 2:
    Do I need to do something special to incorperate graphics in the email?

    #Question 3:
    At I found this code:
    Random caseNumber = new Random();
    NetworkCredential loginInfo = new
    NetworkCredential("", "YourGmailPassword");
    MailDefinition md = new MailDefinition();
    md.From = "SomeEmailAccount";
    md.Subject = "Your Support Case";
    md.BodyFileName = "~/EmailTemplates/email.txt";
    md.IsBodyHtml = true;
    ListDictionary placeholder = new ListDictionary();
    placeholder.Add("<%DateTime%>", DateTime.Now.ToString("MMM dd, yyyy
    placeholder.Add("<%CaseNumber%>", caseNumber.Next(10000, 99999).ToString());
    placeholder.Add("<%Website%>", "");
    MailMessage msg = md.CreateMailMessage("SomeUserEmailAddress", placeholder,
    - why does "MailMessage msg = md.CreateMailMessage("SomeUserEmailAddress",
    placeholder, this); " contain "this" as a paramter?
    Jeff, May 3, 2009
    1. Advertisements

  2. 1) You need to pass the entire code for your email body. If templates
    stored in different files, you can have something like the following
    code to open specific file and send it to MailMessage

    msg = New System.Net.Mail.MailMessage()
    msg.IsBodyHtml = True
    oRead = System.IO.File.OpenText(ApplicationPath & sFileNameOfTemplate)
    EntireFile = oRead.ReadToEnd()
    msg.Body = EntireFile

    or use CreateMailMessage method to create a new e-mail message from a
    text file. That method has replacements parameter where you can set
    all strings to replace by new values.

    System.Net.Mail.MailMessage fileMsg;
    ListDictionary replacements = new ListDictionary();
    replacements.Add("<%To%>", ......);
    replacements.Add("<%From%>", .......);
    fileMsg = md.CreateMailMessage(sourceTo.Text, replacements, this);

    2) if your graphic is located on a server, you need to include the
    link like <img src="http://..../picture1.jpg">

    3) "this" is the value for owner parameter in CreateMailMessage
    method. It tells to the method that the Control that owns this
    MailDefinition is the current web form. It determines which directory
    to search for the text file specified in the BodyFileName property.
    Alexey Smirnov, May 4, 2009
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.