public void sendMail(string to, string from, string subject, string body)
{
///Smtp config
SmtpClient client = new SmtpClient("smtp.gmail.com", 465);
// Edit password and username
client.Credentials = new NetworkCredential("emailid@gmail.com", "password");
client.EnableSsl = true;
///mail details
MailMessage msg = new MailMessage();
try
{
msg.From = new MailAddress(from);
msg.To.Add(to);
// msg.SubjectEncoding = System.Text.Encoding.UTF8;
msg.Subject = subject;
//msg.CC.Add();
msg.IsBodyHtml = true;
msg.BodyEncoding = System.Text.Encoding.UTF8;
msg.Body = body;
msg.Priority = MailPriority.Normal;
client.Send(msg);
}
catch (Exception exp)
{
///This runs the backup plan
SendMailAlt(to, from, subject, body);
}
}
private static void SendMailAlt(string to, string from, string subject, string body)
{
System.Web.Mail.MailMessage Mail = new System.Web.Mail.MailMessage();
Mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserver"] = ("smtp.gmail.com");
Mail.Fields["http://schemas.microsoft.com/cdo/configuration/sendusing"] = 2;
Mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"] = "465";
Mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpusessl"] = "true";
Mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"] = 1;
// Edit username & password
Mail.Fields["http://schemas.microsoft.com/cdo/configuration/sendusername"] = "emailid@gmail.com";
Mail.Fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"] = "password";
Mail.To = to;
Mail.From = from;
Mail.Subject = subject;
Mail.Body = body;
Mail.BodyFormat = System.Web.Mail.MailFormat.Html;
System.Web.Mail.SmtpMail.SmtpServer = "smtp.gmail.com";
System.Web.Mail.SmtpMail.Send(Mail);
}
protected void btnsend_Click(object sender, EventArgs e)
{
string fromEmail = "emailid@gmail.com"; // from email id
string subject = txtbody.Text;
sendMail(txttoemailaddress.Text, fromEmail.ToString(), subject.ToString(), txtmessage.Text);
}