在c#中通过gmail发送邮件时更改发件人地址
我已使用以下代码使用 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 tomail.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
解决方法是这样的:
这对我有用
This is the solution:
This works for me
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.
是的,只需使用 From 属性MailMessage
例如。
编辑:另外,请参阅这篇文章,了解有关如何在 C# 中通过 gmail 发送电子邮件的更多详细信息
通过 Gmail 在 .NET 中发送电子邮件
编辑: 虽然这通常适用于邮件,但似乎不适用于 Gmail,因为 google 在发送之前会覆盖它(参见@Dave Wanta的回答)
Yes just use the From property of the MailMessage
eg.
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)
如果您的发件人数量有限,您可以按照@philip 的建议进行操作。例如,您可能有
[电子邮件受保护]
、 [电子邮件受保护] 和[电子邮件受保护]
甚至[电子邮件受保护]
。只要他们是实际gmail.com
网站上批准的发件人,您就可以通过他们发送邮件。Gmail.com:从其他地址发送邮件
如果您希望从任意用户处发送邮件(例如用户输入电子邮件的网站上的客户服务表格,并且您不希望他们直接向您发送电子邮件),那么您能做的最好的事情是:
如果您'在受控环境中,这效果很好,但请注意,我看到一些电子邮件客户端即使指定了回复(我不知道是哪一个)也会发送到发件人地址。
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 actualgmail.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 :
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).
检查#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