2012-04-06 91 views
2

我在我的程序中有一個函數,用於從Google帳戶發送電子郵件。發送郵件時發生錯誤system.web.httpexceptions:郵件無法發送到SMTP服務器

我寫了這段代碼後,它用於正常工作,它發送電子郵件就像它應該。然而,現在,我無法實現它的工作。 (我現在正在開發Windows 7 64位,如果這會有所作爲)。

錯誤我得到的(這是從代碼中的第一個錯誤消息)是:

system.web.httpexceptions:郵件無法發送到SMTP服務器 。傳輸錯誤代碼是0x80040217。服務器響應 不可用 - > system.reflection.targetinvocationexception: 異常已被調用的目標引發。 - > system.runtime.interopservice.comexception(0x80040211):郵件 無法發送到smtp服務器。運輸錯誤代碼是 0x80040217。服務器響應不可用。

這是代碼:

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","[email protected]"); 
      message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword","thepassword"); 

      message.From = "[email protected]"; 
      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"; 

預先感謝您!

+0

0x80040217訪問表示服務器拒絕您的要求。檢查此調試步驟:http://social.msdn.microsoft.com/Forums/en-US/sqlreportingservices/thread/2bdde9e6-b57a-4b12-b476-7bcf1f8f66d5/ – devshorts 2012-04-06 22:04:53

+0

是的,你給我的鏈接幫助,感謝名單devshorts – 2012-04-06 22:11:30

+0

發表您的解決方案,如果你發現它 – devshorts 2012-04-07 01:48:53

回答

1

我遇到了同樣的問題,但在使用Gmail地址時我解決了問題,因爲我沒有使用Gmail登錄的兩步驗證。如果你只(通過移動電話代碼在我的情況),沒有額外的驗證使用密碼,它的工作原理

1

也許你已經阻止谷歌,因爲你是從一個程序在遠程計算機發送(即不一個你通常工作) 嘗試在運行程序的同一臺計算機上打開gmail,看看你是否有安全問題。

的Gmail帳戶

https://www.google.com/settings/account有打開的安全,看看我的Gmail已經封鎖您的帳戶