C#SmtpClient类不能使用Gmail发送电子邮件 [英] c# SmtpClient class not able to send email using gmail

查看:1314
本文介绍了C#SmtpClient类不能使用Gmail发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法用我的Gmail帐户发送电子邮件。进出口拉我的头发了。

相同的设置雷鸟中很好地工作。

这里的code。我也试了端口465没有运气。

  SmtpClient SS =新SmtpClient(smtp.gmail.com,587);
ss.Credentials =新的NetworkCredential(用户名,传);
ss.EnableSsl = TRUE;
ss.Timeout = 10000;
ss.DeliveryMethod = SmtpDeliveryMethod.Network;
ss.UseDefaultCredentials = FALSE;毫米MAILMESSAGE新= MAILMESSAGE(donotreply@domain.com,destination@domain.com,主题在这里,我的身体);
mm.BodyEncoding = UTF8Encoding.UTF8;
mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
ss.Send(毫米);

继承人的误差

SMTP服务器要求安全连接或客户端未通过身份验证服务器响应为:5.5.1需要验证了解更多

继承人的堆栈跟踪

 在System.Net.Mail.MailCommand.CheckResponse(SmtpStatus code状态code,字符串响应)
   在System.Net.Mail.MailCommand.Send(康涅狄格州SmtpConnection,字节[]命令,从字符串)
   在System.Net.Mail.SmtpTransport.SendMail(MailAddress发件人,收件人MailAddressCollection,字符串deliveryNotify,SmtpFailedRecipientException&安培;除外)
   在System.Net.Mail.SmtpClient.Send(消息MAILMESSAGE)
   在email_example.Program.Main(字串[] args)在C:\\用户\\斯\\文档\\ Visual Studio 2008的\\项目\\电子邮件例如\\电子邮件例如\\的Program.cs:行23
   在System.AppDomain._nExecuteAssembly(议会会议,字串[] args)
   在System.AppDomain.ExecuteAssembly(字符串assemblyFile,证据assemblySecurity,字串[] args)
   在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   在System.Threading.ThreadHelper.ThreadStart_Context(对象状态)
   在System.Threading.ExecutionContext.Run(ExecutionContext中的ExecutionContext,ContextCallback回调,对象状态)
   在System.Threading.ThreadHelper.ThreadStart()


解决方案

您不会相信了固定我的问题。

凭据属性

  ss.Credentials =新的NetworkCredential(用户名,传);

必须在

声明

  ss.UseDefaultCredentials = FALSE;

所以最终的工作code列表

  SmtpClient SS =新SmtpClient(smtp.gmail.com,587);
ss.EnableSsl = TRUE;
ss.Timeout = 10000;
ss.DeliveryMethod = SmtpDeliveryMethod.Network;
ss.UseDefaultCredentials = FALSE;
ss.Credentials =新的NetworkCredential(用户名,传);毫米MAILMESSAGE新= MAILMESSAGE(donotreply@domain.com,destination@domain.com,主题在这里,我的身体);
mm.BodyEncoding = UTF8Encoding.UTF8;
mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
ss.Send(毫米);

这是一个错误吗?

I'm having trouble sending email using my gmail account. Im pulling my hair out.

The same settings work fine in Thunderbird.

Here's the code. I've also tried port 465 with no luck.

SmtpClient ss = new SmtpClient("smtp.gmail.com", 587);
ss.Credentials = new NetworkCredential("username", "pass");
ss.EnableSsl = true;
ss.Timeout = 10000;
ss.DeliveryMethod = SmtpDeliveryMethod.Network;
ss.UseDefaultCredentials = false;

MailMessage mm = new MailMessage("donotreply@domain.com", "destination@domain.com", "subject here", "my body");
mm.BodyEncoding = UTF8Encoding.UTF8;
mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
ss.Send(mm);

Heres the error

"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 "

Heres the stack trace

   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 email_example.Program.Main(String[] args) in C:\Users\Vince\Documents\Visual Studio 2008\Projects\email example\email example\Program.cs:line 23
   at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

解决方案

You won't belive what fixed my problem.

The Credentials property

ss.Credentials = new NetworkCredential("username", "pass");

must be declared after

ss.UseDefaultCredentials = false;

So the final working code listing is

SmtpClient ss = new SmtpClient("smtp.gmail.com", 587);
ss.EnableSsl = true;
ss.Timeout = 10000;
ss.DeliveryMethod = SmtpDeliveryMethod.Network;
ss.UseDefaultCredentials = false;
ss.Credentials = new NetworkCredential("username", "pass");

MailMessage mm = new MailMessage("donotreply@domain.com", "destination@domain.com", "subject here", "my body");
mm.BodyEncoding = UTF8Encoding.UTF8;
mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
ss.Send(mm);

Is this a bug?

这篇关于C#SmtpClient类不能使用Gmail发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆