Following is the tested code to send email from a asp.net application. This example may also work for any SMTP/email service like yahoo, hotmail etc. whereas Gmail account is used for the time being:
using System.Net.Mail;
namespace Experiement
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender,System.EventArgs e)
{
MailMessage mailMessage = new MailMessage(new MailAddress("from@gmail.com") ,new MailAddress("to@yahoo.com"));
mailMessage.Subject = "Sending mail through gmail account";
mailMessage.IsBodyHtml = true;
mailMessage.Body = "<B>Sending mail thorugh gmail from asp.net</B>";
System.Net.NetworkCredential networkCredentials = new
System.Net.NetworkCredential("yourgmailaddress@gmail.com", "yourpassword");
SmtpClient smtpClient = new SmtpClient();
smtpClient.EnableSsl = true;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = networkCredentials;
smtpClient.Host = "smtp.gmail.com";
smtpClient.Port = 587;
smtpClient.Send(mailMessage);
Response.Write("Mail Successfully sent");
}
}
}
Posted Status in Programming