【问题标题】:How to send an email from my C# codebehind - Getting System.Net.Mail.SmtpFailedRecipientException如何从我的 C# 代码隐藏发送电子邮件 - 获取 System.Net.Mail.SmtpFailedRecipientException
【发布时间】:2013-02-10 16:38:44
【问题描述】:

我有一个网络表单,有人可以在其中设置帐户 - 我想向他们发送电子邮件确认。

我正在使用的代码:

       // Create e-mail message
        MailMessage mm = new MailMessage();
        mm.From = new MailAddress("OurOrganisationEmail@ourdomain.com", "Our Organisation");
        mm.To.Add(new MailAddress("webuser@domain.com", "Web User"));

        mm.Subject = "Confirmation";
        mm.Body = "You are now registered test";
        mm.IsBodyHtml = true;

        // Send e-mail
        sc = new SmtpClient();

        NetworkCredential basicAuthenticationInfo = new NetworkCredential(“OurOrganisationEmail@ourdomain.com”, “ourorganisationemailPassword”);
        sc.Credentials = basicAuthenticationInfo;
        sc.UseDefaultCredentials = false;

        sc.Send(mm);

web.config:

<system.net>
  <mailSettings>
    <smtp>
      <network host="mail.OurOrganisationEmail@ourdomain.com" port="9999" userName="OurOrganisationEmail@ourdomain.com" password="OurOrganisationEmailPassword"/>
    </smtp>
  </mailSettings>
</system.net>

但是得到:

System.Net.Mail.SmtpFailedRecipientException was caught
HResult=-2146233088
Message=Mailbox unavailable. The server response was: Authentication is required for relay
Source=System
FailedRecipient=<webuser@domain.com>

看起来它需要我要发送到的网址的用户登录详细信息。我该如何修改它并像所有其他商业公司一样发送此类确认电子邮件?

【问题讨论】:

  • 在 NetworkCredential 中,您是否尝试使用真实的用户名,而不是用户的电子邮件?即“我们的组织电子邮件”。 (你也应该使用代码或配置,但不能同时使用)

标签: c# web-config asp.net-4.0 smtpclient


【解决方案1】:

好的 - 让它工作。以下是工作编码;看起来我配置 NetworkCredential 对象是问题所在。感谢大家帮助我找到解决方案。

        MailAddress fromAddress =  new MailAddress("OurOrganisationEmail@ourdomain.com","Our Organisation");//"name@yourdomain.com";
        MailAddress toAddress = new MailAddress("webuser@domain.com", "Web User"); //"name@anydomain.com"; 

        //Create the MailMessage instance 
        MailMessage myMailMessage = new MailMessage(fromAddress, toAddress);

        //Assign the MailMessage's properties 
        myMailMessage.Subject = "Confirmation";
        myMailMessage.Body = "You are now registered test";
        myMailMessage.IsBodyHtml = true;

        //Create the SmtpClient object
        SmtpClient smtp = new SmtpClient();

        //Send the MailMessage (will use the Web.config settings) 
        smtp.Send(myMailMessage);

web.config

<system.net>
  <mailSettings>
    <smtp>
  <network host="mail.ourdomain.com" port="9999" userName="OurOrganisationEmail@ourdomain.com" password="OurOrganisationEmailPassword"/>
    </smtp>
  </mailSettings>
</system.net>

【讨论】:

    【解决方案2】:

    您的客户端代码看起来不错,问题可能出在web.config

    host="mail.OurOrganisationEmail@ourdomain.com"
    

    这是一个email address,不是一个有效的hostname 它应该是这样的:

    host="mail.ourdomain.com"
    

    您是否测试过smtp server 是否有效? 只需使用telnet 进行一些基本测试。 http://technet.microsoft.com/en-us/library/aa995718(v=exchg.65).aspx

    另外,请查看此帖子以获取更多信息 Send Email via C# through Google Apps account

    【讨论】:

      【解决方案3】:

      我猜这是因为您的网站没有在域帐户下运行。很多邮件服务器不允许匿名用户发送邮件。

      您可以尝试在域中创建一个服务帐户并将应用程序池设置为在该服务帐户下运行。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-06-22
        • 1970-01-01
        • 2016-01-21
        • 2011-09-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多