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