如何从我的C#代码隐藏发送一封电子邮件 - 获取System.Net.Mail.SmtpFailed

2019-08-17 04:07发布

我有一个WebForm如果有人可以建立一个帐户 - 我想发送一封电子邮件确认。

我正在使用的代码:

       // 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>

看起来像它想要的,我想要发送到Web地址的用户登录信息。 我怎样才能修改这一点,并发送这样的确认电子邮件像所有其他商业公司怎么办?

Answer 1:

OK - 得到它的工作。 下面是工作编码; 像我配置的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>


Answer 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

此外,检查这篇文章,了解更多信息通过C#通过谷歌Apps帐户发送电子邮件



Answer 3:

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

您可以尝试创建的域中的服务帐户和设置应用程序池达到该服务帐户下运行。



文章来源: How to send an email from my C# codebehind - Getting System.Net.Mail.SmtpFailedRecipientException