MailAddress类需要更多标头才能可靠

时间:2011-11-09 19:22:46

标签: .net

我想在我的网站上发送邮件。另外,想要使MailMessage.From属性友好。此外,除了gmail,我无法发送消息。 但是使用gmail,我可以发送消息hotmail等等。即使使用gmail,也无法发送友好名称的邮件。由于这些,我认为MailMessage.Headers属性没有足够的标头可靠,它不是传输的跨邮件服务。

  

CSharp代码:

public static void SendMail(string subject, string body, params string[] to)
    {
        string password = "********";
        string userName = "userName@host.com";
        bool isBodyHtml = true;
        string from = "\"displayName\" <userName@host.com>";
        int port = 587;
        string host = "smtp.host.com";
        bool enableSSL = true;
        var credential = new System.Net.NetworkCredential();
            credential.Password = password;
            credential.UserName = userName;
            using (System.Net.Mail.MailMessage mm = new System.Net.Mail.MailMessage())
            {
                mm.Subject = subject;
                mm.Body = body;
                mm.IsBodyHtml = isBodyHtml;
                for (int i = 0; i < to.Count(); i++)
                {
                    mm.To.Add(to[i]);
                }
                mm.From = new System.Net.Mail.MailAddress(from);
                var smtp = new System.Net.Mail.SmtpClient();
                    smtp.UseDefaultCredentials = false;
                    smtp.Credentials = credential;
                    smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
                    smtp.EnableSsl = enableSSL;
                    smtp.Port = port;
                    smtp.Host = host;
                    smtp.Send(mm);
            }
    }

1 个答案:

答案 0 :(得分:-1)

签出MailAddress Constructor (String, String)第一个参数是电子邮件地址,第二个参数是电子邮件地址的显示名称。