Do not show Display Name when sending email

I am sending mail from my site (.NET Framework 2.0, IIS 7) as

MailAddress from = new MailAddress(" from@email.com ", "Name Name"); MailAddress to = new MailAddress(" to@email.com ", ""); MailMessage mm = new MailMessage(from, to); mm.Subject = subject; mm.Body = body; using ( mm ) { if (attach != null) mm.Attachments.Add(attach); mm.IsBodyHtml = true; SmtpClient smtp = new SmtpClient(mailServer); if (!string.IsNullOrEmpty(mailPort)) smtp.Port = int.Parse(mailPort); smtp.Credentials = new System.Net.NetworkCredential(username, pass); smtp.Send(mm); } 

But upon receipt of the letter there is no display name ("Name Name"), only email.

Do you have any idea what might cause this problem?

I am sure that the mail client does not ignore the display name! The client is Outlook.

When transferring the mm transfer object to the server, the From property is {"Name Name" < from@email.com >} . Why does the server delete the name?

+6
source share
4 answers

I also had problems with this problem, but found that MailAddress also accepts the third encoding argument. Therefore, instead of using:

 MailAddress from = new MailAddress(" from@email.com ", "Name Name"); 

Try some of these guys:

 MailAddress from = new MailAddress(" from@email.com ", "Name Name", Encoding.ASCII); MailAddress from = new MailAddress(" from@email.com ", "Name Name", Encoding.UTF8); MailAddress from = new MailAddress(" from@email.com ", "Name Name", Encoding.Unicode); 

This list is not limiting, there are other encoding options that you can use. I use gmail and after enabling access for Less Secure Apps , it works for all of them after using SmtpClient.

+2
source
  /// <summary> /// SMTP服务发送邮件/// </summary> /// <param name="mailSubjct">邮件主题</param> /// <param name="mailBody">邮件内容(html)</param> /// <param name="mailFrom">发件地址</param> /// <param name="mailAddress">收件地址</param> /// <param name="hostIp">SMTP主机IP</param> /// <param name="username">SMTP用户名</param> /// <param name="password">SMTP密码</param> /// <param name="ssl">是否启用SSL方式</param> /// <param name="port">SMTP端口</param> /// <returns></returns> public static string SendMail(string mailSubjct, string mailBody, string mailFrom, List<string> mailAddress, string hostIp, string username, string password, bool ssl, int port) { string error = string.Empty; try { MailMessage mm = new MailMessage(); mm.IsBodyHtml = false; mm.Subject = mailSubjct; mm.BodyEncoding = Encoding.UTF8; mm.Body = mailBody; mm.IsBodyHtml = true; mm.From = new MailAddress(mailFrom, Const.SYS_NAME, Encoding.UTF8); Regex regex = new Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"); for (int i = 0; i < mailAddress.Count; i++) { if (regex.IsMatch(mailAddress[i])) { mm.To.Add(mailAddress[i]); } } if (mm.To.Count == 0) { return string.Empty; } SmtpClient sc = new SmtpClient(); NetworkCredential nc = new NetworkCredential(username, password); sc.EnableSsl = ssl; sc.UseDefaultCredentials = false; sc.Credentials = nc; sc.DeliveryMethod = SmtpDeliveryMethod.Network; sc.Host = hostIp; sc.Port = port; sc.Send(mm); } catch (Exception ex) { error = ex.Message; } return error; } 

I am Chinese and my English is bad. ha ha ....

int this line, second parameter: "mm.From = new MailAddress (mailFrom, Const.SYS_NAME, Encoding.UTF8);"

DisplayName: "Const.SYS_NAME", I think you need it.

0
source

I had the same problem, and it seems that providing credentials forced the server to use those for the name 'from'.

I had to comment:

 //client.UseDefaultCredentials = true; 
0
source

You can set the DisplayName property of the MailAddress to a common name instead of just using an email address.

 MailAddress fromAddress = new MailAddress(" thedude@lebowski.net "); fromAddress.DisplayName = "The Dude"; 
-1
source

Source: https://habr.com/ru/post/890203/


All Articles