简体   繁体   English

C#:尝试发送电子邮件时抛出SmtpException

[英]C# : SmtpException thrown when trying to send an email

when run, the following code throws this exception: "Additional information: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. " 运行时,以下代码将引发此异常:“其他信息:SMTP服务器需要安全连接,或者客户端未通过身份验证。服务器响应为:5.5.1需要身份验证。”

        public static void VerzendMail(Mail opgesteldeMail)
    {
        MailMessage mail = new MailMessage();
        SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
        mail.From = new MailAddress(opgesteldeMail.Verzender);
        mail.To.Add(opgesteldeMail.Ontvanger.Email);
        mail.Subject = opgesteldeMail.Onderwerp;
        mail.Body = opgesteldeMail.Data;

        SmtpServer.Port = 587;
        SmtpServer.Credentials = new NetworkCredential("myEmailAddress", "myPass");
        SmtpServer.EnableSsl = true;
        SmtpServer.UseDefaultCredentials = false;

        SmtpServer.Send(mail);
    }

(myEmailAddress & myPass aren't in code variables, i just wouldn't give the account away) (myEmailAddress和myPass不在代码变量中,我只是不会放弃该帐户)

I've searched for a while now, since it seems a lot of people can get this error but in all their cases it's easily solved (like adding credentials or stuff) but this doesn't seem the case in my code. 我已经搜索了一段时间,因为似乎很多人都可以得到此错误,但是在所有情况下都可以轻松解决(例如添加凭据或内容),但是在我的代码中情况并非如此。

The weird thing is and this is why i just don't get it, 2 months ago it worked... We were able to send emails regarding new passwords that had been generated and such. 奇怪的是,这就是为什么我不明白它的原因,两个月前它起作用了……我们能够发送有关已生成的新密码的电子邮件。 I haven't looked at this piece of code in the past 2 months, so idk, maybe someone deleted 1 line and i just can't figure out which one? 在过去的两个月中,我没有看过这段代码,所以idk,也许有人删除了1行,但我不知道是哪一行?

When debugging the code, i've checked all variables and they fill up good, with the correct values and such. 在调试代码时,我检查了所有变量,并使用正确的值等将它们填满。

We also tried making a new dummy account for testing, put it in myEmailAddress & myPass and it wouldn't work aswell :. 我们还尝试创建一个新的虚拟帐户进行测试,将其放入myEmailAddress和myPass中,但它也不能正常工作。

I know a lot of people ask & have asked this question before but i just can't seem to find it and it's bugging me beyond believable measures... 我知道很多人之前都曾问过这个问题,但我似乎找不到它,这使我感到难以置信。

Thanks for looking into this! 感谢您查看这个!

Jep.

try modifying this segment of code: 尝试修改这段代码:

SmtpServer.Credentials = new NetworkCredential("myEmailAddress", "myPass");
SmtpServer.EnableSsl = true;
SmtpServer.UseDefaultCredentials = false;

to this: 对此:

SmtpServer.UseDefaultCredentials = false;
SmtpServer.Credentials = new NetworkCredential("myEmailAddress", "myPass");
SmtpServer.EnableSsl = true;

In other words, try disabling UseDefaultCredentials before setting the credentials to use. 换句话说, 设置要使用的凭据之前 ,请尝试禁用UseDefaultCredentials。

The order that the properties are set may make a difference. 属性设置的顺序可能会有所不同。

I'm pretty sure I've seen a similar question and reordering things worked. 我很确定我已经看到了类似的问题,并且重新排序了。 I could be misremembering, though. 不过,我可能记错了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM