Monday, December 14, 2015

How to send email from c# .net

hi everyone , this article having the example for sending the email from the coding ............

Step 1 . first of all we have to define the namespace System.Web and System.Web.Mail or System.Net and System.Net.Mail

Step 2 . create a public class for sending the email having the attributes from email, to email and cc email if having.

Example 1 : Send Email using the namespace System.Net.Mail

              public class EmailExample()
             {
                    System.Net.Mail.MailAddress from = new System.Net.Mail.MailAddress();
                    System.Net.Mail.MailAddress to = new System.Net.Mail.MailAddress();
                    System.Net.Mail.MailAddress cc = new System.Net.Mail.MailAddress();
                 
                    System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
                    mail.CC.Add(cc);  /////this is optional
                    mail.Subject ="Enter Your mail suject here";
                    mail.Subject ="Enter Your mail body here";
                    mail.IsBodyHtml =true; /// if you use your email as html

                    System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient(); // your smtp
                    control to configure mail
                   smtp.Host = "enter your smtp host/exchange server";
                   smtp.Port = 25;
                   smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
     
                  smtp.EnableSsl = false;///if you are not using the default credentials
                  smtp.Credentials = new System.Net.NetworkCredential("Username",    
                  "Password");

                 smtp.Send(mail);
     
                 return true;
             }

Example 2 : Send Email using the namespace System.Web.Mail

              public class EmailExample()
             {                
                    System.Web.Mail.MailMessage mail = new System.Web.Mail.MailMessage();
                    mail.From = "XXX@yyy.com";
                    mail.To = "ZZZ@yyy.com";
                    mail.CC = "XXX@yyy.com"; ///optional
                    mail.Subject ="Enter Your mail suject here";
                    mail.Subject ="Enter Your mail body here";

                    mail.IsBodyHtml =true; /// if you use your email as html

                    System.Web.Mail.SmtpClient smtp = new System.Web.Mail.SmtpClient(); // your smtp
                    control to configure mail
                   smtp.SmtpSever = "enter your smtp host/exchange server";
                   smtp.Send(mail);
     
                   return true;
             }

After creating the class, use this class in your code.

i hope this is enough to send the mail from the intranet within the domain and outside the domain.


1 comment: