Sending email via SMTP in ASP.NET is a really painless experience. However, there are a couple of hoops to jump through if you want to use Google’s GMail SMTP servers. The following example shows a really simple function to get the job done.
The Google support page for configuring a mail client gives us a good starting point. We can see that we need to use the host smtp.gmail.com on port 587. We can also see that we must transmit over a secure connection and that we need to use our GMail username and password to authenticate with the server.
using System.Net.Mail;
public class GMailSMTP
{
public void Send(string to, string subject, string message, bool isHtml)
{
// Create a new message
var mail = new MailMessage();
// Set the to and from addresses.
// The from address must be your GMail account
mail.From = new MailAddress("example@gmail.com");
mail.To.Add(new MailAddress(to));
// Define the message
mail.Subject = subject;
mail.IsBodyHtml = isHtml;
mail.Body = message;
// Create a new Smpt Client using Google's servers
var mailclient = new SmtpClient();
mailclient.Host = "smtp.gmail.com";
mailclient.Port = 587;
// This is the critical part, you must enable SSL
mailclient.EnableSsl = true;
// Specify your authentication details
mailclient.Credentials = new System.Net.NetworkCredential(
"YOUR GMAIL USERNAME",
"YOUR GMAIL PASSWORD");
mailclient.Send(mail);
}
}
That's all there is to it!
Ohh thanks it is so usefull code for me now. ı wanna use it the future.
its not working..getting an error
“Command not implemented. The server response was: 5.5.1 Unrecognized command. e40sm981652wfj.11″
can anyone help?
I am suddenly having the same issue as sandeep’s. Does anyone know why this can suddenly happen?
Best regards,
I changed EnableSsl to “false”, now it is working. Regards
Thanks! brilliant
I do not understand this. When testing web site through visual studio (asp.net development server), SSL must be set to false for the email to go through successfully.
However once the site is published, say, to IIS 6.0. SSL must be TRUE or you get another error. Why the different settings?
thanks for ur post its so useful :)
I get the same problem when testing. SSL must be set to False in the ASP.NET Development Server….
The transport failed to connect to the server.
Error occure in that application which setting are change in sending mail…………
this issue is because the smtp is not available for that kind of operation. You must use other gmail’s SMTP Server.
send me a email and I will share a couple of them.
My email is james.leon@mensajitoparatodos.com
Best regards,
i m getting below error.
System.Net.Mail.SmtpException: Failure sending mail. —> System.Net.WebException: Unable to connect to the remote server —> System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 74.125.155.109:587 at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress) at System.Net.Sockets.Socket.InternalConnect(EndPoint remoteEP) at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout, Exception& exception)
Please help me out..
I used very similiar code for a wcf service. my service is sending email succesfully if I run locally . but it is not working on the website after publish. it gaves me page error at bottom of internet explorer and nothing else. it is able to access the service but it throw an exception. success variable gets ‘false’ value. ( it gets ‘true’ when I run locally. )
I used my gmail acount for credentials.
smtp.gmail.com for address
enablessl=true
and 587 for port.
also I add UseDefaultCredentials=false; (actually, it was working locally without this line too)
looks like smtp is not working on site.
any recommendation?
working 100%
i made so :
using System.Net;
using System.Net.Mail;
namespace LoginWebSite
{
public partial class Email : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button2_Click(object sender, EventArgs e)
{
MailMessage msg = new MailMessage {From = new MailAddress(txtGAddress.Text, “Sender’s Name”)};
msg.To.Add(new MailAddress(txtTo.Text));
msg.Subject = txtSubject.Text;
msg.Body = txtMessage.Text;
msg.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient
{
Host = “smtp.gmail.com”,
Credentials = new NetworkCredential(txtGAddress.Text, txtGPassword.Text),
Port = 587,
EnableSsl = true
};
Label1.Visible = true;
try
{
smtp.Send(msg);
Label1.Text = “Email sent accessfully.”;
}
catch (Exception exeption)
{
Label1.Text = exeption.Message;
}
}
}
}
this code is working with source but this code is not running after the publishing the asp.net source and no error are found in this context…