C#自定义MailAddress.From字段不起作用

时间:2016-05-24 23:35:42

标签: c# email smtp gmail

我正在尝试使用C#和googles smtp服务从我的自定义地址向用户发送电子邮件。我的问题是,当用户收到消息时,它会在电子邮件的from部分显示admin@customDomain.co.uk <myAddress@gmail.com>

我的问题是我希望用户看到admin@customDomain.co.uk <admin@customDomain.co.uk>。这可能使用谷歌服务吗?或者我应该看一些其他选择?如果是这样的话?

我已尝试this线程上的答案,但没有工作,我也找到了google blog post,但我无法解决它如何解决我的问题

我的代码

    string SmtpAddress = "smtp.gmail.com";
    int MyPort = 587;
    bool enableSSL = true;
    string MyPassword = "somePassword";
    string MyUsername = "aUsername";

    public string EmailFrom = "admin@customDomain.co.uk";
    public string Subject = "Test Subject";

public void Send(string body, BL.Customer customer)
    {
        using (MailMessage message = new MailMessage())
        {
            message.To.Add(new MailAddress(customer.EmailAddress));
            if (!String.IsNullOrEmpty(customer.SCEmailAddress))
            {
                //message.To.Add(new MailAddress(customer.SCEmailAddress));
            }
            message.From = new MailAddress(EmailFrom, EmailFrom);
            message.Subject = Subject;
            message.Body = string.Format(body);
            message.IsBodyHtml = true;

            using (var smtp = new SmtpClient())
            {
                var credential = new NetworkCredential
                {
                    UserName = MyUsername,
                    Password = MyPassword
                };
                smtp.Credentials = credential;
                smtp.Host = SmtpAddress;
                smtp.Port = MyPort;
                smtp.EnableSsl = enableSSL;
                try
                {
                    smtp.Send(message);
                }
                catch (SmtpException se)
                {
                    throw se;
                }
            }
        }
    }

0 个答案:

没有答案