0

I am trying to send Emails from my custom address to users using C# and googles smtp service. My problem is that when the when the user receives the message it is displayed admin@customDomain.co.uk <myAddress@gmail.com> in the from section of the email.

My problem is that I want the user to see admin@customDomain.co.uk <admin@customDomain.co.uk>. Is this possible using the google service? Or should I look at some other option? If so what?

I have tried the answers on this thread but non worked and I also found this google blog post but I cant work out how it can solve my issue

My Code

    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;
                }
            }
        }
    }
Community
  • 1
  • 1
tony09uk
  • 2,841
  • 9
  • 45
  • 71
  • try using the EmailAddress class. – Dr. Stitch May 24 '16 at 23:39
  • Possible duplicate of [Sending email in .NET through Gmail](http://stackoverflow.com/questions/32260/sending-email-in-net-through-gmail) – Dr. Stitch May 24 '16 at 23:39
  • The problem is that Google (or more accurately, your account) doesn't have "permission" to send from that address so it uses your logged in address instead. If you want to send email from a particular address, you will almost certainly have to use a commercial ail provider and prove that you own the domain too. – DavidG May 24 '16 at 23:45

0 Answers0