Sending Email through Gmail SMTP Server with C# : #
For some reason neither the accepted answer nor any others work for me for “Sending email in .NET through Gmail”. Why would they not work?
UPDATE: I have tried all the answers (accepted and otherwise) in the other question, but none of them work.
I would just like to know if it works for anyone else, otherwise Google may have changed something (which has happened before).
When I try the piece of code that uses SmtpDeliveryMethod.Network, I quickly receive an SmtpException on Send(message). The message is
The SMTP server requires a secure connection or the client was not authenticated.
The server response was:
5.5.1 Authentication Required. Learn more at” <– seriously, it ends there.
UPDATE: #
This is a question that I asked a long time ago, and the accepted answer is code that I’ve used many, many times on different projects.
I’ve taken some of the ideas in this post and other EmailSender projects to create an EmailSender project at Codeplex. It’s designed for testability and supports my favourite SMTP services such as GoDaddy and Gmail.
Here is some solutions :
Solution 1: #
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Net;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
var client = new SmtpClient(“smtp.gmail.com”, 587)
{
Credentials = new NetworkCredential(“myusername@gmail.com”, “mypwd”),
EnableSsl = true
};
client.Send(“myusername@gmail.com”, “myusername@gmail.com”, “test”, “testbody”);
Console.WriteLine(“Sent”);
Console.ReadLine();
}
}
}
Solution2 : #
//Satrt Send Email Function
public string SendMail(string toList, string from, string ccList,
string subject, string body)
{
MailMessage message = new MailMessage();
SmtpClient smtpClient = new SmtpClient();
string msg = string.Empty;
try
{
MailAddress fromAddress = new MailAddress(from);
message.From = fromAddress;
message.To.Add(toList);
if (ccList != null && ccList != string.Empty)
message.CC.Add(ccList);
message.Subject = subject;
message.IsBodyHtml = true;
message.Body = body;
// We use gmail as our smtp client
smtpClient.Host = “smtp.gmail.com”;
smtpClient.Port = 587;
smtpClient.EnableSsl = true;
smtpClient.UseDefaultCredentials = true;
smtpClient.Credentials = new System.Net.NetworkCredential(
“Your Gmail User Name”, “Your Gmail Password”);
smtpClient.Send(message);
msg = “Successful<BR>”;
}
catch (Exception ex)
{
msg = ex.Message;
}
return msg;
}
//End Send Email Function
Solution 3: #
- First of all setup your GMail account:
- Enable IMAP and assert the right maximum number of messages (you can do so here)
- Make sure your password is at least 7 characters and is strong (according to Google)
- Make sure you don’t have to enter a captcha code first. You can do so by sending a test email from your browser.
- Make changes in web.config (or app.config, I haven’t tried that yet but I assume it’s just as easy to make it work in a windows application):
<configuration>
<appSettings>
<add key=”EnableSSLOnMail” value=”True”/>
</appSettings>
<!– other settings –>
…
<!– system.net settings –>
<system.net>
<mailSettings>
<smtp from=”yourusername@gmail.com” deliveryMethod=”Network”>
<network
defaultCredentials=”false”
host=”smtp.gmail.com”
port=”587″
password=”stR0ngPassW0rd”
userName=”yourusername@gmail.com”
/>
<!– When using .Net 4.0 (or later) add attribute: enableSsl=”true” and you’re all set–>
</smtp>
</mailSettings>
</system.net>
</configuration>
Add a Class to your project:
Imports System.Net.Mail
Public Class SSLMail
Public Shared Sub SendMail(ByVal e As System.Web.UI.WebControls.MailMessageEventArgs)
GetSmtpClient.Send(e.Message)
‘Since the message is sent here, set cancel=true so the original SmtpClient will not try to send the message too:
e.Cancel = True
End Sub
Public Shared Sub SendMail(ByVal Msg As MailMessage)
GetSmtpClient.Send(Msg)
End Sub
Public Shared Function GetSmtpClient() As SmtpClient
Dim smtp As New Net.Mail.SmtpClient
‘Read EnableSSL setting from web.config
smtp.EnableSsl = CBool(ConfigurationManager.AppSettings(“EnableSSLOnMail”))
Return smtp
End Function
End Class
Solution 4: #
<system.net>
<mailSettings>
<smtp from=”myusername@gmail.com” deliveryMethod=”Network”>
<network defaultCredentials=”false” enableSsl=”true” host=”smtp.gmail.com” port=”587″ password=”password” userName=”myusername@gmail.com”/>
</smtp>
</mailSettings>
</system.net>