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?
Having run your code snippet I get:
Senderis the email address used to authenticate with Google Mail.Fromis the “from” provided in code. The receiving application might be confusing the two, and the rest looks just as expected. Some mail clients present the From+Sender (when they are different) as “sent by Sender on behalf of From”.You might be concerned with the fact that Google Mail still reveal the account from which the email is sent, through
Senderfield, but this is how it works. You do send from this account.And, another possible reason is the
Frommail address itself. If you added it to your Google Mail account as one of your own addresses (and confirmed via test email with a link), then Google Mail will allow putting it ontoFromfield. Otherwise it might drop it and replace it with theSender.