Send an email using SMTP and control sender address

心不动则不痛 提交于 2019-12-24 02:13:59

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/6209163/send-an-email-using-smtp-and-control-sender-address

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!