下面是我删除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();
当我添加显示名称的邮件地址()我收到这个错误消息。
系统网邮政SmtpException:SMTP服务器需要安全连接,或者客户端未通过身份验证。服务器响应为:5.5。1需要身份验证。在系统中了解更多信息。网邮政MailCommand。系统上的CheckResponse(SmtpStatusCode状态码、字符串响应)。网邮政短程运输。SendMail(MailAddress发件人、MailAddressCollection收件人、字符串deliveryNotify、布尔AllowInCode、SmtpFailedRecipientException
我错在哪里?
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;
}
}