我的程序中有一个函数可以从 Google 帐户发送电子邮件。
我不久前编写了这段代码,它曾经工作正常,它按照预期发送电子邮件。然而现在我无法让它发挥作用。 (我现在正在使用 Windows 7 64 位,如果这会有所不同的话)。
我得到的错误(这是代码中的第一条错误消息)是:
system.web.httpexceptions: the message could not be sent to the smtp server. the transport error code was 0x80040217. the server response was not available --> system.reflection.targetinvocationexception: exception has been thrown by the target of an invocation. --> system.runtime.interopservice.comexception (0x80040211): the message could not be sent to the smtp server. the transport error code was 0x80040217. the server response was not available.
这是代码:
void sendEmail(string [] emailList, int emailLength, string fileName)
{
int i = 0; //variable to act as temporary index into emailList array
try
{
try
{
MailMessage message = new MailMessage();
//Because using google server, requires SSL
message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");
message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", "465");
message.Fields.Add( "http://schemas.microsoft.com/cdo/configuration/smtpauthenticate",1 );
message.Fields.Add( "http://schemas.microsoft.com/cdo/configuration/sendusername","<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0e7a666b7b7d6b7c606f636b4e69636f6762206d6163" rel="noreferrer noopener nofollow">[email protected]</a>" );
message.Fields.Add( "http://schemas.microsoft.com/cdo/configuration/sendpassword","thepassword" );
message.From = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2e5a464b5b5d4b5c404f434b6e49434f4742004d4143" rel="noreferrer noopener nofollow">[email protected]</a>";
message.To = emailList[i];
//add Bcc
while (++i < emailLength)
{
message.Bcc = emailList[i];
}//end while
message.Subject = "Test Subject";
message.Body = "Test Body";
MailAttachment fileAttach = new MailAttachment(fileName);
message.Attachments.Add(fileAttach);
try
{
SmtpMail.SmtpServer = "smtp.gmail.com";
提前谢谢您!
最佳答案
我遇到了同样的问题,但当我使用 Gmail 地址时没有使用 Gmail 登录的两步验证时,问题得到解决。如果您只使用密码而不进行额外的验证(在我的例子中通过手机代码),它就可以工作
关于c# - 发送电子邮件时出错 system.web.httpexceptions : the message could not be sent to the smtp server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10049714/