C# 从Gmail发送电子邮件DisplayName属性出错

C# 从Gmail发送电子邮件DisplayName属性出错,c#,email,gmail,C#,Email,Gmail,下面是我删除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

下面是我删除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服务器需要安全的 连接或客户端未通过身份验证。服务器响应 was:5.5.1需要身份验证。了解更多信息,请访问 System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode状态码, 字符串响应)在System.Net.Mail.SmtpTransport.SendMail(邮件地址 发件人、MailAddressCollection收件人、字符串deliveryNotify、, 布尔AllowInCode、SmtpFailedRecipientException和exception)位于 System.Net.Mail.SmtpClient.Send(MailMessage)位于***

我错在哪里

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;
        }
    }

谢谢你的快速回复。您在哪里提到电子邮件的显示名称?我不确定我是否理解您的问题。你说的移除是什么意思?DisplayName是我添加此邮件时的GET属性。From=new System.Net.mail.MailAddress(“emailID@domainName.com“,”姓名“);接收以上错误消息我从错误消息中读取的内容原因是身份验证。在大多数情况下,UserNam/Password中存在问题。请参阅我关于设置用户密码的更新。