SMTP 服务器需要安全连接或客户端未经过身份验证 - 仍然无法工作
我收到上述错误。我查看了在线解决方案,似乎已经完成了所有必要的操作,但我仍然明白。我已经包含了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我检查了你的代码,它对我有用。但是,我不确定
PrepareSender()
方法的用途。我在PrepareMailMessage()
上包含了消息正文 (_message.Body
)。您是否尝试过使用默认凭据?
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
) onPrepareMailMessage()
.Did you tried using the default credentials?