2017-06-27 38 views
-1

我希望我的系統在單擊按鈕時發送電子郵件,但是我每次都會在smtp.send(消息)中發現問題,沒有任何錯誤或任何內容,它只是雖然它仍然在運行,但它仍然凍結。我寫了4次嘗試,都不成功。無法使用c#中的smtp發送電子郵件#

1日嘗試:

try 
{ 
    MailMessage mail = new MailMessage(); 
    SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com"); 

    mail.From = new MailAddress("[email protected]"); 
    mail.To.Add("[email protected]"); 
    mail.Subject = "Test Mail"; 
    mail.Body = "This is for testing SMTP mail from GMAIL"; 

    SmtpServer.Port = 25; 
    SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password"); 
    SmtpServer.EnableSsl = true; 

    SmtpServer.Send(mail); 
    MessageBox.Show("mail Send"); 
    MessageBox.Show("Email sent successfully"); 
} 
catch (Exception ex) 
{ 
    MessageBox.Show(ex.Message); 
} 

第2次嘗試:

try 
{ 
    //string s = $"User {edtID} has failed to login"; 

    MailMessage message = new MailMessage(); 
    message.To.Add("[email protected]"); 
    message.Subject = "Employee login failure"; 
    message.From = new MailAddress("[email protected]"); 
    message.Body = "Hello"; 
    SmtpClient smtp = new SmtpClient("smtp.saix.net"); 
    smtp.Send(message); 
    MessageBox.Show("mail Sent"); 
} 
catch (Exception ex) 
{ 
    MessageBox.Show(ex.Message); 
} 

SmtpClient smtp = new SmtpClient(); 
smtp.Port = 25; 
smtp.Credentials = new System.Net.NetworkCredential("username", "password"); 
smtp.EnableSsl = true; 
MailMessage mailMessage = new MailMessage(); 
mailMessage.To.Add(new MailAddress("[email protected]", "ME")); 
mailMessage.Subject = "Some Subject"; 
mailMessage.Body = "Test"; 
smtp.Send(mailMessage); 

第三嘗試:

// string s = $"User {edtID} has failed to login"; 
MailMessage mail = new MailMessage(); 
SmtpClient SmtpServer = new SmtpClient(); 
mail.To.Add("[email protected]"); 
mail.From = new MailAddress("[email protected]"); 
mail.Subject = "Employee login failure"; 
mail.IsBodyHtml = true; 
mail.Body = "Hello"; 
SmtpServer.Host = "smtp.saix.net"; 
SmtpServer.Port = 25; 
SmtpServer.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network; 
try 
{ 

    SmtpServer.Send(mail); 
} 
catch (Exception ex) 
{ 
    MessageBox.Show("Exception Message: " + ex.Message); 
    if (ex.InnerException != null) 
     MessageBox.Show("Exception Inner: " + ex.InnerException); 

} 

第四嘗試:

var sysLogin = "[email protected]"; 
var sysPass = "[email protected]"; 
var sysAddress = new MailAddress(sysLogin, "Message from me!"); 

var receiverAddress = new MailAddress("mymail#gmail.com"); 

var smtp = new SmtpClient 
{ 
    Host = "smtp.gmail.com", //gmail example 
    Port = 587, 
    EnableSsl = false, 
    DeliveryMethod = SmtpDeliveryMethod.Network, 
    UseDefaultCredentials = false, 
    Credentials = new System.Net.NetworkCredential(sysLogin, sysPass) 
}; 


using (var message = new MailMessage(sysAddress, receiverAddress) { Subject = "Some subject", Body = "Some text" }) 
{ 
    smtp.Send(message); 
} 
MessageBox.Show("Success"); 
+1

如果你的代碼全部被註釋掉了,你如何期待它做任何事情?如果您使用'SmtpClient',如何「使用SQL發送電子郵件」?你爲什麼要將'SmtpClient'的實例稱爲'SmtpServer'?這似乎倒退了。 – mason

+0

它的全部註釋,因爲我是單獨嘗試每個人,它貫穿一切,但在smtp.Send(消息)失敗; – Armee

+0

查看以下網址:https://support.google.com/accounts/answer/185833?hl = zh_CN https://support.google.com/accounts/answer/6010255?hl = zh_CN –

回答

1

你試過嗎?看起來你所有的嘗試都有SSL + 25或587 + NoSSL。 SSL應與端口587

var smtpClient = new SmtpClient("smtp.gmail.com") 
{ 
       Port = 587, 
       DeliveryMethod = SmtpDeliveryMethod.Network, 
       UseDefaultCredentials = false, 
       EnableSsl = true, 
       Credentials = new NetworkCredential("[email protected]", "MyPassword") 
}; 
+1

此外,gmail現在對SMTP訪問非常挑剔。如果啓用了「安全性較低的應用程序」(如果您未使用雙因素身份驗證),或者您使用的是應用程序特定的密碼(如果您使用的是雙因素身份驗證),則此功能纔可用。 –

1

我曾經有一個問題,我的電子郵件客戶端將凍結上使用STMP設置Gmail時發送郵件。然後,我必須在Gmail帳戶設置中創建應用程序密碼,並在stmp設置中使用該應用程序密碼才能使我的客戶端正常工作。試試這個,希望它修復凍結問題

  1. 轉到https://security.google.com/settings/security/apppasswords
  2. 選擇和應用[自定義,如果有的話],設備[自定義,如果有的話],然後點擊生成。
  3. 將生成一個類似如下的密碼:kxyo gmnz dais wcau
  4. 使用此應用程序密碼而不是您現在在stmp設置中使用的密碼。
+0

謝謝拉胡爾。你有沒有可能給我指導如何實現這一點? – Armee

+0

對此的需求取決於您是否啓用了雙因素身份驗證。如果你有2FA,你需要這個應用特定的密碼。如果您不使用2FA,則需要啓用「安全性較低的應用程序」,或者更新您的C#代碼以使用OAuth(而不是微不足道的)。 –

+0

@Armee我更新了我的答案,但我的解決方案要求您的帳戶啓用兩步驗證。那麼只有這樣才行 –