如何在 C# 中进行 SMTP 身份验证

问题描述 投票:0回答:7

我创建了新的 ASP.NET Web 应用程序,它使用 SMTP 发送消息。问题是 smtp 未经过消息发送者的身份验证。

如何在我的程序中对 SMTP 进行身份验证? C# 是否有一个具有输入用户名和密码属性的类?

c# authentication smtp
7个回答
161
投票
using System.Net;
using System.Net.Mail;

using(SmtpClient smtpClient = new SmtpClient())
{
    var basicCredential = new NetworkCredential("username", "password"); 
    using(MailMessage message = new MailMessage())
    {
        MailAddress fromAddress = new MailAddress("[email protected]"); 

        smtpClient.Host = "mail.mydomain.com";
        smtpClient.UseDefaultCredentials = false;
        smtpClient.Credentials = basicCredential;

        message.From = fromAddress;
        message.Subject = "your subject";
        // Set IsBodyHtml to true means you can send HTML email.
        message.IsBodyHtml = true;
        message.Body = "<h1>your message body</h1>";
        message.To.Add("[email protected]"); 

        try
        {
            smtpClient.Send(message);
        }
        catch(Exception ex)
        {
            //Error, could not send the message
            Response.Write(ex.Message);
        }
    }
}

您可以使用上面的代码。


91
投票

确保您设置

SmtpClient.Credentials
after 拨打
SmtpClient.UseDefaultCredentials = false

顺序很重要,因为设置

SmtpClient.UseDefaultCredentials = false
会将
SmtpClient.Credentials
重置为空。


7
投票

在发送消息之前设置 Credentials 属性。


3
投票

要通过TLS/SSL发送消息,需要将SmtpClient类的Ssl设置为true。

string to = "[email protected]";
string from = "[email protected]";
MailMessage message = new MailMessage(from, to);
message.Subject = "Using the new SMTP client.";
message.Body = @"Using this new feature, you can send an e-mail message from an application very easily.";
SmtpClient client = new SmtpClient(server);
// Credentials are necessary if the server requires the client 
// to authenticate before it will send e-mail on the client's behalf.
client.UseDefaultCredentials = true;
client.EnableSsl = true;
client.Send(message);

1
投票

如何发送消息?

System.Net.Mail
命名空间中的类(这可能是您应该使用的)完全支持身份验证,可以在 Web.config 中指定,也可以使用
SmtpClient.Credentials
属性。


0
投票

就我而言,即使遵循了上述所有内容。我必须将我的项目从 .net 3.5 升级到 .net 4,才能针对我们的内部 Exchange 2010 邮件服务器进行授权。


0
投票

通过NuGet Package Manager添加

MailKit
,然后使用以下代码:

using MailKit.Net.Smtp;
using MailKit.Security;
using MimeKit;
using MimeKit.Text;

using (SmtpClient smtpClient = new SmtpClient())
{
    using (MimeMessage message = new MimeMessage())
    {
        try
        {
            var email = new MimeMessage();
            email.From.Add(MailboxAddress.Parse(@"<sender email>"));
            email.To.Add(MailboxAddress.Parse("<recipient email>"));
            email.Subject = "<subject here>";
            email.Body = new TextPart(TextFormat.Html) { Text = "<h1>your message body</h1>" };
            var smtp = new SmtpClient();
            await smtp.ConnectAsync("smtp.<provider>.com", 587, SecureSocketOptions.StartTls);
            await smtp.AuthenticateAsync(@"<sender email>", @"<password>");
            await smtp.SendAsync(email);
            await smtp.DisconnectAsync(true);
        }
        catch (Exception ex)
        {
            //Error, could not send the message
            // Do something
        }
    }
}

方法

smtp.AuthenticateAsync(user, password)
是您传递用户名/电子邮件和密码/密钥的地方

将尖括号中的值替换为您这边合适的值

© www.soinside.com 2019 - 2024. All rights reserved.