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

Posted by kedar karthik on Server Fault See other posts from Server Fault or by kedar karthik
Published on 2010-05-01T03:33:35Z Indexed on 2010/05/01 3:38 UTC
Read the original article Hit count: 347

Filed under:
|
|

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 = "[email protected]"; 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);

© Server Fault or respective owner

Related posts about c#

Related posts about exchange