The intent of this blog is to enable you to send E-mails leveraging all the features available in .Net Framework. Probably, if you already know how to send an E-mail in .Net then hold on and keep reading because you'll learn some other not-so-usually-known aspects/properties while sending an E-mail. You should be having your IIS configured with your SMTP details on your web-server OR should mention them explicitly in the [web.config] file before sending your first E-Mail. The former option is possible only in fraction of the cases since developers do not have access to the IIS Console on the web-server and hence the latter is the only option which a majority of developers use. The [web.config] method to declare the SMTP details has been included in this chapter. Note that the third option of declaring the SMTP details explicitly in the CODE is also available but as such it is vulnerable to exploits and it also contradicts the very concept of ENCAPSULATION.
The classes and methods for sending an E-mail are defined in the "System.Net.Mail" namespace and are accessible in both Windows and Web Forms(i.e. ASP.Net). The three main classes in use from the namespace while sending an E-mail are the MailAddress, MailMessage and SmtpClient.
The classes and methods for sending an E-mail are defined in the "System.Net.Mail" namespace and are accessible in both Windows and Web Forms(i.e. ASP.Net). The three main classes in use from the namespace while sending an E-mail are the MailAddress, MailMessage and SmtpClient.
The following are the attributes which will come into play while sending an E-Mail :
- To address
- CC address
- Bcc address
- From address
- Body of the mail
- Subject of the mail
- Priority of the mail
- Attachments (if any)
- Does the body have HTML(as in case of Newsletters) ?
- ReplyTo Address
Make sure the the requisite namespace [System.Net.Mail] is referenced in your code-behind.
CODE Snippet :
- try
- {
- MailAddress to = new MailAddress("reciever@microsoft.com", "Mr. RECEIVER");
- MailAddress from = new MailAddress("sender@microsoft.com", "Mr. SENDER");
- MailAddress cc = new MailAddress("cc@microsoft.com", "Mr. CC");
- MailAddress bcc = new MailAddress("bcc@microsoft.com", "Mr. Bcc");
- MailAddress rto = new MailAddress("donotreply@microsoft.com", "DO NOT REPLY");
- MailMessage mm = new MailMessage(to, from);
- mm.CC.Add(cc);
- mm.Bcc.Add(bcc);
- mm.Subject = "";
- mm.Body = "";
- mm.ReplyTo = rto; //Specifies the E-Mail address of the SENDER which will be seen if the RECEIVER decides to reply to the mail
- mm.IsBodyHtml = true; // "true" if your body has some Html content. DEFAULT is "false".
- mm.Priority = MailPriority.Normal; //Possible values are "High/Normal/Low" depending upon the priority required. DEFAULT is "Normal"
- mm.Attachments.Add(new Attachment(Server.MapPath("./files/image.jpg")));
- SmtpClient sc = new SmtpClient();
- sc.Send(mm);
- }
- catch (Exception ex)
- {
- Response.Write("Exception : " + ex);
- }
[web.config] CODE Snippet :
You can also store some requisite variables like the recipient's address, sender's address, signature or say any values in [web.config] for access at runtime.
[web.config] CODE Snippet :
To access this global variable from CODE-BEHIND:
- String from = System.Configuration.ConfigurationSettings.AppSettings.("FromAddress");
Post a Comment
0 comments:
Post a Comment