newbie: how to send html mail in asp.net 3.5

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

  1. Jeff

    Jeff Guest

    hi

    asp.net 3.5

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

    #Question 1:
    Can the templatefile be an .aspx webpage, suppose I have this webpage
    ~/Email/ReportTemplate.aspx?,
    (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 http://forums.asp.net/t/1387177.aspx 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
    hh:mmtt"));
    placeholder.Add("<%CaseNumber%>", caseNumber.Next(10000, 99999).ToString());
    placeholder.Add("<%Website%>", "xyz.com");
    MailMessage msg = md.CreateMailMessage("SomeUserEmailAddress", placeholder,
    this);
    - why does "MailMessage msg = md.CreateMailMessage("SomeUserEmailAddress",
    placeholder, this); " contain "this" as a paramter?
     
    Jeff, May 3, 2009
    #1
    1. Advertising

  2. On May 3, 2:02 pm, "Jeff" <> wrote:
    > hi
    >
    > asp.net 3.5
    >
    > I'm wonder how to send html mail in asp.net 3.5 where the body is based on a
    > templatefile.
    >
    > #Question 1:
    > Can the templatefile be an .aspx webpage, suppose I have this webpage
    > ~/Email/ReportTemplate.aspx?,
    > (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:
    > Athttp://forums.asp.net/t/1387177.aspxI 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
    > hh:mmtt"));
    > placeholder.Add("<%CaseNumber%>", caseNumber.Next(10000, 99999).ToString());
    > placeholder.Add("<%Website%>", "xyz.com");
    > MailMessage msg = md.CreateMailMessage("SomeUserEmailAddress", placeholder,
    > this);
    > - why does "MailMessage msg = md.CreateMailMessage("SomeUserEmailAddress",
    > placeholder, this); " contain "this" as a paramter?


    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
    client.Send(msg)

    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.

    http://msdn.microsoft.com/en-us/library/0002kwb2.aspx
     
    Alexey Smirnov, May 4, 2009
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Sutter
    Replies:
    0
    Views:
    639
    John Sutter
    Jan 13, 2004
  2. refv8
    Replies:
    2
    Views:
    27,076
    ittower
    Oct 13, 2006
  3. ja
    Replies:
    0
    Views:
    382
  4. Mike P
    Replies:
    1
    Views:
    567
    =?ISO-8859-1?Q?G=F6ran_Andersson?=
    Mar 1, 2007
  5. Ramon
    Replies:
    2
    Views:
    1,013
    Ramon
    Oct 25, 2007
Loading...

Share This Page