开发者

Don't show "Display Name" at sending e-mail

I send 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, 开发者_JAVA技巧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 there is no Display Name ("Name Name") at getting letter, only e-mail.

Do you have any idea of what could cause this issue?

I'm sure, the email client isn't ignoring the display name! Client is Outlook.

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


I've had trouble with this issue as well, but found that MailAddress also takes a third encoding argument. So 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 you can use. I use gmail and after enabling the access for Less Secure Apps, it works for all of these after using the SmtpClient.


I had the same issue and it seems that providing the credentials caused the server to use those for the 'from' name.

I had to comment out:

//client.UseDefaultCredentials = true;


    /// <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 poor. haha....

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

DisplayName : "Const.SYS_NAME ", i think this is you want.


You can set the DisplayName property of the MailAddress object to set the regular name, instead of just using the email address.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜