C#.Net How To: Send email from gmail using c#
Sometimes you don't want to rely on your host to send emails. What if you want to send an email using gmail account? Sending an email in C# is a very simple task and it would not take much time to implement the functionality in C#.net.
Send email from Gmail using C#.Net
Send email from Gmail using C#.Net
In last article we learned to send an email in asp.net using C#. That article is very generic and only need to change the smtp server address, sender email id and password.
How to send email from gmail using c#
In this c# tutorial we are going to learn to send email from gmail using C#. Sending an email from C# is never been a big task. Please look at below code. Below given SendEmail is a method that accepts Recipients email id, subject of an email and body of an email. Call this method wherever required with passing three parameter values. Mostly you would like to call this method on send button. The most important thing is that you can place and call this method in any web application to send email in asp.net or winforms application or wpf applications.
protected string SendEmail(string toAddress, string subject, string body)
{
string result = "Message Sent Successfully..!!";
string senderID = "SenderEmailID";// use sender's email id here..
const string senderPassword = "Password"; // sender password here...
try
{
SmtpClient smtp = new SmtpClient
{
Host = "smtp.gmail.com", // smtp server address here...
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
Credentials = new System.Net.NetworkCredential(senderID, senderPassword),
Timeout = 30000,
};
MailMessage message = new MailMessage(senderID, toAddress, subject, body);
smtp.Send(message);
}
catch (Exception ex)
{
result = "Error sending email.!!!";
}
return result;
}
You can use above method to send an email from gmail using c#. But what particular settings we used for gmail in above code? Below are the settings that you need to use –
Smtp server address – smtp.gmail.com
Port – 587 or 465
EnableSsl – true
Gmail always uses SSL/TLS authentication, hence you need to enable the SSL property on port 587 or 465 to send email in c#. As given above, the smtp address of the gmail is "smtp.gmail.com". The highlighted part on above code shows the SenderEmailID and password. You just need to replace them with your gmail id and password to send email using c#.
To get more insights on how to code, please read how to send an email in Asp.net using C#.
In this c# tutorial we learned how to send email in c#. I hope you enjoyed the article. If you find this article helpful, then could you please share the article on your social media?
More articles -
How to enable tracing in Asp.net.
How to bind GridView in Asp.net with SQLDataSource in code behind.
How to bind GridView in Asp.net with SQLDataSource at Design Time.
How to load XML in dataset and display data in GridView.
More asp.net articles...
Copyright © 2011 - All Rights Reserved - Softron.in
Template by Softron Technology