我想使用SmtpClient类发送邮件,但它不起作用.
码:
SmtpClient smtpClient = new SmtpClient();
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new System.Net.NetworkCredential(obMailSetting.UserName, obMailSetting.Password);
smtpClient.Host = obMailSetting.HostMail;
smtpClient.Port = obMailSetting.Port;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.EnableSsl = obMailSetting.Connect_Security;//true
//smtpClient.UseDefaultCredentials = true;//It would work if i uncomment this line
smtpClient.Send(email);
它引发了一个异常:
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.1 Client was not authenticated
我确定用户名和密码是正确的.我的代码有问题吗?
谢谢.
最佳答案 你可以试试这个:
MailMessage mail = new MailMessage();
mail.Subject = "Your Subject";
mail.From = new MailAddress("senderMailAddress");
mail.To.Add("ReceiverMailAddress");
mail.Body = "Hello! your mail content goes here...";
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.EnableSsl = true;
NetworkCredential netCre =
new NetworkCredential("SenderMailAddress","SenderPassword" );
smtp.Credentials = netCre;
try
{
smtp.Send(mail);
}
catch (Exception ex)
{
// Handle exception here
}
你可以试试this:
在Exchange管理控制台的“服务器配置”节点下,选择“集线器传输”和服务器的名称.在“接收连接器”窗格中,打开任一个Recive连接器(我的默认安装创建2),或者您可以为TFS创建一个新的连接器(我也试过这个并且它工作正常).对于任何这些连接器,请打开“属性”,然后在“权限组”选项卡上确保选中“匿名用户”(默认情况下不是这样).
要么
您也可以通过初始化SmtpClient尝试this,如下所示:
SmtpClient smtp = new SmtpClient("127.0.0.1");