Why I use google smtp cannot send out email?

你。 提交于 2019-12-10 16:09:07

问题


Hi I have following program to send 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("myTest@gmail.com");
            mail.To.Add("myTest2@gmail.com");
            mail.Subject = "Test Mail";
            mail.Body = "This is for testing SMTP mail";

            SmtpServer.Port = 587;
            SmtpServer.Credentials = new System.Net.NetworkCredential("myTest@gmail.com", "myPassword");
            SmtpServer.EnableSsl = true;
            SmtpServer.Send(mail);
            Console.WriteLine("Send out");

        }
    }
}

myTest@gmail.com, myTest2@gmail.com are really existing and myTest@gmail.com'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 . . .


回答1:


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);
}



回答2:


I had the code that Ferruccio posted and this recently stopped working. I moved my settings into the .config file for my site and it started to work again:

<system.net>
    <mailSettings>
        <smtp from="fromEmail" deliveryMethod="Network">
            <network defaultCredentials="false" enableSsl="true" host="smtp.gmail.com" port="587"
                 userName="fromEmail" password="password"/>
        </smtp>
    </mailSettings>
</system.net>



回答3:


As far as i remember by default UseDefaultCredentialsProperty is set to true. So that could cause the authentification error you've got. try to add these lines from the previous answer to your code

SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;
SmtpServer.UseDefaultCredentials = false;

I've experienced couple of weird exceptions if not set DeliveryMethod property explicitly.



来源:https://stackoverflow.com/questions/5819065/why-i-use-google-smtp-cannot-send-out-email

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!