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();

All other things are going fine.

help would be appreciated.

thanks,
kk

What's the error message you get?

no error message comes..nothing happens...just stucked....

What if you re-enable the throw ex; ? Are you sure the live server has access to the exchange server?

1) what do u mean by re-enable throw ex? Please suggest.
2) no i don't know how to check wether live server has access or not? how to check it...

Please suggest.i am also stucked here as my appp page stucked.
:(

You commented line 83 which re-throws the exception. Because you did, you will never see an error. Uncomment it, deploy to your server and see if it shows an exception.

hi pritaeas,
thanks for the suggestion.
i wrote below code and uncomment it.

System.Windows.Forms.MessageBox.Show(ex.Message.ToString());
throw ex;

and it throws error :

the request failed.unable to connect to remote server.

in case what i have to do now..

thanks,
kk

Contact your system administrator to allow your web server access to exchange.

hi pritaeas,
i have already asked him...
let see what happen..
thanks to point out to uncomment line..
thanks,
kk

hi pritaeas,
i found solution myself...
actually error was

    the request failed.unable to connect to remote server.

i write my exchange server url as https://myrul/owa/ it works earlier but i don't know sudden i found my url https://myrul/ews/exchange.asmx that's why the request failed and not unable to connect my remote server and error came.

but my question is i was testing with https://myrul/owa/ earlier and it worked why not now.

thanks,
kk

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.