Take the 2-minute tour ×

Is it possible to write C# code as below and send email using my home network?

I have a valid user name and password on that exchange server. Is there any configuration that I can set to achieve this?

BTW this code blow works when I run it within office network. I want this code to work when run from any network.

String cMSExchangeWebServiceURL = (String)System.Configuration.ConfigurationSettings.AppSettings["MSExchangeWebServiceURL"];
String cEmail = (String)System.Configuration.ConfigurationSettings.AppSettings["Cemail"];
String cPassword = (String)System.Configuration.ConfigurationSettings.AppSettings["Cpassword"];
String cTo = (String)System.Configuration.ConfigurationSettings.AppSettings["CTo"];

ExchangeServiceBinding esb = new ExchangeServiceBinding();
esb.Timeout = 1800000;
esb.AllowAutoRedirect = true;
esb.UseDefaultCredentials = false;

esb.Credentials = new NetworkCredential(cEmail, cPassword);
esb.Url = cMSExchangeWebServiceURL;
ServicePointManager.ServerCertificateValidationCallback += delegate(object sender1, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) 


    return true; };

// Create a CreateItem request object
CreateItemType request = new CreateItemType();

// Setup the request:
// Indicate that we only want to send the message. No copy will be saved.
request.MessageDisposition = MessageDispositionType.SendOnly;
request.MessageDispositionSpecified = true;

// Create a message object and set its properties
MessageType message = new MessageType();
message.Subject = subject;
message.Body = new TestOutgoingEmailServer.com.cogniti.mail1.BodyType();
message.Body.BodyType1 = BodyTypeType.HTML;

message.Body.Value = body;

message.ToRecipients = new EmailAddressType[3];

message.ToRecipients[0] = new EmailAddressType();
//message.ToRecipients[1] = new EmailAddressType();
//message.ToRecipients[2] = new EmailAddressType();

message.ToRecipients[0].EmailAddress = "kedarkarthik@gmail.com";
message.ToRecipients[0].RoutingType = "SMTP";

//message.CcRecipients = new EmailAddressType[1];
//message.CcRecipients[0] = new EmailAddressType();
//message.CcRecipients[0].EmailAddress = toEmailAddress.ElementAt(1).ToString();
//message.CcRecipients[0].RoutingType = "SMTP";

//There are some more properties in MessageType object 
//you can set all according to your requirement
// Construct the array of items to send
request.Items = new NonEmptyArrayOfAllItemsType();
request.Items.Items = new ItemType[1];
request.Items.Items[0] = message;

// Call the CreateItem EWS method.
CreateItemResponseType response = esb.CreateItem(request);
share|improve this question

migrated from stackoverflow.com May 2 '10 at 4:02

This question came from our site for professional and enthusiast programmers.

This is most certainly not a coding issue, and instead, a setting, permissions, or configuration issue. As such, this should be posted on Serverfault.com instead of StackOverflow. –  David Stratton May 1 '10 at 1:19
I'm never quite so certain as @David - but I agree the actual question is whether the exchange service is visible outside the the corporate firewall –  Murph May 1 '10 at 8:56
add comment

1 Answer

I'd start by using the Microsoft Exchange Server Remote Connectivity Analyzer, particularly the Exchange Web Services tests to see if EWS is accessible remotely.

Depending on what that suggests, you should be able to solve the issue you're experiencing, probably in conjunction with your Exchange admin.

BTW, I'm not sure if you realise (or I might just be mistaken), but you seem to be using the low-level EWS classes, instead of the better abstracted, more OO classes in the Microsoft.Exchange.WebServices.Data namespace (I'd link, but SF would get angry at me). I'm not saying that's a problem though as I'm not very familiar with those classes.

share|improve this answer
add comment

Your Answer

