Steps of Sending mail
- Create object of MailMessage Class "oMsg".
- Assign "From email" address to oMsg.From.
- Assign "To email address" to oMsg.To.
- Assign Subject to oMsg.Subject.
- Assign body text to oMsg.Body.
- Assign Subject Encoding to oMsg.SubjectEncoding.
- Assign Body Encoding to oMsg.BodyEncoding.
- Assign Isbodyhtml bool variable value to oMsg.IsBodyHtml.
- Assign priority to oMsg.Priority.
- Create SMTP Client object.
- Give SMTP credentials in the constructor as parameters.
- Assign Delivery method to oSmtp.DeliveryMethod.
- Assign SSL bool variable to oSmtp.EnableSsl.
- Create object of NetworkCredential class.
- Pass SMTP user name and password as constructor parameters.
- Assign "UseDefaultCredentials" bool value to oSmtp.UseDefaultCredentials.
- Assign Network credential object to oSmtp.Credential.
- Pass "oMsg" object to Send function.
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