Alpay
Alpay

Reputation: 1368

C# send email using Outlook dlls

In my application, I need to send emails. I can' t use smtp and there is no option installing MS Outlook in normal ways. I tried;

private Microsoft.Office.Interop.Outlook.Application oApp;
private Microsoft.Office.Interop.Outlook._NameSpace oNameSpace;
private Microsoft.Office.Interop.Outlook.MAPIFolder oOutboxFolder;

oApp = new Outlook.Application();
oNameSpace = oApp.GetNamespace("MAPI");
oNameSpace.Logon(null, null, true, true);

Outlook._MailItem oMailItem = (Outlook._MailItem)oApp.
    CreateItem(Outlook.OlItemType.olMailItem);

    oMailItem.To = toValue;
    oMailItem.Subject = subjectValue;
    oMailItem.Body = bodyValue;
    oMailItem.Send();

This code works well if Office 2010 is installed and running on machine. But I need to find out what dlls are being referenced. Is it possible to get only required dlls from Outlook and send email using them?

Thanks in advance

Upvotes: 0

Views: 2843

Answers (1)

Chris
Chris

Reputation: 3162

As per comment, examples of how to use the Exchange Web Services to send emails through an exchange server. Most of the information is available from the following link copied into the answer for preservation.

Example of creating an email message and sending it ( with a copy in the sent items folder of the user )

// Create an email message and identify the Exchange service.
EmailMessage message = new EmailMessage(service);

// Add properties to the email message.
message.Subject = "Interesting";
message.Body = "The merger is finalized.";
message.ToRecipients.Add("[email protected]");

// Send the email message and save a copy.
message.SendAndSaveCopy();

More code on the creation here

Slightly more complicated is the instantiation of the service variable used in the above code. Which is available here

ExchangeService service = new ExchangeService();
service.Credentials = new WebCredentials("[email protected]", "password");
service.AutodiscoverUrl("[email protected]");

Which will attempt to autodiscover the url of the exchange services from the email address. It is worth noting however that calls to the service will fail unless you attach a callback method to validate a self signed certificate which is used by default by Exchange. More info here

There is a wealth of information on how to connect to exchange services, send emails, create meetings, and calender requests. I have yet to personally test all of the above but probably gives you a decent start.

Upvotes: 1

Related Questions