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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With