如何在 ASP.Net 中从 google、yahoo 或其他邮件域发送邮件?

发布于 2024-10-20 08:39:32 字数 1172 浏览 1 评论 0原文

我有一个“联系我们”页面,用户将在其中提供他们的电子邮件 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 技术交流群。

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

发布评论

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

评论(4

瞎闹 2024-10-27 08:39:33

不知道为什么你会遇到问题——我们有一些系统可以做到这一点,没有任何问题。但邮件是一个挑剔又狡猾的野兽。我敢打赌服务器上的配置设置会把事情搞砸——你在那里有多少控制权?

无论如何,更正确的方法是使用 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.

蔚蓝源自深海 2024-10-27 08:39:33

这是因为您正在使用本地主机发送电子邮件 - 您需要一个电子邮件服务器。如果您确实拥有 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.

假情假意假温柔 2024-10-27 08:39:33
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Net.Mail;


public class YourClass
{
    private void SendMailFromGmail(string vFrom, string vTo, string vGmailID, string vGmailPass, string vMailText, string vSMPTDNS, string vSubject)
    {
        MailMessage MyMailMessage = new MailMessage();
        SmtpClient SMTPServer = new SmtpClient(vSMPTDNS);
        var _with1 = SMTPServer;
        //Start by creating a mail message object

        //From requires an instance of the MailAddress type

        MyMailMessage.From = new MailAddress(vFrom);

        //To is a collection of MailAddress types
        MyMailMessage.To.Add(vTo);    
        MyMailMessage.Subject = vSubject;
        MyMailMessage.Body = vMailText;
        //Create the SMTPClient object and specify the SMTP GMail server
        _with1.Port = 587;

        _with1.Credentials = new System.Net.NetworkCredential(vGmailID, vGmailPass);
        _with1.EnableSsl = true;

        try {
            _with1.Send(MyMailMessage);
            string lNewVariable5 = "Email Sent";
        //MessageBox.Show(lNewVariable5)
        } catch (SmtpException ex) {
            throw ex;
        }
    }

    public void Main()
    {
        string vFrom = "[email protected]";
        string vTo = "to_address_here@domain_name_here";
        string vGmailID = "account uid";
        string vGmailPass = " account pwd";
        string vMailText = "This is the test text for Gmail email";
        string vSMPTDNS = "smtp.gmail.com";
        string vSubject = "GMail Test";

        SendMailFromGmail(vFrom, vTo, vGmailID, vGmailPass, vMailText, vSMPTDNS, vSubject);
    }



}
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Net.Mail;


public class YourClass
{
    private void SendMailFromGmail(string vFrom, string vTo, string vGmailID, string vGmailPass, string vMailText, string vSMPTDNS, string vSubject)
    {
        MailMessage MyMailMessage = new MailMessage();
        SmtpClient SMTPServer = new SmtpClient(vSMPTDNS);
        var _with1 = SMTPServer;
        //Start by creating a mail message object

        //From requires an instance of the MailAddress type

        MyMailMessage.From = new MailAddress(vFrom);

        //To is a collection of MailAddress types
        MyMailMessage.To.Add(vTo);    
        MyMailMessage.Subject = vSubject;
        MyMailMessage.Body = vMailText;
        //Create the SMTPClient object and specify the SMTP GMail server
        _with1.Port = 587;

        _with1.Credentials = new System.Net.NetworkCredential(vGmailID, vGmailPass);
        _with1.EnableSsl = true;

        try {
            _with1.Send(MyMailMessage);
            string lNewVariable5 = "Email Sent";
        //MessageBox.Show(lNewVariable5)
        } catch (SmtpException ex) {
            throw ex;
        }
    }

    public void Main()
    {
        string vFrom = "[email protected]";
        string vTo = "to_address_here@domain_name_here";
        string vGmailID = "account uid";
        string vGmailPass = " account pwd";
        string vMailText = "This is the test text for Gmail email";
        string vSMPTDNS = "smtp.gmail.com";
        string vSubject = "GMail Test";

        SendMailFromGmail(vFrom, vTo, vGmailID, vGmailPass, vMailText, vSMPTDNS, vSubject);
    }



}
差↓一点笑了 2024-10-27 08:39:33

添加回复标头

mail.Headers.Add( "Reply-To", "[email protected]" );

这将使电子邮件客户端填充此地址而不是发件人地址。这就是您要找的吗?上面的代码未经测试。

Add a reply to header

mail.Headers.Add( "Reply-To", "[email protected]" );

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.

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