如何在 ASP.Net 中从 google、yahoo 或其他邮件域发送邮件?
我有一个“联系我们”页面,用户将在其中提供他们的电子邮件 ID 和查询,并在提交表单后,网络管理员将收到该电子邮件。
如果我将他们的电子邮件 ID 配置为“来自”MailAddress 并发送邮件,如果该 ID 来自流行的邮件域(如 gmail 或 hotmail)但可以与其他不受欢迎或不存在的域(如 [电子邮件受保护],无需提供任何凭据!
在我正确配置 SMTP 和网络凭据后,它可以与 Gmail 一起使用。 目的是让收到电子邮件的我的网站管理员能够点击邮件客户端中的回复按钮,并看到“联系我们”页面中填充的“发件人”字段填充的“收件人”字段。 是否有任何正确的方法来做到这一点或完成它的提示或技巧。
这是我的代码
MailMessage emailMessage = new MailMessage();
MailAddress emailTo = new MailAddress("[email protected]", "Web Dev");
MailAddress emailFrom = new MailAddress(tbEmail.Text);
SmtpClient localhost = new SmtpClient("localhost");
emailMessage.To.Add(emailTo);
emailMessage.From = emailFrom;
emailMessage.Subject = "Enquiry / Feedback";
emailMessage.Body = "Name: " + tbName.Text +
"\nAddress: " + tbEmail.Text +
"\nComments: " + tbComments.Text;//emails body
localhost.Send(emailMessage);
谢谢
Sid
I have a "Contact Us" page where in users will give in their email id and a query and on submitting the form, web admin would receive that email.
If I configure their email id to "from" MailAddress and send the mail, it will fail to do so if the ID is from popular mail domains like gmail or hotmail but would work with other unpopular or non existent domains like [email protected] without any credentials provided!
It worked with gmail after I configured SMTP and network credentials properly.
The aim is to let the admin of my website who receives the email be able to hit the reply button in his mail client and see the "to" field populated with the "from" field filled in "contact us" page.
Is there any proper way to do this or a tip or trick to accomplish it.
Heres my code
MailMessage emailMessage = new MailMessage();
MailAddress emailTo = new MailAddress("[email protected]", "Web Dev");
MailAddress emailFrom = new MailAddress(tbEmail.Text);
SmtpClient localhost = new SmtpClient("localhost");
emailMessage.To.Add(emailTo);
emailMessage.From = emailFrom;
emailMessage.Subject = "Enquiry / Feedback";
emailMessage.Body = "Name: " + tbName.Text +
"\nAddress: " + tbEmail.Text +
"\nComments: " + tbComments.Text;//emails body
localhost.Send(emailMessage);
Thanks
Sid
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不知道为什么你会遇到问题——我们有一些系统可以做到这一点,没有任何问题。但邮件是一个挑剔又狡猾的野兽。我敢打赌服务器上的配置设置会把事情搞砸——你在那里有多少控制权?
无论如何,更正确的方法是使用 EmailMessage.ReplyTo (2.0/3.5) 或 EmailMessage.ReplyToList (4.0) 属性来发送消息。这可能会绕过服务器上导致此问题的任何配置。
Not sure why you've got problems here -- we've got a few systems that do just this without any issues. But mail is a finnicky and hinky beast; I would bet on a configuration setting on the server messing things up -- how much control do you have there?
In any case, the more proper way to do this is use the EmailMessage.ReplyTo (2.0/3.5) or EmailMessage.ReplyToList (4.0) property to send the messages. This will probably bypass any configuration on the server that is causing this problem.
这是因为您正在使用本地主机发送电子邮件 - 您需要一个电子邮件服务器。如果您确实拥有 GMail(或其他)帐户 - 则使用具有正确凭据的服务器。
This is because you are using your localhost to send the email - you need an email server. If you actually have a GMail (or whatever) account - then use their server with the correct credentials.
添加回复标头
这将使电子邮件客户端填充此地址而不是发件人地址。这就是您要找的吗?上面的代码未经测试。
Add a reply to header
This will make the email client to populate this address instead of from address. Is this what you are looking for. The above code is untested.