【问题标题】:I want to send mail through ASP.NET but error occurred我想通过 ASP.NET 发送邮件但发生错误
【发布时间】:2019-05-27 17:24:57
【问题描述】:

我在此联系表单的后端使用 C#,而 html 是前端

   using System;
   using System.Collections.Generic;
   using System.Linq;
   using System.Web;
   using System.Web.UI;
   using System.Web.UI.WebControls;
   using System.Net.Mail;

   public partial class index : System.Web.UI.Page
   {
         protected void Send_Click(object sender, EventArgs e)
         {
              try
              {
                   MailMessage message = new MailMessage(From.Text, To.Text, Subject.Text, Body.Text);
                   message.IsBodyHtml = true;
                   SmtpClient client = new SmtpClient("smtp.gmail.com", 465);
                   client.EnableSsl = true;
                   // The credentials when I ran the code were correct.
                   client.Credentials = new System.Net.NetworkCredential("example@gmail.com","password");

                   client.Send(message);

                   status.Text = "Mail was sent successfully";
                   status.Text = "Send was clicked";
              }
              // This catch block is so that you can see what error 
              // occurs if there is an error
              catch(Exception ex)
              {
                 status.Text = ex.StackTrace;
              }
       }
 }

这是显示的错误

System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response)

在 System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] command, MailAddress from, Boolean allowUnicode)
在 System.Net.Mail.SmtpTransport.SendMail(MailAddress 发件人,MailAddressCollection 收件人,字符串 deliveryNotify,布尔 allowUnicode,SmtpFailedRecipientException& 异常)
在 System.Net.Mail.SmtpClient.Send(MailMessage message) 在 index.Send_Click(Object sender, EventArgs e) 在 C:\Users\robert.crider\source\repos\WebSite2\WebSite2\index.aspx.cs:line 37

【问题讨论】:

  • 这是 System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response) 在 System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] 命令中显示的错误, MailAddress from, Boolean allowUnicode) at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, Boolean allowUnicode, SmtpFailedRecipientException& exception) at System.Net.Mail.SmtpClient.Send(MailMessage message) at index.Send_Click (Object sender, EventArgs e) 在 C:\Users\robert.crider\source\repos\WebSite2\WebSite2\index.aspx.cs:line 37
  • 能否显示 ex.Message 或 ex.InnerMessage 以获得更多错误详情?
  • 我没有 ex.message 或 ex.innermessage?
  • 你的错误捕获 "catch(Exception ex)" 它应该有 ex.Message 或 ex.InnerException.Message 。就像你的显示堆栈跟踪 ex.StackTrace
  • 请使用问题下方的edit 链接添加任何附加细节,例如错误消息,而不是在 cmets 中。

标签: c# asp.net visual-studio smtp


【解决方案1】:

确保您为 google smtp 和交付网络使用正确的端口。并且您已经在 Gmail 帐户设置中启用 IMAP 和/或 POP3 访问。

SmtpClient client= new SmtpClient
        {
           Host = "smtp.gmail.com",
           Port = 587,
           EnableSsl = true,
           DeliveryMethod = SmtpDeliveryMethod.Network,
           Credentials    = new NetworkCredential(yourgoogleemail, yourgooglepassword),
           Timeout = 3000
        };

【讨论】:

  • 我刚刚启用了 Imap 和 POP3,但更改后仍然出现相同的错误
  • 刚刚使用您提供的代码。感激不尽!
  • @RobertCrider - 在这种情况下,您应该按照here 的说明接受答案。
猜你喜欢
  • 2018-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多