Send an email using SMTP and control sender address(使用 SMTP 发送电子邮件并控制发件人地址)
问题描述
我正在尝试使用 c# App 发送电子邮件,下一个代码正在运行.
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);
但 Gmail 的 SMTP 服务器将 gmail 电子邮件地址 (Ryan.White@gmail.com) 作为发件人,
But Gmail's SMTP server puts gmail e-mail address (Ryan.White@gmail.com) as the sender,
不管 MSG.FROM 地址 (Sender.name@gmail.com).
regardless the MSG.FROM address (Sender.name@gmail.com).
是否可以使用 C#/.NET 发送电子邮件并控制发件人地址?
或者发送一封不经过身份验证的电子邮件?
Or alternatively send an email without authentication?
我知道在 UNIX 中,您可以在邮件"命令中控制发件人地址.
I know that in UNIX you can control the sender address in 'Mail' command.
推荐答案
Gmail 这样做是出于安全原因,否则垃圾邮件发送者很容易发送看似来自虚假地址的电子邮件.
Gmail is doing that for security reasons, otherwise it would be easy for spammers to send emails that appear to come from fake addresses.
您已正确编码,C# 将尝试将发件人地址设置为 Sender.Name@gmail.com,但 SMTP 服务器拥有最终决定权.如果您有权以其他用户的身份发送邮件,这将起作用,例如在 Exchange 服务器环境中您被验证为管理员的情况.但是,Gmail 似乎不允许这样做.您需要以 Sender.name 身份登录才能以该用户身份发送电子邮件.
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.
这篇关于使用 SMTP 发送电子邮件并控制发件人地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 SMTP 发送电子邮件并控制发件人地址


基础教程推荐
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01