2011-09-26 70 views
13

在Windows Azure中無法發送郵件這是我的web.config:C# - 通過Gmail SMTP

<system.net> 
    <mailSettings> 
     <smtp deliveryMethod="Network"> 
      <network defaultCredentials="true" enableSsl="true" host="smtp.gmail.com" port="25" userName="[email protected]" password="xxxxxxxxxxx"/> 
     </smtp> 
    </mailSettings> 
</system.net> 

我的方法:

public static void EmailVerification(string email, string nickname) 
    { 
     MailAddress from = new MailAddress("xxxxxxxxxxx", "xxxxxxxxxxxx"); 
     MailAddress to = new MailAddress(email); 

     MailMessage message = new MailMessage(from, to); 

     message.Subject = "Test"; 
     message.Body = "Test"; 
     SmtpClient client = new SmtpClient(); 

     try 
     { 
      client.Send(message); 
     } 
     catch(SmtpFailedRecipientException ex) 
     { 
      HttpContext.Current.Response.Write(ex.Message); 
      return; 
     } 
    } 

我的失敗SmtpException:

Failure sending mail. 

InnerException:

Unable to connect to the remote server 

我在本地測試它,但我想它應該工作。我必須在Windows Azure上運行它,我試過了,它也沒有工作。有什麼我失蹤?

+0

看到這樣一個問題:http://stackoverflow.com/questions/32260/sending-email-in-net-through-gmail/32336#32336 –

回答

7

基本認證和默認網絡憑證選項是互斥的;如果您將defaultCredentials設置爲true並指定用戶名和密碼,則會使用默認網絡憑據,並且基本身份驗證數據將被忽略。

使用

<system.net> 
    <mailSettings> 
     <smtp deliveryMethod="Network"> 
      <network enableSsl="true" host="smtp.gmail.com" port="25" userName="[email protected]" password="xxxxxxxxxxx"/> 
     </smtp> 
    </mailSettings> 
</system.net> 
+0

我看,我用這樣的:我需要將端口更改爲587 –