Ivan
Ivan

Reputation: 261

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

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

The code I'm using:

       // Create e-mail message
        MailMessage mm = new MailMessage();
        mm.From = new MailAddress("[email protected]", "Our Organisation");
        mm.To.Add(new MailAddress("[email protected]", "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(“[email protected]”, “ourorganisationemailPassword”);
        sc.Credentials = basicAuthenticationInfo;
        sc.UseDefaultCredentials = false;

        sc.Send(mm);

web.config:

<system.net>
  <mailSettings>
    <smtp>
      <network host="[email protected]" port="9999" userName="[email protected]" password="OurOrganisationEmailPassword"/>
    </smtp>
  </mailSettings>
</system.net>

But get:

System.Net.Mail.SmtpFailedRecipientException was caught
HResult=-2146233088
Message=Mailbox unavailable. The server response was: Authentication is required for relay
Source=System
FailedRecipient=<[email protected]>

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?

Upvotes: 2

Views: 8629

Answers (3)

Ivan
Ivan

Reputation: 261

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("[email protected]","Our Organisation");//"[email protected]";
        MailAddress toAddress = new MailAddress("[email protected]", "Web User"); //"[email protected]"; 

        //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="[email protected]" password="OurOrganisationEmailPassword"/>
    </smtp>
  </mailSettings>
</system.net>

Upvotes: 5

ClotzA
ClotzA

Reputation: 161

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

host="[email protected]"

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

Upvotes: 1

Seth Moore
Seth Moore

Reputation: 3545

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.

Upvotes: 0

Related Questions