【问题标题】:c# Send email using visual studio 2015c#使用visual studio 2015发送邮件
【发布时间】:2018-05-12 02:38:12
【问题描述】:

我正在尝试在 Windows 电脑上使用 Visual Studio 2015 发送电子邮件。我想用一个 Outlook 电子邮件地址来发送电子邮件,请有人帮我获取正确的代码。我尝试了很多方法,但他们要么超时,要么说他们不能发送失败以发送电子邮件。请帮忙

SmtpClient cv = new SmtpClient("smtp.live.com", 25);
cv.EnableSsl = true;
cv.Credentials = new NetworkCredential("xxxemail@mail.com", "password");
try
{
    cv.Send("xxxemail@mail.com", "xxxanotheremail@mail.com", "", "Hello");
    MessageBox.Show("Done");
}
catch(Exception w)
{
    MessageBox.Show("Not send" + w.InnerException);
}  

【问题讨论】:

  • 请将您的代码添加到问题中,以便我们知道您尝试了什么。
  • 有几种方法可以做到这一点 - 我想最好选择其中一个失败的尝试并详细说明您遇到的错误。
  • 这是我最近的尝试
  • 我很确定 live.com 需要一个不同于标准 smtp 端口 25 的端口。另外我认为它让大多数人畏缩阅读“使用 Visual Studio 发送电子邮件”.
  • 您知道是否有防火墙阻止了 SMTP 端口?

标签: c# email


【解决方案1】:

您必须弄清楚,在设置 UseDefaultCredentials = false 之前设置 SmtpClient Credentials 属性会导致凭据被忽略。

失败:

SmtpClient smtp = new SmtpClient;
smtp.Credentials = new NetworkCredential("richardteunen2@hotmail.com","pass");
smtp.UseDefaultCredentials = false;

作品:

SmtpClient smtp = new SmtpClient;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential("richardteunen2@hotmail.com","pass");

【讨论】:

  • 仍然无法正常工作。它只是说操作超时
【解决方案2】:
    public async Task<bool> SendAsync(EmailMessage message)
    {
        bool result = true;

        try
        {
            using (var email = new MailMessage("from@gmail.com", "to@gmail.com", message.Subject, message.Body))
            {
                var mailClient = new SmtpClient("smtp.gmail.com", 587) { Credentials = new NetworkCredential("from@gmail.com", "password"), EnableSsl = true };

                await mailClient.SendMailAsync(email);
            }
        }
        catch (Exception ex)
        {
            result = false;
        }

        return result;
    }
}
Hotmail settings
Server          Port
smtp.live.com   25, 587

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多