Reputation: 73
I'm trying to send an email using c# App, the next code is working.
SmtpClient MailClient = new SmtpClient("smtp.gmail.com");
MailClient.EnableSsl = false;
MailClient.Credentials = new NetworkCredential("Ryan.White", "Password");
MailMessage Msg = new MailMessage();
Msg.From = new MailAddress("[email protected]");
Msg.To.Add(new MailAddress("[email protected]"));
Msg.Subject = "testSub";
Msg.Body = "testBody";
MailClient.Send(Msg);
But Gmail's SMTP server puts gmail e-mail address ([email protected]) as the sender,
regardless the MSG.FROM address ([email protected]).
Is it possible to send an email and control the sender address using C#/.NET ?
Or alternatively send an email without authentication?
I know that in UNIX you can control the sender address in 'Mail' command.
Upvotes: 3
Views: 6229
Reputation: 22266
Gmail is doing that for security reasons, otherwise it would be easy for spammers to send emails that appear to come from fake addresses.
You have coded this correctly, and C# will attempt to set the from address as [email protected], but the SMTP server has the final say. If you had authorization to send as another user, this would work, like in the case of an Exchange server environment where you are authenticated as an admin. However, Gmail doesn't seem to allow this. You would need to login as Sender.name to be able to send emails as that user.
Upvotes: 6