Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP email
I have an enail class I use for sending email. It builds the body and then sends it using web.config details. I suspect I have done spomething wrong in the setup of the ASP.net project since the code works in other projects but this one I am getting an error. I have checked the email setting and password so that is not the problem.
 
I have also read the posts under:
[^]
and

[^]
 
First the error
The handle is invalid.
 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
 
Exception Details: System.Security.Cryptography.CryptographicException: The handle is invalid.
 

Source Error: 
 

Line 177:              + DateTime.Now.Date.ToShortDateString() + " re:" + myMsg.Subject;
Line 178:        myMsg.From = new MailAddress(_sysEmail, "Reply to " + myMsg.From.Address);
Line 179:        NTLMAuthentication = new System.Net.NetworkCredential(_sysEmail, _sysPass);
Line 180:        client = new SmtpClient(_sysSMTP, 587);   ///back up is normally a GMail address so use a secure port
Line 181:        client.EnableSsl = true;
 
Now the code:
      SmtpClient client;
      System.Net.NetworkCredential NTLMAuthentication;
 
      string _sysEmail = ConfigurationManager.AppSettings[CONST_BACKEND_EMAIL];
      string _sysPass = ConfigurationManager.AppSettings[CONST_BACKEND_PASS];
      string _sysSMTP = ConfigurationManager.AppSettings[CONST_BACKEND_SMTP];
 
      NTLMAuthentication = new System.Net.NetworkCredential(_sysEmail, _sysPass);
      client = new SmtpClient(_sysSMTP);
      client.UseDefaultCredentials = false;
      client.Credentials = NTLMAuthentication;
      myMsg.Body = sbMsgBody.ToString();  // this is a property in the calss
      client.Send(myMsg);
 

It is crashing on the NTML line
 
What have I done wrong, do I need to add a reference to my project?
Posted 23-Sep-12 21:33pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

hello friend i have checked your code and i cant get any error
i just specified from and to email address and it will send testing email to my account successfully.
 
SmtpClient client;
System.Net.NetworkCredential NTLMAuthentication;
 
string _sysEmail = "xyz@abc.com";
string _sysPass = "xyzabc";
string _sysSMTP = "yoursmtpserver";
 
NTLMAuthentication = new System.Net.NetworkCredential(_sysEmail, _sysPass);
client = new SmtpClient(_sysSMTP);
client.UseDefaultCredentials = false;
client.Credentials = NTLMAuthentication;
System.Net.Mail.MailMessage myMsg = new MailMessage();
myMsg.From = (MailAddress)(new MailAddress("xyz@abc.com"));
myMsg.To.Add("abc@xyz.com");
myMsg.Body = "hi testing mail";  // this is a property in the calss
client.Send(myMsg);
 
please check this with replacing your server information and email addresses.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Hard coded the values and still getting the error. It is almost like system is struggling to even call
Quote:
NTLMAuthentication = new System.Net.NetworkCredential

 
Even if I pass rubbish it gives me the same error,
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Not sure why this fixed the problem but it did.
 
I went to the property pages of the website, clicked on Start Options, ticked NTLM Authentication, clicked on okay and it worked locally. Check the web.config file, could see any changes. I change all the code back to read from the configuration part of the config file and then ftp'd the files to the hosting environment and all works fine.
 

I am not sure where it stores the changes that are done when you change the property page of the project, but something must have changed because t is working now.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 534
1 Gihan Liyanage 298
2 RyanDev 270
3 Sergey Alexandrovich Kryukov 223
4 Richard Deeming 185
0 Sergey Alexandrovich Kryukov 8,906
1 OriginalGriff 7,771
2 CPallini 2,603
3 Richard MacCutchan 2,121
4 Abhinav S 1,928


Advertise | Privacy | Mobile
Web02 | 2.8.140827.1 | Last Updated 24 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100