2011-09-26 62 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 –