SMTP 服务器需要安全连接或客户端未经过身份验证 - 仍然无法工作

发布于 2024-12-14 07:26:42 字数 2115 浏览 2 评论 0原文

我收到上述错误。我查看了在线解决方案,似乎已经完成了所有必要的操作,但我仍然明白。我已经包含了 Enablessl = true;交付方式是网络,我已提供凭据。我调试了一下,用户名和密码似乎是正确的。

using System;
using System.Net;
using System.Net.Mail;
using System.Windows.Forms;

namespace SendMail
{
    public partial class MainWindow : Form
    {
        #region Private variables
        private MailMessage _message = new MailMessage();
        private EmailSender sender = EmailSender.GetInstance();
        private SmtpClient _smtpClient = new SmtpClient();
        #endregion

        public MainWindow()
        {
            InitializeComponent();
        }

        private void PrepareMailMessage()
        {
            // Set the FROM address
            _message.From = new MailAddress(tbFromAddr.ToString().Trim());

            // Set the TO address
            _message.To.Add(new MailAddress(tbToAddr.ToString().Trim()));

            // Set the SUBJECT
            _message.Subject = tbSubject.ToString();

            _message.IsBodyHtml = false;
            _message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
            _message.Priority = MailPriority.High;
            _message.SubjectEncoding = System.Text.Encoding.UTF8;
            _message.BodyEncoding = System.Text.Encoding.UTF8;
        }

        private void PrepareServer()
        {
            _smtpClient.Host = "smtp.gmail.com";
            _smtpClient.Port = 587;
            _smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
            _smtpClient.UseDefaultCredentials = false;
            _smtpClient.Credentials = new NetworkCredential(tbFromAddr.ToString().Trim(), tbPassword.ToString().Trim());
            _smtpClient.EnableSsl = true;
        }

        private void PrepareSender()
        {
            sender.Message = _message;
            sender.smtpClient = _smtpClient;
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            PrepareMailMessage();
            PrepareServer();
            PrepareSender();
            //this.sender.Send();
            _smtpClient.Send(_message);
        }
    }
}

I'm getting the above error. I've looked at online solutions and I seem to have done all that is necessary but I'm still getting it. I've included Enablessl = true; Delivery method is Network and I've supplied the credentials. I debugged it and the username and password seem to be correct.

using System;
using System.Net;
using System.Net.Mail;
using System.Windows.Forms;

namespace SendMail
{
    public partial class MainWindow : Form
    {
        #region Private variables
        private MailMessage _message = new MailMessage();
        private EmailSender sender = EmailSender.GetInstance();
        private SmtpClient _smtpClient = new SmtpClient();
        #endregion

        public MainWindow()
        {
            InitializeComponent();
        }

        private void PrepareMailMessage()
        {
            // Set the FROM address
            _message.From = new MailAddress(tbFromAddr.ToString().Trim());

            // Set the TO address
            _message.To.Add(new MailAddress(tbToAddr.ToString().Trim()));

            // Set the SUBJECT
            _message.Subject = tbSubject.ToString();

            _message.IsBodyHtml = false;
            _message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
            _message.Priority = MailPriority.High;
            _message.SubjectEncoding = System.Text.Encoding.UTF8;
            _message.BodyEncoding = System.Text.Encoding.UTF8;
        }

        private void PrepareServer()
        {
            _smtpClient.Host = "smtp.gmail.com";
            _smtpClient.Port = 587;
            _smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
            _smtpClient.UseDefaultCredentials = false;
            _smtpClient.Credentials = new NetworkCredential(tbFromAddr.ToString().Trim(), tbPassword.ToString().Trim());
            _smtpClient.EnableSsl = true;
        }

        private void PrepareSender()
        {
            sender.Message = _message;
            sender.smtpClient = _smtpClient;
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            PrepareMailMessage();
            PrepareServer();
            PrepareSender();
            //this.sender.Send();
            _smtpClient.Send(_message);
        }
    }
}

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

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

发布评论

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

评论(1

小苏打饼 2024-12-21 07:26:42

我检查了你的代码,它对我有用。但是,我不确定 PrepareSender() 方法的用途。我在 PrepareMailMessage() 上包含了消息正文 (_message.Body)。

您是否尝试过使用默认凭据?

_smtpClient.UseDefaultCredentials = true

I checked your code and it worked for me. However, I am unsure about the purpose of the method PrepareSender(). I included message body(_message.Body) on PrepareMailMessage().

Did you tried using the default credentials?

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