Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
C#
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

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.

C#
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.
 
Share this answer
 
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,
 
Share this answer
 
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.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900