简体   繁体   中英

Send an email using SMTP and control sender address

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM