Question

I have a contact us page set up on my website and I would like to make it appear like it is coming from a different email address.

<mailSettings>
  <smtp from="info@magazine.com">
    <network host="smtp.gmail.com" enableSsl="true" port="587" userName="admin@magazine.com" password="password" defaultCredentials="false" />
  </smtp>
</mailSettings>

So I make my mail client and try and send the email

 MailMessage message = new MailMessage();

 message.To.Add(new MailAddress(ConfigurationManager.AppSettings["ContactEmailTo"]));

 message.Subject = "Contact Request";
 message.Body = body;

 SmtpClient client = new SmtpClient();
 client.Send(message);

However, when I receive the email, I receive it from the admin address.

Était-ce utile?

La solution

Use one of the constructor overloads on MailAddress to specify an address and display name like so:

MailAddress address = new MailAddress("user@website.com", "John Smith");

See: http://msdn.microsoft.com/en-us/library/1s17zfkf%28v=vs.110%29.aspx for more info.

Autres conseils

It looks like you're sending through GMail. Their SMTP server rewrites the FROM address. See for example this question.

If you want to use GMail, this answer suggests adding the FROM email in your account settings under:

Settings -> Accounts -> Send mail as -> Add another email address you own

The other option is to use a different SMTP server that allows you to set your FROM address.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top