如何从背后的C#代码发送电子邮件-获取System.Net.Mail.SmtpFailedRecipientException [英] How to send an email from my C# codebehind - Getting System.Net.Mail.SmtpFailedRecipientException

查看:108
本文介绍了如何从背后的C#代码发送电子邮件-获取System.Net.Mail.SmtpFailedRecipientException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网络表单,有人可以在其中建立一个帐户-我想向他们发送一封电子邮件确认信.

I have a webform where someone can set up an account - I want to send them an email confirmation.

我正在使用的代码:

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

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>

好像想要我要发送到的网址的用户登录详细信息.我该如何修改并发送确认电子邮件,就像其他所有商业公司一样?

Looks like it's wanting the user login details for the web address I'm wanting to send to. How can I amend this and send such confirmation emails like how all the other commercial companies do?

推荐答案

确定-正常运行了.下面是工作代码;看起来像我配置NetworkCredential对象是问题.谢谢大家的帮助,以帮助我达成解决方案.

OK - got it working. Below is the working coding; looks like me configuring the NetworkCredential object was the issue. Thanks everyone though for your assistance in helping me reach the solution.

        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

web.config

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

这篇关于如何从背后的C#代码发送电子邮件-获取System.Net.Mail.SmtpFailedRecipientException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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