在ASP.NET中使用免费SMTP服务器发送电子邮件的问题 [英] Problems in sending email using free SMTP server in ASP.NET

查看:178
本文介绍了在ASP.NET中使用免费SMTP服务器发送电子邮件的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是错误所说的,第26行是client.Send(message);在System.Net.Mail.MailCommand.Send的System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode,String response)中,


(SmtpConnection conn,Byte []在System.Net.Mail.SmtpClient.Send(MailMessage消息)处的System.Net.Mail.SmtpTransport.SendMail(MailAddress sender,MailAddressCollection recipients,String deliveryNotify,Boolean allowUnicode,SmtpFailedRecipientException& exception)处的命令,MailAddress from,Boolean allowUnicode)联系我们.Form.send_Click(Object sender,EventArgs e)在d:\10Ap \prototype \ContactUsForm.aspx.cs:第26行



我是什么尝试过:



This is what the error says and line 26 is where it says client.Send(message);

at System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response) at System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] command, 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 ContactUsForm.send_Click(Object sender, EventArgs e) in d:\10Ap\prototype\ContactUsForm.aspx.cs:line 26

What I have tried:

This is the code i'm using

public partial class ContactUsForm : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    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", 587);
            client.EnableSsl = true;

            client.Credentials = new System.Net.NetworkCredential("smajwegmail.com", "Password here");

            client.Send(message);

            status.Text = "Mail was send successfully...!";

            from.Text = "";
            to.Text = "";
            subject.Text = "";
            body.Text = "";
        }

        catch (Exception ex)
        {
            status.Text = ex.StackTrace;
        }
    }
}


推荐答案

SmtpFailedRecipientException 几乎说明了这个电子邮件无法传递给收件人的当前状态。



如果您为此例外添加了特定的捕获并查看消息属性或任何,那么您最有可能获得更好的结果的InnerException 。这是一个简化版本;它们与堆叠捕获键()的关键是泛型异常EX 需要是最后一个,因为它将捕获到达该行的任何异常
The SmtpFailedRecipientException pretty much states that in it's current state that this email cannot be deliver to the recipient.

You would be most likely get better results if you added a specific catch for this exception and review the Message property or any InnerException. Here is a simplified version; they key with "stacked" catches() is that the generic Exception EX needs to be last as it will catch any exception that hits that line
try { /* your code */ }
catch (SmtpFailedRecipientException SX) { status.Text = SX.Message; }
catch (Exception EX) { status.Text = ex.StackTrace; }

我发现您的代码与大多数联系表单略有不同;通常来往/来自硬编码以防止形式出于恶意目的。如果使用的网络凭据没有代表正在使用的发件人地址发送的权限,则实际上可能会导致问题。 Google在执行规则方面非常紧张。



使用gmail时的另一个常见问题是所使用的帐户必须具有允许安全性较低的应用程序... 设置为开启位置。

I do find your code a little different than most contact forms; usually the to/from are hardcoded to prevent the form from being for nefarious purposes. This could actually be causing the problem if the network credentials being used do not have permission to send on behalf of the from address being used. Google is pretty tight on enforcing the rules.

Another common issue when using gmail is that the account being used must have the Allow less secure apps... setting be in the on position.


这篇关于在ASP.NET中使用免费SMTP服务器发送电子邮件的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆