【问题标题】:How to send an email using smtp over local host? asp.net如何通过本地主机使用 smtp 发送电子邮件?网
【发布时间】:2014-06-02 05:49:14
【问题描述】:

我已经为 Web 应用程序实现了一个联系表单,但是当我尝试发送电子邮件时,我收到 JavaScript 运行时错误,http://www.codeproject.com/Tips/371417/Send-Mail-Contact-Form-using-ASP-NET-and-Csharp line 37 这是一个空行。

是否有人知道我的实现有什么问题,或者我是否必须更改一些设置才能在本地主机上测试它?

//calls the SendMail() and resets the textboxes
    protected void sendBtn_Click(object sender, EventArgs e)
    {
        try
        {
            //here on button click what will done 
            SendMail();
            confirmationLbl.Text = "Your email has been sent to customer support.";
            confirmationLbl.Visible = true;
            subjectTbx.Text = "";
            emailTbx.Text = "";
            nameTbx.Text = "";
            questionTbx.Text = "";
        }
        catch (Exception ex) {

            confirmationLbl.Text = "Your email has failed to send,please check your connection.";
            Console.WriteLine("IOException source: {0}", ex.Message );


        }

//line 37 is here, which is blank
    }

    //method to compose email from textboxes
    protected void SendMail()
    {
        // Gmail Address from where you send the mail
        var fromAddress = emailTbx.Text.ToString(); 
        // any address where the email will be sending
        var toAddress = "brianDoe@gmail.com";
        //Password of your gmail address
        const string fromPassword = "Password";
        // Passing the values and make a email formate to display
        string subject = subjectTbx.Text.ToString();
        string body = "From: " + nameTbx.Text + "\n";
        body += "Email: " + emailTbx.Text + "\n";
        body += "Subject: " + subjectTbx.Text + "\n";
        body += "Question: \n" + questionTbx.Text + "\n";
        // smtp settings
        var smtp = new System.Net.Mail.SmtpClient();
        {
            smtp.Host = "smtp.gmail.com";
            smtp.Port = 587;
            smtp.EnableSsl = true;
            smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
            smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
            smtp.Timeout = 20000;
        }
        // Passing values to smtp object
        smtp.Send(fromAddress, toAddress, subject, body);

    }

【问题讨论】:

  • 您的电子邮件地址在其中,即所有人都可以在此处看到。
  • 您的 C# 代码中出现 JavaScript 错误?非常奇怪(提示:尝试查看 JavaScript 中的第 37 行...也尝试发布错误;这对调试过程非常重要)。
  • 我不认为他的姓氏是 Doe
  • 你是对的,它不是......因为他改变了它 - 与继续讨论无关。

标签: c# asp.net email smtp localhost


【解决方案1】:

您应该检查已解析的 HTML 是否存在 JavaScript 错误,而不是 C# 代码。此外,请确保在 Gmail 中的设置下启用 POP/IMAP 访问。

【讨论】:

  • 我刚刚更改了我的电子邮件设置,但我不确定您检查解析后的 H​​TML 是什么意思,我在哪里可以找到此代码?是那个页面的 Html 文件吗?
  • aspx 和 C# 代码被解析为 HTML 和 JavaScript 以便在浏览器中显示。打开页面时,右键单击并选择查看页面源(在 Chrome 中)并检查第 37 行。
  • 另外我不确定这是否是导致问题的错误,因为单击按钮时会引发异常ex
  • 代码似乎没问题。您确实需要在 Gmail 中启用 POP/IMAP 才能使其正常工作。另外,当您单击按钮时,您会遇到什么异常? Chk this
  • 好的,所以我打印了被捕获的异常消息,它指出:The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required
猜你喜欢
  • 2014-01-10
  • 1970-01-01
  • 2011-06-16
  • 2012-05-25
  • 2018-08-06
  • 2017-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多