Bellow是我的代码,当我删除MailAddress()的DisplayName属性时,它工作正常,但接收结束邮件在显示名称上显示EmailID,如emailID@gmail.com。
MailMessage mail = new MailMessage();
mail.From = new System.Net.Mail.MailAddress("emailID@domainName.com");
// The important part -- configuring the SMTP client
SmtpClient smtp = new SmtpClient();
smtp.Port = 587; // [1] You can try with 465 also, I always used 587 and got success
smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network; // [2] Added this
smtp.UseDefaultCredentials = false; // [3] Changed this
smtp.Credentials = new NetworkCredential(mail.From.ToString(), "password"); // [4] Added this. Note, first parameter is NOT string.
smtp.Host = "smtp.gmail.com";
mail.IsBodyHtml = true;
mail.Subject = Subject;
mail.Body = Body;
mail.To.Add(new MailAddress(To));
smtp.Send(mail);
mail.Dispose();
当我添加MailAddress()的显示名称时,我收到此错误消息。
System.Net.Mail.SmtpException:SMTP服务器需要安全 连接或客户端未经过身份验证。服务器响应 是:5.5.1需要身份验证。了解更多信息 System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, 字符串响应)在System.Net.Mail.SmtpTransport.SendMail(MailAddress sender,MailAddressCollection收件人,String deliveryNotify, Boolean allowUnicode,SmtpFailedRecipientException&例外) 在***
的System.Net.Mail.SmtpClient.Send(MailMessage消息)
我哪里错了?
答案 0 :(得分:0)
var client = new SmtpClient
{
Host = "smtp.googlemail.com",
Port = 587,
UseDefaultCredentials = false,
DeliveryMethod = SmtpDeliveryMethod.Network,
EnableSsl = true,
Credentials = new NetworkCredential(SmtpUserName, SmtpUserPassword);
};
client.Send(mail);
其中mail
是MailMessage
private static SecureString _smtpPassword;
public static SecureString SmtpUserPassword
{
get
{
if (_smtpPassword != null)
return _smtpPassword;
_smtpPassword = new SecureString();
foreach (var c in "your password here")
_smtpPassword.AppendChar(c);
_smtpPassword.MakeReadOnly();
return _smtpPassword;
}
}