gpt4 book ai didi

c# - SMTP 服务器需要安全连接或客户端未通过身份验证 - 仍无法正常工作

转载 作者:行者123 更新时间:2023-11-30 16:27:20 25 4
gpt4 key购买 nike

我遇到了上述错误。我查看了在线解决方案,我似乎已经完成了所有必要的工作,但我仍然无法解决。我包括了 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);
}
}
}

最佳答案

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

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

_smtpClient.UseDefaultCredentials = true

关于c# - SMTP 服务器需要安全连接或客户端未通过身份验证 - 仍无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8016264/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com