无法连接到 SMTP 服务器

发布于 2024-11-08 16:16:05 字数 1504 浏览 0 评论 0原文

//aspx.cs file    
protected void SendButton_Click(object sender, EventArgs e)
{
    MailHelper.SendMailMessage("[email protected]", "[email protected]", "", "", "Sample Subject", "Sample body of text for mail message");
}


//MailHelper.cs
 using System.Net.Mail;

public class MailHelper
{
    public static void SendMailMessage(string from, string to, string bcc, string cc, string subject, string body)
    {
        MailMessage mMailMessage = new MailMessage();
        mMailMessage.From = new MailAddress(from);
        mMailMessage.To.Add(new MailAddress(to));


        if ((bcc != null) && (bcc != string.Empty))
        {
            mMailMessage.Bcc.Add(new MailAddress(bcc));
        }

        if ((cc != null) && (cc != string.Empty))
        {
            mMailMessage.CC.Add(new MailAddress(cc));
        }      

        mMailMessage.Subject = subject;
        mMailMessage.Body = body;

        mMailMessage.IsBodyHtml = true;
        mMailMessage.Priority = MailPriority.Normal;

        SmtpClient mSmtpClient = new SmtpClient("127.0.0.1");
        mSmtpClient.Send(mMailMessage);//Error here
    }
}

我在 mSmtpClient.Send(mMailMessage) 处遇到错误。

你能帮我吗?

//aspx.cs file    
protected void SendButton_Click(object sender, EventArgs e)
{
    MailHelper.SendMailMessage("[email protected]", "[email protected]", "", "", "Sample Subject", "Sample body of text for mail message");
}


//MailHelper.cs
 using System.Net.Mail;

public class MailHelper
{
    public static void SendMailMessage(string from, string to, string bcc, string cc, string subject, string body)
    {
        MailMessage mMailMessage = new MailMessage();
        mMailMessage.From = new MailAddress(from);
        mMailMessage.To.Add(new MailAddress(to));


        if ((bcc != null) && (bcc != string.Empty))
        {
            mMailMessage.Bcc.Add(new MailAddress(bcc));
        }

        if ((cc != null) && (cc != string.Empty))
        {
            mMailMessage.CC.Add(new MailAddress(cc));
        }      

        mMailMessage.Subject = subject;
        mMailMessage.Body = body;

        mMailMessage.IsBodyHtml = true;
        mMailMessage.Priority = MailPriority.Normal;

        SmtpClient mSmtpClient = new SmtpClient("127.0.0.1");
        mSmtpClient.Send(mMailMessage);//Error here
    }
}

I'm getting error at mSmtpClient.Send(mMailMessage).

Could you help me with it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萌化 2024-11-15 16:16:05

鉴于发件人地址是 gmail 帐户,我假设您正在尝试通过 GMAIL 发送电子邮件,如果不是,则本地计算机中的 SMTP 服务器有问题。
我希望这有帮助:

    public static void SendMailMessage(string from, string to, string bcc, string cc, string subject, string body)
    {
        //Creating Mail Message
        var mMailMessage = new MailMessage(from, to, subject, body);
        if (!String.IsNullOrEmpty(bcc))
            mMailMessage.Bcc.Add(bcc);
        if (!String.IsNullOrEmpty(cc))
            mMailMessage.CC.Add(cc);
        mMailMessage.IsBodyHtml = true;
        mMailMessage.Priority = MailPriority.Normal;

        //Sending Mail Message through GMAIL
        var mSmtpClient = new SmtpClient("smtp.gmail.com", 587);
        mSmtpClient.EnableSsl = true;
        mSmtpClient.Credentials = new System.Net.NetworkCredential(from, "GMAIL Password");
        mSmtpClient.Send(mMailMessage);
    }

Given that the from address is a gmail account i'm assuming you are trying to send an email through GMAIL, if not there is a problem with your SMTP server in the local machine.
I hope this helps:

    public static void SendMailMessage(string from, string to, string bcc, string cc, string subject, string body)
    {
        //Creating Mail Message
        var mMailMessage = new MailMessage(from, to, subject, body);
        if (!String.IsNullOrEmpty(bcc))
            mMailMessage.Bcc.Add(bcc);
        if (!String.IsNullOrEmpty(cc))
            mMailMessage.CC.Add(cc);
        mMailMessage.IsBodyHtml = true;
        mMailMessage.Priority = MailPriority.Normal;

        //Sending Mail Message through GMAIL
        var mSmtpClient = new SmtpClient("smtp.gmail.com", 587);
        mSmtpClient.EnableSsl = true;
        mSmtpClient.Credentials = new System.Net.NetworkCredential(from, "GMAIL Password");
        mSmtpClient.Send(mMailMessage);
    }
笑着哭最痛 2024-11-15 16:16:05

你的标题说明了一切。服务器仅允许 TLS 连接。
全部解释如下:https://www.rfc-editor.org/rfc/rfc2487

Your title says it all. The server does only allow TLS connections.
All explained here: https://www.rfc-editor.org/rfc/rfc2487

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文