Sending mail using c#

For sending mail from one end to another end some parameters are required such as email Id, password, SMTP server and port number.
Email Id and password are required for sending mail to others. That means sender mail id and password required.
SMTP server address and port number get from your sender mail accounts.
E.g. :
For gmail we use,
SMTP -- smtp.gmail.com
Port-- 465

Namespace used for sending mail is :
Using System.Net.Mail;

Then follow below source code for sending mail. 
Source Code:       

MailMessage Msg = new MailMessage();
Msg.From = new MailAddress("Your-own-mail-id");
Msg.To.Add("mail-id-of-reciever");
Msg.Subject = "Send Mail";
Msg.IsBodyHtml = true;
Msg.Body = "Hii Sir";
SmtpClient client = new SmtpClient();
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("Your-own-mail-id", "Your-Password");
client.Port =your-port-number;
client.Host = "your-smtp-server";
client.DeliveryMethod = SmtpDeliveryMethod.Network;
try
{
client.Send(Msg);
statusLabel.Text = "Message Sent Succesfully";
}
catch (Exception ex)
{
statusLabel.Text = ex.ToString();
}

In this way you send mails to others.

Post a Comment

1 Comments