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

  • Thread starter Thread starter Jeff
  • Start date Start date
J

Jeff

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("(e-mail address removed)", "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?
 
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("(e-mail address removed)", "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
 
Back
Top