2017-05-27 44 views
0

我有一個winforms應用程序,通過Gmail發送電子郵件。 它工作的很好,但在一種情況下,發送失敗,我得到上述錯誤。 當我的應用程序在通過RDP連接訪問的計算機上運行時發生。 我至今嘗試沒有成功:通過Gmail發送電子郵件c#錯誤#10051網絡無法訪問

  1. 禁用Windows防火牆
  2. 禁用Microsoft安全Essensials。
  3. 確保沒有安裝其他防病毒程序。
  4. 將587端口添加到出站的rull。

我的代碼如下:(請注意,此代碼工作在所有情況下,但這)

string fromAddress = txtFrom.Text.Trim(); 
       string toAddress = txtTo.Text.Trim(); 
       string fromPassword = ePassword; 
       string subject = txtSubject.Text; 
       string body = txtMessage.Text; 

       var smtp = new SmtpClient 
       { 
        Host = "smtp.gmail.com", 
        Port = 587, 
        EnableSsl = true, 
        DeliveryMethod = SmtpDeliveryMethod.Network, 
        UseDefaultCredentials = false, 
        //Credentials = new NetworkCredential(fromAddress.Address, fromPassword) 
        Credentials = new NetworkCredential(fromAddress, fromPassword) 
       }; 
       //using (var message = new MailMessage(fromAddress, toAddress) 
       using (var message = new MailMessage() 
       { 
        From = new MailAddress(fromAddress, eDisplayName), 
        Subject = subject, 
        Body = body 
       }) 
       { 
        // Check for recipiens 
        if (txtTo.Text.Trim() != "") 
        { 
         foreach (string email in txtTo.Text.Split(';')) 
         { 
          message.To.Add(email.Trim()); 
         } 
        } 
        // check for copies 
        if (txtCopy.Text.Trim() != "") 
        { 
         foreach (string email in txtCopy.Text.Split(';')) 
         { 
          message.CC.Add(email.Trim()); 
         } 
        } 
        // check for blind copy 
        if (txtBlindCopy.Text.Trim() != "") 
        { 
         foreach (string email in txtBlindCopy.Text.Split(';')) 
         { 
          message.Bcc.Add(email.Trim()); 
         } 
        } 
        //check for attachments 
        for (int i = 0; i < clbAttachments.Items.Count; i++) 
        { 
         if (clbAttachments.GetItemChecked(i) == true) 
         { 
          Attachment attachment = new Attachment(clbAttachments.Items[i].ToString()); 
          message.Attachments.Add(attachment); 
         } 
        } 

        smtp.Send(message); 
        return true; 
+1

RDP中的網絡類型是什麼?如果遠程計算機位於正在使用Outlook的網絡中,則可能永遠無法使其工作。可能有一個阻止電子郵件的代理服務器。代理服務器會自動獲取包括端口587在內的所有電子郵件端口號。因此,代理服務器必須設置爲允許發送您的電子郵件。代理服務器將自動將電子郵件轉發到Outlook服務器。 – jdweng

+0

如何檢查代理服務器設置? 遠程計算機不在我的本地網絡中。 我從第三方購買了遠程服務器。 – subirshan

+0

首先檢查控制面板:用戶帳戶:電子郵件。防火牆可能設置爲端口轉發電子郵件端口。所以看到這篇文章:https://www.pcsteps.com/1154-port-forwarding-router-windows-firewall/ – jdweng

回答

0

找到了答案。我已將端口更改爲25,並且發送了消息!我將所有更改回滾到操作系統,並進行雙重檢查,以確保端口更改是唯一需要完成的事情。希望有人會發現這個有用的。

編輯: 端口25是一個非安全端口。就我而言,我不在乎,所以它解決了我的問題。

相關問題