Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why I use google 'smtp' cannot send out email?

Tags:

c#

email

smtp

gmail

I have the following program to send an email by using "smtp.gmail.com:587"

namespace TestMailServer
{
    class Program
    {
        static void Main(string[] args)
        {
            MailMessage mail = new MailMessage();
            SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
            mail.From = new MailAddress("[email protected]");
            mail.To.Add("[email protected]");
            mail.Subject = "Test Mail";
            mail.Body = "This is for testing SMTP mail";

            SmtpServer.Port = 587;
            SmtpServer.Credentials = new System.Net.NetworkCredential("[email protected]", "myPassword");
            SmtpServer.EnableSsl = true;
            SmtpServer.Send(mail);
            Console.WriteLine("Send out");

        }
    }
}

[email protected], [email protected] are really existing and [email protected]'s password is myPassword. Why I got the following error:

Unhandled Exception: System.Net.Mail.SmtpException: 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 at System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response) at System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] command, String from)
at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, SmtpFailedRecipientException& exception) at System.Net.Mail.SmtpClient.Send(MailMessage message) at TestMailServer.Program.Main(String[] args) in D:\visual studio 2010\Projects\TestMailServer\TestMailServer\Program.cs:line 26 Press any key to continue . . .

like image 940
spspli Avatar asked Apr 28 '11 13:04

spspli


People also ask

Why is SMTP not sending emails?

Check whether there is network access from CSO to the SMTP server. Check whether the firewall is blocking SMTP traffic to SMTP server or whether the ports are blocked. If the server settings and authentication settings are correct, check whether the firewall is blocking port 587 and 465 and SMTP traffic.

Why SMTP Gmail is not working?

Solution. Check SMTP settings, enable less secure apps, and unlock Captcha: Confirm the form's SMTP settings are correct. Enable access to Less secure apps.

Can I use Gmail SMTP server for sending mail?

Use the Gmail SMTP serverIf you connect using SSL or TLS, you can send mail to anyone inside or outside of your organization using smtp.gmail.com as your server. This option requires you to authenticate with your Gmail or Google Workspace account and passwords.


1 Answers

I'm not sure what is causing your problem. Here is some code I have been using to successfully send email through a gmail account:

const string from = "...";
var fromAddr = new MailAddress(from, "Bug Tracker");
var toAddr = new MailAddress("...@...", "...");
var client = new SmtpClient {
    Host = "smtp.gmail.com",
    Port = 587,
    EnableSsl = true,
    DeliveryMethod = SmtpDeliveryMethod.Network,
    UseDefaultCredentials = false,
    Timeout = 30 * 1000,
    Credentials = new NetworkCredential(fromAddr.Address, "...")
};
using (var msg = new MailMessage(fromAddr, toAddr)) {
    msg.Subject = "...";
    msg.Body = string.Format("username: {0}\nversion: {1}\n\n{2}", Environment.UserName, Assembly.GetExecutingAssembly().GetName().Version.ToString(3), cbtext);
    client.Send(msg);
}
like image 98
Ferruccio Avatar answered Oct 14 '22 13:10

Ferruccio