1

I am trying to send an email address in my application as someone (email should show up as if it were sent from that), but anytime I send the email, the name shows up as I supplied, but no matter what I do, the email address is what I am using to authenticate.

Is there any way to have the email to appear as if it is coming from someone I specify or will it always show up as coming from the authenticated email?

Here is what I have...

        using (var message = new MailMessage()
        {
            From = From != new MailAddress(From.Email, From.FormalName),
            Subject = Subject,
            Body = Body
        })
        {
            if (To != null)
            {
                foreach (var address in To)
                {
                    message.To.Add(new MailAddress(address.Email, address.FormalName));
                }
            }
            if (CC != null)
            {
                foreach (var address in CC)
                {
                    message.CC.Add(new MailAddress(address.Email, address.FormalName));
                }
            }
            var smtp = new SmtpClient
            {
                Host = "smtp.gmail.com",
                Port = 587,
                EnableSsl = true,
                DeliveryMethod = SmtpDeliveryMethod.Network,
                UseDefaultCredentials = false,
                Credentials = new NetworkCredential("myaccount", "mypassword")
            };
            message.IsBodyHtml = true;
            message.Headers.Add("Reply-To", message.From.Address);
            smtp.Send(message);
        }
Benny
  • 3,899
  • 8
  • 46
  • 81

2 Answers2

0

It looks like you're expecting the Reply-To: header to indicate who the message is from. In fact, there is a header called From: for this purpose. So try:

  message.Headers.Add("From", message.From.Address);

Do note that Gmail will probably add a Sender: header (that may or may not be shown by the recipient's email client) that reflects the actual account you used to send the message.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • Can you view headers on the receiving end and confirm that what you believe you put in there actually *is* in there? – Greg Hewgill Jan 06 '12 at 03:54
  • The From header is definitely being changed on me...it's different in the actual message received than what I'm seeing while debugging. – Benny Jan 06 '12 at 03:59
0

Best practice when sending automated emails from a specific user is to use the 'on behalf of' syntax (ReplyTo or Sender in MailMessage) in conjunction with a from address from your system see Sending "on behalf of" emails

however you can put the address straight into from as a string so long as your mail server supports it and the receiving mail server does not do a reverse look-up

Community
  • 1
  • 1
undefined
  • 33,537
  • 22
  • 129
  • 198
  • I tried specifying all three (From, Sender, and ReplyTo) and same thing – Benny Jan 06 '12 at 03:52
  • I think your probem is prehaps that you are using GMail as your SMTP server. It looks like GMail will only allow you to relay email which is from your own address. Try putting your gmail address in the from field and leaving the others blank. see http://support.google.com/mail/bin/answer.py?hl=en&answer=22370&ctx=cb&src=cb&cbid=-ypwk65f3hnjg&cbrank=3 for details on smtp permisssions – undefined Jan 06 '12 at 03:58