Cao Cu Hao

Cao Cu Hao

  • NA
  • 22
  • 5.9k

Send email using Gmail SMTP Mail Server in ASP.Net

Jun 30 2015 10:08 PM
Im coded here:
 <code>
private void Send()
{
try
{
string Subject = "This is test mail using smtp settings",
Body = "sad",
ToEmail = txtEmail.Text.Trim();

string SMTPUser = "[email protected]", SMTPPassword = "myPass";

//Now instantiate a new instance of MailMessage
MailMessage mail = new MailMessage();

//set the sender address of the mail message
mail.From = new MailAddress(SMTPUser, "iDOC");

//set the recepient addresses of the mail message
mail.To.Add(ToEmail);

//set the subject of the mail message
mail.Subject = Subject;

//set the body of the mail message
mail.Body = Body;

//leave as it is even if you are not sending HTML message
mail.IsBodyHtml = true;

//set the priority of the mail message to normal
mail.Priority = MailPriority.Normal;

//instantiate a new instance of SmtpClient
SmtpClient smtp = new SmtpClient();

//if you are using your smtp server, then change your host like "smtp.yourdomain.com"
smtp.Host = "smtp.gmail.com";

//chnage your port for your host
smtp.Port = 25; //or you can also use port# 587

//provide smtp credentials to authenticate to your account
smtp.Credentials = new System.Net.NetworkCredential(SMTPUser, SMTPPassword);
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
//if you are using secure authentication using SSL/TLS then "true" else "false"
smtp.EnableSsl = true;

smtp.Send(mail);
}
catch (SmtpException ex)
{
}
</code>
Im used port 587
My code is not error but is not work. Any guess what to do?
Thanks 

Answers (5)