Sending mail in asp.net using gmail gateway
using System.Net.Mail;
.........
.........
public string SendMail(string uname,string password, string To, string Subject, string Body)
{
string fromid = uname;
string mypass = password;
try
{
//Create Mail Message Object with content that you want to send with mail.
MailMessage MyMailMessage = new MailMessage(fromid, To, Subject, Body);
MyMailMessage.IsBodyHtml = true;
//Proper Authentication Details need to be passed when sending email from gmail
System.Net.NetworkCredential mailAuthentication = new System.Net.NetworkCredential(fromid, mypass.ToString());
//Smtp Mail server of Gmail is "smpt.gmail.com" and it uses port no. 587
//For different server like yahoo this details changes and you can
//get it from respective server.
System.Net.Mail.SmtpClient mailClient = new System.Net.Mail.SmtpClient("smtp.gmail.com", 587);
//Enable SSL
mailClient.EnableSsl = true;
mailClient.UseDefaultCredentials = false;
mailClient.Credentials = mailAuthentication;
mailClient.Send(MyMailMessage);
return "Mail Sent";
}
catch (SmtpException ex)
{
string error = ex.ToString();
if (error.Contains("The remote name could not be resolved: 'smtp.gmail.com'"))
{
return "No Connection";
}
else if (error.Contains("The server response was: 5.5.1 Authentication Required"))
{
return "Username Password Missmatch";
}
else
{
return "Mail Not Sent";
}
}
}
.........
.........
public string SendMail(string uname,string password, string To, string Subject, string Body)
{
string fromid = uname;
string mypass = password;
try
{
//Create Mail Message Object with content that you want to send with mail.
MailMessage MyMailMessage = new MailMessage(fromid, To, Subject, Body);
MyMailMessage.IsBodyHtml = true;
//Proper Authentication Details need to be passed when sending email from gmail
System.Net.NetworkCredential mailAuthentication = new System.Net.NetworkCredential(fromid, mypass.ToString());
//Smtp Mail server of Gmail is "smpt.gmail.com" and it uses port no. 587
//For different server like yahoo this details changes and you can
//get it from respective server.
System.Net.Mail.SmtpClient mailClient = new System.Net.Mail.SmtpClient("smtp.gmail.com", 587);
//Enable SSL
mailClient.EnableSsl = true;
mailClient.UseDefaultCredentials = false;
mailClient.Credentials = mailAuthentication;
mailClient.Send(MyMailMessage);
return "Mail Sent";
}
catch (SmtpException ex)
{
string error = ex.ToString();
if (error.Contains("The remote name could not be resolved: 'smtp.gmail.com'"))
{
return "No Connection";
}
else if (error.Contains("The server response was: 5.5.1 Authentication Required"))
{
return "Username Password Missmatch";
}
else
{
return "Mail Not Sent";
}
}
}
Comments
Post a Comment