change sender address when sending mail through gmail in c#
Asked Answered
M

5

28

I have used the following code to send mail from my web application using a gmail account. My question is, can i change the sender address to another address other than original sender(gmail) address? My code is as follows:

System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
System.Net.NetworkCredential cred = new System.Net.NetworkCredential("[email protected]", "*******");

Whatever i do is useless as i always receive mail from [email protected]. Is it possible to change it?

I have changed to mail.From = new System.Net.Mail.MailAddress("[email protected]"); but i received the mail with the from address [email protected] and not from the new "From" address. I think gmail smtp overwrites the from address with the original credential.

Masseur answered 6/10, 2010 at 10:32 Comment(0)
I
35

Gmail doesn't allow you to change the FROM to something different than your gmail account.

It doesn't matter what you use, they over-write it, before they relay it on. This prevent spamming/spoofing.

Irrepealable answered 6/10, 2010 at 13:21 Comment(2)
Not true, see my comment below. It is possible, as long as you have the other account added as an alias.Stclair
This is not the correct answer, I implemented philip and Simon_Weaver's answers together today and was able to successfully send email from a G Suite alias using the account's main login details (which are different from the alias).Alsacelorraine
L
41

This is the solution:

  1. use the codes above to set mail.From = new MailAddress(address, display name)
  2. in Gmail, go to Mail Settings >> Accounts and Import.
  3. Add the email account you will use as sender in "Send Mail As". (tick as Alias)

This works for me

Lowney answered 4/1, 2012 at 13:5 Comment(3)
Much better answer. It is possible to change the From in Gmail.Columbium
Perfect. Thanks a lot!Daylong
This is a much better answer. Thanks.Chrysoberyl
I
35

Gmail doesn't allow you to change the FROM to something different than your gmail account.

It doesn't matter what you use, they over-write it, before they relay it on. This prevent spamming/spoofing.

Irrepealable answered 6/10, 2010 at 13:21 Comment(2)
Not true, see my comment below. It is possible, as long as you have the other account added as an alias.Stclair
This is not the correct answer, I implemented philip and Simon_Weaver's answers together today and was able to successfully send email from a G Suite alias using the account's main login details (which are different from the alias).Alsacelorraine
N
5

Yes just use the From property of the MailMessage

eg.

mail.From = "[email protected]";

EDIT: Also, see this post for more detailed info on how to emails via gmail in C#

Sending email in .NET through Gmail

EDIT: Although this works for mail in general, it appears this won't work for gmail as google overwrite it before its sent (see @Dave wanta's answer)

Needleful answered 6/10, 2010 at 10:34 Comment(2)
While you're right in general, when specifically sending mail through Google SMTP servers the "From:" property is overwritten.Matchwood
This is not true, you CAN use a different from address using Google SMTP. Just make sure it is an alias (as w69rdy points out) and that you follow the RFC 2822 specification (my mistake was having a space between the email and name brackets, it should be [email protected]<Test User>). The sender address will NOT be changed by Google, but you can influence this because it will use the account marked as default in your gmail settings.Stclair
L
2

If you have a limited number of senders you can do as @philip suggested. For instance you may have [email protected], [email protected] and [email protected] or even [email protected]. As long as they are approved senders on the actual gmail.com website you can send from them.

Gmail.com : Sending mail from a different address

If you are expecting to send from an arbitrary user (such as a customer service form on a website where the user enters their email and you don't want them emailing you directly) about the best you can do is this :

        msg.ReplyToList.Add(new System.Net.Mail.MailAddress(email, friendlyName));

If you're in a controlled environment this works great, but please note that I've seen some email clients send to the from address even when reply-to is specified (I don't know which).

Lallation answered 7/7, 2013 at 20:46 Comment(0)
S
1

Check #56 and #58. They might be relevant to what you want to do https://code.google.com/p/google-apps-script-issues/issues/detail?id=172

Submerged answered 19/3, 2014 at 14:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.