Use C # To add tasks to outlook and send emails

Source: Internet
Author: User
Tags mailmessage smtpclient microsoft outlook

C # When using some APIs provided by outlook, You need to reference Outlook-related com to the project. The specific method is to use vs to open the project and add reference to the project. On the com tab, select Microsoft Outlook 12.0 object library. If outlook2007 is not installed, the corresponding com version is different. Note that the method described below is in command line or winform mode, not in Web mode. The methods used in Web mode are slightly different and are not discussed here.

Add a task to outlook, Code As follows:
/// <Summary>
/// Add a new job to outlook
/// </Summary>
/// <Param name = "subject"> new task title </param>
/// <Param name = "body"> new task body </param>
/// <Param name = "duedate"> new task expiration time </param>
/// <Param name = "importance"> new task priority </param>
Public static void addnewtask (string subject, string body, datetime duedate, olimportance importance)
{
Try
{
Application outlookapp = new application ();
Taskitem newtask = (taskitem) outlookapp. createitem (olitemtype. oltaskitem );
Newtask. Body = body;
Newtask. Subject = subject;
Newtask. Importance = importance;
Newtask. duedate = duedate;
Newtask. Save ();
}
Catch (system. Exception E)
{
Throw E;
}
}
Simplest email sending
The following is the simplest example of sending an email. In this example, only one email address can be sent, and attachments cannot be added. The Code is as follows:

/// <Summary>
/// The simplest example of sending an email. Synchronization mode. Only one address can be sent without attachments.
/// </Summary>
/// <Param name = "server"> SMTP server address </param>
/// <Param name = "from"> sender's email address </param>
/// <Param name = "to"> recipient's email </param>
/// <Param name = "subject"> topic </param>
/// <Param name = "body"> body </param>
/// <Param name = "ishtml"> whether the body is displayed in HTML format </param>
Public static void simpleseedmail (string server, string from, string to, string subject, string body, bool ishtml)
{
Try
{
Mailmessage message = new mailmessage (from, to, subject, body );
Message. isbodyhtml = ishtml;
Smtpclient client = new smtpclient (server );
Client. Credentials = new networkcredential ("sender's email username (I .e. @ the previous stuff)", "sender's email password ");
Client. Send (Message );
}
Catch (system. Exception E)
{
Throw E;
}
}
Sends emails to multiple users and supports sending multiple attachments.
The Code is as follows:

/// <Summary>
/// Supports sending emails to multiple users and sending one email to multiple attachments.
/// </Summary>
/// <Param name = "server"> SMTP server address </param>
/// <Param name = "from"> sender's email address </param>
/// <Param name = "to"> receiver's email address. Multiple recipients are separated by commas (,). </param>
/// <Param name = "subject"> subject </param>
/// <Param name = "body"> email body </param>
/// <Param name = "mailattach"> attachment </param>
/// <Param name = "ishtml"> whether the email body needs to be displayed in HTML format </param>
Public static void multisendemail (string server, string from, string to, string subject, string body, arraylist mailattach, bool ishtml)
{
Mailmessage email = new mailmessage ();
Smtpclient eclient = new smtpclient (server );
Eclient. Credentials = new networkcredential ("sender email user name (I .e. @ the previous stuff)", "sender email password ");

Email. Subject = subject;
Email. subjectencoding = encoding. utf8;

Email. Body = body;
Email. bodyencoding = encoding. utf8;

Email. From = new mailaddress (from );

String [] arrmailaddr;

Try
{
# Add multiple recipients to Region
Email. to. Clear ();
If (! String. isnullorempty ())
{
Arrmailaddr = to. Split (';');
Foreach (string strto in arrmailaddr)
{
If (! String. isnullorempty (strto ))
{
Email. to. Add (strto );
}
}
}
# Endregion
# Add multiple attachments to Region
Email. attachments. Clear ();
If (mailattach! = NULL)
{
For (INT I = 0; I <mailattach. Count; I ++)
{
If (! String. isnullorempty (mailattach [I]. tostring ()))
{
Email. attachments. Add (new system. net. Mail. Attachment (mailattach [I]. tostring ()));
}
}
}
# Endregion
# Region send email
Eclient. Send (email );
# Endregion
}
Catch (system. Exception E)
{
Throw E;
}

} // End of Method
An example of asynchronous mail sending. Take an SMTP server of 163 as an example.
The Code is as follows:

Using system;
Using system. net;
Using system. net. mail;
Using system. net. Mime;
Using system. Threading;
Using system. componentmodel;
Namespace examples. smptexamples. async
{
Public class simpleasynchronousexample
{
Static bool mailsent = false;

Private Static void sendcompletedcallback (Object sender, asynccompletedeventargs E)
{
// Get the unique identifier for this asynchronous operation.
String token = (string) E. userstate;

If (E. cancelled)
{
Console. writeline ("[{0}] Send canceled.", token );
}
If (E. Error! = NULL)
{
Console. writeline ("[{0}] {1}", Token, E. Error. tostring ());
}
Else
{
Console. writeline ("message sent .");
}
Mailsent = true;
}

Public static void main (string [] ARGs)
{
Smtpclient client = new smtpclient ("smtp.163.com ");
Client. Credentials = client. Credentials = new networkcredential ("sender email username", "sender email password ");

Mailaddress from = new mailaddress ("softwarezxj@163.com ");
Mailaddress to = new mailaddress ("lastBeachhead@gmail.com ");
Mailmessage message = new mailmessage (from, );
Message. Body = "this is an asynchronous test email ";
Message. bodyencoding = system. Text. encoding. utf8;
Message. Subject = "test asynchronous mail ";
Message. subjectencoding = system. Text. encoding. utf8;

// Set the callback function
Client. sendcompleted + = new sendcompletedeventhandler (sendcompletedcallback );
// The second parameter of the sendasync method can be any object. Here, a string is used to identify this sending
// The input object can be accessed in the callback function triggered by the mail sending end.
String userstate = "test message1 ";
Client. sendasync (message, userstate );
Console. writeline ("sending message... press C to cancel mail. Press any other key to exit .");
String answer = console. Readline ();

If (answer. startswith ("C") & mailsent = false)
{
Client. sendasynccancel ();
}
// Cleanup
Message. Dispose ();
Console. writeline ("goodbye .");
Console. Readline ();
}
}
}

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.