Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use any email address in the FROM field while sending email through Gmail SMTP?

I'm trying to send an email using gmail SMTP in C# using the code bellow

MailMessage message = new MailMessage();
message.To.Add("my email");
message.Subject = "subject";
message.From = new MailAddress("any email");
message.Body = "body";

message.Attachments.Add(new System.Net.Mail.Attachment(path));
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential("my user", "my pass");

smtp.Send(message);

When I receive the email, the FROM field is filled with my user. I'm using UseDefaultCredentials as false. When I look to the result, the FROM field is filled with my user. Shouldn't the FROM field be filled with any email? How can I send an email using any email as sender?

like image 249
Victor Avatar asked Oct 08 '12 17:10

Victor


People also ask

Can you send email from alias?

Yes, you could now send emails using your alias address. Having an alias (proxy) address besides a primary mail address has always been a boon to every office admin/user.


1 Answers

I employ the very same to send email using GMail as a service. I originally set the .From property to "[email protected]" but the email still arrives with the From header set to the account used to authenticate.

Faced with this problem, I used the ReplyToList property (.ReplyToList.Add(MailAddress))) so that recipients that reply to the message will be sending the reply to an email account other than the "automated" one that we use to send outgoing messages.

Edit:

For more information, see this thread on Google Groups. Also, a related answer on Stack Overflow.

like image 184
JYelton Avatar answered Sep 23 '22 01:09

JYelton