Click here to Skip to main content
15,868,292 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi all,

i am sending mail through exchange server version Exchange2010.

below is my code:

static void Main()
        {
            try
            {
                string owausername = string.Empty;
                string owapassword = string.Empty;
                string mailFrom;
                string mailTo;
                string mailSub;
                string mailBody;

                ServicePointManager.ServerCertificateValidationCallback = CertificateValidationCallBack;
                ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010);
                EmailMessage email = new EmailMessage(service);

                string reg_subKey = "Software\\VB and VBA Program Settings\\LangServ15.2";
                RegistryKey root = Registry.CurrentUser.CreateSubKey(reg_subKey);
                foreach (string keyname in root.GetSubKeyNames())
                {
                    using (RegistryKey key = root.OpenSubKey(keyname, true))
                    {
                        foreach (string valueName in key.GetValueNames())
                        {

                            if (valueName == "owausername")
                            {
                                if (key.GetValue(valueName).ToString() != "")
                                {
                                    owausername = key.GetValue(valueName).ToString();
                                }
                            }
                            if (valueName == "owapassword")
                            {
                                if (key.GetValue(valueName).ToString() != "")
                                {
                                    owapassword = key.GetValue(valueName).ToString();
                                }
                            }
                            service.Credentials = new WebCredentials(owausername, owapassword);
                            if (valueName == "owaurl")
                            {
                                if (key.GetValue(valueName).ToString() != "")
                                {
                                    service.Url = new Uri(key.GetValue(valueName).ToString());
                                }
                            }
                            if (valueName == "textFrom")
                            {
                                if (key.GetValue(valueName).ToString() != "")
                                {
                                    mailFrom = key.GetValue(valueName).ToString();
                                }
                            }
                            if (valueName == "textTo")
                            {
                                mailTo = key.GetValue(valueName).ToString();
                                email.ToRecipients.Add(mailTo);
                            }
                            if (valueName == "textSub")
                            {
                                mailSub = key.GetValue(valueName).ToString();
                                email.Subject = mailSub;
                            }
                            if (valueName == "textBody")
                            {
                                mailBody = key.GetValue(valueName).ToString();
                                email.Body = mailBody;

                            }
                        }
                        root.DeleteSubKey(keyname);
                    }
                }
                email.SendAndSaveCopy();
                System.Windows.Forms.MessageBox.Show("Email Sent Successfully...");
            }
            catch (Exception ex)
            {
                if (ex.Message.ToString() == "The request failed. The remote server returned an error: (401) Unauthorized.")
                {
                    System.Windows.Forms.MessageBox.Show("The OWA user name or OWA password you entered isn't correct. Enter corrent credentials and then Try again.");
                }
                //throw ex;
            }
        }
        private static bool CertificateValidationCallBack(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors)
        {
            return true;
        }


what i am doing i made .exe of this code and calling from my vb6.0 application.
it works fine on my local machine.but when i upload it on my live server and test it.it stucks at one line
email.SendAndSaveCopy();</pre>
. All other things are going fine.

help would be appreciated.

thanks,
kk
Posted
Comments
AnkitGoel.com 11-Dec-12 7:37am    
when it sucks you, what does it say as error message?
kk2014 11-Dec-12 7:48am    
no error comes..nothing happens....and also not sending mail...
it works fine on my local machine but problem is on my live server....

1 solution

actually in my above code i commented
throw ex;
. so i uncomment it and wrote below code to raise error.
System.Windows.Forms.MessageBox.Show(ex.Message.ToString());
throw ex;

and i found it throws error :

the request failed.unable to connect to remote server.
actually my exchange server is not opening on my live server.my live server firewall blocked my exchange server.

thanks,
kk
 
Share this answer
 
Comments
TorstenH. 12-Dec-12 2:31am    
why don't you simply print out the exception?

if (ex.Message.ToString() == "The request failed. The remote server returned an error: (401) Unauthorized.")
is imho bad style, that's no valid "exception handling" - or whatever.
kk2014 12-Dec-12 6:15am    
i had written for just to check actually where and what the actual error is?

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