How to send an email from my C# codebehind - Getting System.Net.Mail.SmtpFailedRecipientException

∥☆過路亽.° 提交于 2019-12-02 01:25:18

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

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

Your client code looks fine, the problem may be in web.config:

host="mail.OurOrganisationEmail@ourdomain.com"

it's an email address, not a valid hostname it should look like this:

host="mail.ourdomain.com"

Have you tested if the smtp server works ? just make some basic tests using telnet. http://technet.microsoft.com/en-us/library/aa995718(v=exchg.65).aspx

also, check this post for additional info Send Email via C# through Google Apps account

I'm guessing this is because your website is not running under a domain account. A lot of mail servers don't allow anonymous users send email.

You could try creating a service account that's in the domain and set the application pool up to run under that service account.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!