To send A email from asp.net page using SMTP protocol follow following steps:
<mailSettings>
<smtp>
<network host="smtp.gmail.com"
port="587"
userName="your mail id"
password="password"
enableSsl="true"/>
</smtp>
</mailSettings>
</system.net>
</configuration>
protected void sendMail()
{
try
{
MailMessage mailMessage = new MailMessage()
{
Subject = contactSubject.Text,
Body = contactBody.Text ,
From = new MailAddress(contactEmail.Text),
IsBodyHtml = true
};
mailMessage.To.Add(new MailAddress("atul39.chavan@gmail.com"));
SmtpClient smtpClient = new SmtpClient();
smtpClient.SendAsync(mailMessage, mailMessage);
lblStatus.Visible = true;
lblStatus.Text = "Your Request Submitted Successfully ..!!";
}
catch
{
lblStatus.Text = "Sorry Try Again Later..!!";
}
}
- In web.config add following code
<mailSettings>
<smtp>
<network host="smtp.gmail.com"
port="587"
userName="your mail id"
password="password"
enableSsl="true"/>
</smtp>
</mailSettings>
</system.net>
</configuration>
- Add a C# function to send email
protected void sendMail()
{
try
{
MailMessage mailMessage = new MailMessage()
{
Subject = contactSubject.Text,
Body = contactBody.Text ,
From = new MailAddress(contactEmail.Text),
IsBodyHtml = true
};
mailMessage.To.Add(new MailAddress("atul39.chavan@gmail.com"));
SmtpClient smtpClient = new SmtpClient();
smtpClient.SendAsync(mailMessage, mailMessage);
lblStatus.Visible = true;
lblStatus.Text = "Your Request Submitted Successfully ..!!";
}
catch
{
lblStatus.Text = "Sorry Try Again Later..!!";
}
}
- Call the above created function on any of the event such as button click, page load etc.
0 comments