您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

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

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

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("from@yourdomain.com");

        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("to@anydomain.com");

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

您可以使用上面的代码

c# 2022/1/1 18:17:49 有537人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶