简体   繁体   中英

smtp email from name not working

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM