Send an email using SMTP and control sender addres

2019-07-24 03:37发布

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("Sender.name@gmail.com");
Msg.To.Add(new MailAddress("Ryan.White@gmail.com"));
Msg.Subject = "testSub";
Msg.Body = "testBody";

MailClient.Send(Msg);

But Gmail's SMTP server puts gmail e-mail address (Ryan.White@gmail.com) as the sender,

regardless the MSG.FROM address (Sender.name@gmail.com).

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.

标签: c# .net smtp
1条回答
放我归山
2楼-- · 2019-07-24 04:33

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 Sender.Name@gmail.com, 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.

查看更多
登录 后发表回答