C # send and receive emails through interaction with Outlook

Source: Internet
Author: User
Tags microsoft outlook

. Net has integrated functions into the POP3 mail system. However, if it is an Exchange server-based mail system, it is relatively complicated. If it's just sending, you can simply call CDO for implementation (see my previous article http://www.bkjia.com/kf/201203/122705.html), but CDO won't do it if you want to receive or perform other more complex operations.

 

In fact, Exchange Server 2003 does not support. net direct interaction. It is said that Exchange Server 2007 has opened a set of Web Service interfaces. If Exchange Server 2007 is used, you can directly interact with Exchange server through the Web Service interface, however, our company still uses exchange server 2003, so we have not tested how to use this set of interfaces.

 

In an environment where exchange server 2003 is used, the lowest price should be said to call the outlook function. The following lists some common operations for interacting with outlook.

 

First, Add a Reference to the outlook component in the Project (Project-> Add Reference-> switch to the COM tab-> select Microsoft Outlook 14.0 Object Library ), here, the specific version of outlook depends on the locally installed outlook version. I installed outlook 2010, so the displayed version number is 14.0, which has little to do with it, the Code of each version seems to be identical.

 

Run the following code to list unread emails in the Inbox:

Var app = new Microsoft. Office. Interop. Outlook. Application ();
Var ns = app. GetNamespace ("MAPI ");
Ns. Logon ("Outlook", Type. Missing, false, false );

Var inbox = ns. GetDefaultFolder (Microsoft. Office. Interop. Outlook. OlDefaultFolders. olFolderInbox );
For (int I = 1; I <= inbox. Items. Count; I ++)
{
If (inbox. Items [I]. UnRead)
{
TxtMailList. Text + = inbox. Items [I]. Subject + System. Environment. NewLine;
}
}
Ns. Logoff ();
Marshal. ReleaseComObject (inbox );
Marshal. ReleaseComObject (ns );
Marshal. ReleaseComObject (app );
Inbox = null;
Ns = null;
App = null;
 

The "Outlook" text appears in the third line of the above Code. This is the default profile name automatically created by Outlook. If you have modified this profile, or have multiple local profiles, or are not sure about the profile name, click control panel --> User Accounts --> email, for example:




 

Click "show configuration file ":




 

The name of the configuration file is displayed.

 

When you use a loop to enumerate items in the inbox, pay attention to the number starting from 1.

To read emails from local data files:

Var localFolder = ns. Stores ["Local"]. GetRootFolder (). Folders ["Archieve"];
If you want to delete emails in a file, note that every time you delete an index number, it will be re-numbered. Therefore, you cannot increment the number, but must decrease the number from large to small.

To call the alias resolution function of exchange server:

String alias = "Marvin Yan ";
Var recipient = app. Session. CreateRecipient (alias );
If (! Recipient. Resolve ())
{
// Alias can't be recoganized.
}

Obtain the smtp address (in the username @ server Format) based on the recipient ):

String mailAddr = recipient. AddressEntry. GetExchangeUser (). PrimarySmtpAddress;
 

If you want to reply to an existing Email:

Var mail = item. Reply ();
Mail. To = item. SenderEmailAddress;
Mail. Subject = "Hello ";
Mail. HTMLBody = "F. Y. I. <br/>" + mail. HTMLBody;
Mail. Send ();

The code for creating a new email and sending it is as follows:

Var mail = app. CreateItem (Microsoft. Office. Interop. Outlook. OlItemType. olMailItem );
Mail. HTMLBody = "Hello! ";
// Add an attachment.
String attachName = "hello ";
Int attachPos = (int) mail. Body. Length + 1;
Int attachType = (int) Microsoft. Office. Interop. Outlook. OlAttachmentType. olByValue;
// Now attached the file
Mail. Attachments. Add (@ "C: \ hello.txt", attachType, attachPos, attachName );
// Subject line
Mail. Subject = "test ";
// Add a recipient.
Var oRecip = mail. Recipients. Add ("xx@xxx.com ");
ORecip. Resolve ();
// Send.
Mail. Send ();

Author: Xia Lang zai
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.