Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

change sender address when sending mail through gmail in c#

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.

like image 909
ACP Avatar asked Oct 06 '10 10:10

ACP


People also ask

Can you change the sender of an email?

Change the Email Sender Name in OutlookGo to File and select Info. Select Account Settings > Account Settings. In the Account Settings dialog box, choose the email account you want to edit and select Change. In the Your name text box, enter the name you want to appear in the From line of your emails.

How do I send an email from an alias in Gmail?

In the "Send mail as," click Add another email address. In the window that opens, enter the name you want recipients to view. Enter the email address alias you've set up for email forwarding. Confirm that "Treat as an alias" is marked, and click Next step.


1 Answers

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

like image 62
philip Avatar answered Oct 17 '22 19:10

philip