Monday, 13 July 2015

To check mail functionality


Steps of Sending mail
  1.  Create object of MailMessage Class "oMsg".
  2. Assign "From email" address to oMsg.From.
  3. Assign "To email address" to oMsg.To.
  4. Assign Subject to oMsg.Subject.
  5. Assign body text to oMsg.Body.
  6. Assign Subject Encoding to  oMsg.SubjectEncoding.
  7. Assign Body Encoding to oMsg.BodyEncoding.  
  8. Assign Isbodyhtml bool variable value to oMsg.IsBodyHtml.
  9.  Assign priority to oMsg.Priority.
  10. Create SMTP Client object.
  11. Give SMTP credentials in the constructor as parameters.
  12. Assign Delivery method to oSmtp.DeliveryMethod.
  13. Assign SSL bool variable to oSmtp.EnableSsl.
  14. Create object of NetworkCredential class.
  15. Pass SMTP user name and password as constructor parameters.
  16. Assign "UseDefaultCredentials" bool value to oSmtp.UseDefaultCredentials.
  17. Assign  Network credential object to oSmtp.Credential.
  18. Pass "oMsg" object to Send function.
     

Example :

using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
using System.Net;

namespace mysendemail
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("mail Processs started");
                MailMessage oMsg = new MailMessage();
                oMsg.From = new MailAddress("FromEmailAddress", "xxx");
                oMsg.To.Add(new MailAddress("ToEmailAddress", "Tejas"));
                oMsg.Subject = "Packet Parsing Problem";
                oMsg.Body = " Problem Occuread test mail";
                oMsg.SubjectEncoding = System.Text.Encoding.UTF8;
                oMsg.BodyEncoding = System.Text.Encoding.UTF8;
                oMsg.IsBodyHtml = false;
                oMsg.Priority = MailPriority.High;
                SmtpClient oSmtp = new SmtpClient("smtp.mail.yahoo.com", 587);//SMTP and Port
                oSmtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                oSmtp.EnableSsl = true;
                NetworkCredential oCredential = new NetworkCredential("SMTP UserName", "Password");
                oSmtp.UseDefaultCredentials = false;
                oSmtp.Credentials = oCredential;
                oSmtp.Send(oMsg);
                Console.WriteLine("Mail sent");
                Console.ReadKey();
            }

            catch (Exception ex)
            {
                Console.WriteLine(ex.InnerException);
                Console.ReadKey();
            }
        }
    }
}

No comments:

Post a Comment