C# - 无法通过Gmail SMTP在WIndows Azure中发送邮件

时间:2011-09-26 18:20:36

标签: azure smtpclient

这是我的Web.config:

<system.net>
    <mailSettings>
        <smtp deliveryMethod="Network">
            <network defaultCredentials="true" enableSsl="true" host="smtp.gmail.com" port="25" userName="xxxxxxx@gmail.com" 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上运行它,我尝试过它也没有用。有什么我想念的吗?

1 个答案:

答案 0 :(得分:8)

基本身份验证和默认网络凭据选项是互斥的;如果将defaultCredentials设置为true并指定用户名和密码,则使用默认网络凭据,并忽略基本身份验证数据。

使用

<system.net>
    <mailSettings>
        <smtp deliveryMethod="Network">
            <network enableSsl="true" host="smtp.gmail.com" port="25" userName="xxxxxxx@gmail.com" password="xxxxxxxxxxx"/>
        </smtp>
    </mailSettings>
</system.net>