我正在更新一些使用 .Net 的 SMTP 类发送简单电子邮件的现有代码。示例代码如下。 SMTP 主机是 google,登录信息包含在 App.config 中,如下所示(显然不是真正的登录信息:))。
我遇到的问题是,我无法在谷歌搜索中找到任何答案,是我无法覆盖网络元素的“用户名”属性中包含的“发件人”电子邮件地址的显示在发送的电子邮件的配置中。
在下面明确设置 myMailMessage 对象的 From 属性的行中,收到电子邮件时不会显示该值“[email protected] ”。它仍然在网络标记中显示为“[email protected] ”。但是,发件人姓名“Sparky”确实出现在电子邮件中。
我尝试将自定义“From” header 添加到 myMailMessage 的 Header 属性中,但这也不起作用。
是否可以使用网络标记凭据登录到 smtp 服务器(如下所示),但在实际收到的电子邮件中会覆盖显示的发件人电子邮件地址?
示例代码:
MailMessage myMailMessage = new MailMessage();
myMailMessage.Subject = "My New Mail";
myMailMessage.Body = "This is my test mail to check";
myMailMessage.From = new MailAddress("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c192b1a0b3aab8818cb885aeaca0a8afefa2aeac" rel="noreferrer noopener nofollow">[email protected]</a>", "Sparky");
myMailMessage.To.Add(new MailAddress("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="22504741474b544750626f5b664d4f434b4c0c414d4f" rel="noreferrer noopener nofollow">[email protected]</a>", "receiver name"));
SmtpClient mySmtpClient = new SmtpClient();
mySmtpClient.Send(myMailMessage);
在App.config中:
<system.net>
<mailSettings>
<smtp deliveryMethod="Network" from="<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bedbccccd1cccdfed3c7dad1d3dfd7d090ddd1d3" rel="noreferrer noopener nofollow">[email protected]</a>">
<network host="smtp.gmail.com" port="587" userName="<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="305542425f4245435542705d49545f5d51595e1e535f5d" rel="noreferrer noopener nofollow">[email protected]</a>" password="mypassword" defaultCredentials="false"/>
</smtp>
</mailSettings>
</system.net>
最佳答案
这取决于 SMTP 服务器。 Google 的 SMTP 服务器不允许您创建自己的“发件人”字段,但我发现其他 SMTP 服务器可以。您是否需要使用 Google 的服务器?
关于c# - 无法使用 .config 登录凭据覆盖 MailMessage 类中的 "From"地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2904988/