• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 448
  • Last Modified:

Dynamic content for Email

How to embedd a dynamic content into a email?


Now for each email i send out , i have to write the HTML body tag and create the HTML content like :
 
MailMessage msg = new MailMessage();  
 
msg.HTMLMessage.Body ="<html> the Whole email content here </html"; ...etc and finally

msg.send();

Is there a way for me to write the HTML content somewhere and retrieve here?I want the most efficient method avalilable...

Thanks
dee
0
pradeepa_np
Asked:
pradeepa_np
  • 2
1 Solution
 
rubixxcubeCommented:
You can read the HTML in from an html file using a streamreader.
Here is a real simple example that worked for me:

                                                MailMessage oMessage=new MailMessage();
                  oMessage.To = "Abrancato@juniper.com";
                  oMessage.From = "abrancato@juniper.com";
                  oMessage.Subject = "testing htmlmail";
                                      oMessage.BodyFormat = MailFormat.Html;
                  System.Text.StringBuilder sb=new System.Text.StringBuilder();
                  StreamReader oStream=new StreamReader("C:\\my documents\\test.html");
                  string txtline;
                  while((txtline=oStream.ReadLine())!=null)
                  {
                        sb.Append(txtline);
                  }
                  oMessage.Body=sb.ToString();
                  SmtpMail.SmtpServer = "mail";
                  SmtpMail.Send(oMessage);
Hope this helps
Ruby
0
 
pradeepa_npAuthor Commented:
But what if I have some Parameters that i have to send to this HTML pages that replaces certain values in the page
For example if the HTML page requires a constant {Name}
I have to pass from here to HTML page
I guess i should make the page as aspx???

I am beginner do clarify me on this.

Thanks
dee
0
 
rubixxcubeCommented:
Try this, use a string.Replace to replace values in the text.
I modified the code to show you:

MailMessage oMessage=new MailMessage();
                  oMessage.To = "Abrancato@juniper.com";
                  oMessage.From = "abrancato@juniper.com";
                  oMessage.Subject = "testing htmlmail";
              oMessage.BodyFormat = MailFormat.Html;
                  System.Text.StringBuilder sb=new System.Text.StringBuilder();
                  StreamReader oStream=new StreamReader("C:\\my documents\\test.html");
                  string txtline;
                  while((txtline=oStream.ReadLine())!=null)
                  {
                        sb.Append(txtline);
                  }
                                                //replace variable names with their values
                  string bodytext=sb.ToString();
                  bodytext=bodytext.Replace(constantVariableName,ConstantVariableValue);
                  oMessage.Body=bodytext;
                  SmtpMail.SmtpServer = "mail";
                  SmtpMail.Send(oMessage);
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now