在c#中通过gmail发送邮件时更改发件人地址

发布于 2024-09-26 11:17:54 字数 985 浏览 7 评论 0原文

我已使用以下代码使用 Gmail 帐户从我的 Web 应用程序发送邮件。我的问题是,我可以将发件人地址更改为原始发件人(gmail)地址以外的其他地址吗? 我的代码如下:

System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
System.Net.NetworkCredential cred = new System.Net.NetworkCredential("[email protected]", "*******");

无论我做什么都是无用的,因为我总是收到来自 [ 的邮件电子邮件受保护]。可以改变吗?

我已经改为 mail.From = new System.Net.Mail.MailAddress("[电子邮件受保护]"); 但我收到了发件人地址为 [电子邮件受保护],而不是来自新的“发件人”地址。我认为 gmail smtp 会用原始凭据覆盖发件人地址。

I have used the following code to send mail from my web application using a gmail account. My question is, can i change the sender address to another address other than original sender(gmail) address?
My code is as follows:

System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
System.Net.NetworkCredential cred = new System.Net.NetworkCredential("[email protected]", "*******");

Whatever i do is useless as i always receive mail from [email protected]. Is it possible to change it?

I have changed to
mail.From = new System.Net.Mail.MailAddress("[email protected]"); but i received the mail with the from address [email protected] and not from the new "From" address. I think gmail smtp overwrites the from address with the original credential.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

倾其所爱 2024-10-03 11:17:54

解决方法是这样的:

  1. 使用上面的代码在Gmail中设置mail.From = new MailAddress(address, display name)
  2. ,进入邮件设置>>>帐户和导入。
  3. 在“邮件发送方式”中添加您将用作发件人的电子邮件帐户。 (勾选别名)

这对我有用

This is the solution:

  1. use the codes above to set mail.From = new MailAddress(address, display name)
  2. in Gmail, go to Mail Settings >> Accounts and Import.
  3. Add the email account you will use as sender in "Send Mail As". (tick as Alias)

This works for me

再见回来 2024-10-03 11:17:54

Gmail 不允许您将 FROM 更改为与您的 Gmail 帐户不同的内容。

无论你使用什么,他们都会在转发之前重写它。这可以防止垃圾邮件/欺骗。

Gmail doesn't allow you to change the FROM to something different than your gmail account.

It doesn't matter what you use, they over-write it, before they relay it on. This prevent spamming/spoofing.

终难愈 2024-10-03 11:17:54

是的,只需使用 From 属性MailMessage

例如。

mail.From = "[email protected]";

编辑:另外,请参阅这篇文章,了解有关如何在 C# 中通过 gmail 发送电子邮件的更多详细信息

通过 Gmail 在 .NET 中发送电子邮件

编辑: 虽然这通常适用于邮件,但似乎不适用于 Gmail,因为 google 在发送之前会覆盖它(参见@Dave Wanta的回答)

Yes just use the From property of the MailMessage

eg.

mail.From = "[email protected]";

EDIT: Also, see this post for more detailed info on how to emails via gmail in C#

Sending email in .NET through Gmail

EDIT: Although this works for mail in general, it appears this won't work for gmail as google overwrite it before its sent (see @Dave wanta's answer)

白云不回头 2024-10-03 11:17:54

如果您的发件人数量有限,您可以按照@philip 的建议进行操作。例如,您可能有 [电子邮件受保护][电子邮件受保护][电子邮件受保护] 甚至 [电子邮件受保护]。只要他们是实际 gmail.com 网站上批准的发件人,您就可以通过他们发送邮件。

Gmail.com:从其他地址发送邮件

如果您希望从任意用户处发送邮件(例如用户输入电子邮件的网站上的客户服务表格,并且您不希望他们直接向您发送电子邮件),那么您能做的最好的事情是:

        msg.ReplyToList.Add(new System.Net.Mail.MailAddress(email, friendlyName));

如果您'在受控环境中,这效果很好,但请注意,我看到一些电子邮件客户端即使指定了回复(我不知道是哪一个)也会发送到发件人地址。

If you have a limited number of senders you can do as @philip suggested. For instance you may have [email protected], [email protected] and [email protected] or even [email protected]. As long as they are approved senders on the actual gmail.com website you can send from them.

Gmail.com : Sending mail from a different address

If you are expecting to send from an arbitrary user (such as a customer service form on a website where the user enters their email and you don't want them emailing you directly) about the best you can do is this :

        msg.ReplyToList.Add(new System.Net.Mail.MailAddress(email, friendlyName));

If you're in a controlled environment this works great, but please note that I've seen some email clients send to the from address even when reply-to is specified (I don't know which).

_蜘蛛 2024-10-03 11:17:54

检查#56 和#58。它们可能与您想做的事情相关
https://code.google.com/p /google-apps-script-issues/issues/detail?id=172

Check #56 and #58. They might be relevant to what you want to do
https://code.google.com/p/google-apps-script-issues/issues/detail?id=172

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文