c#在使用outlook提供的一些API時,需要将outlook相關的com引用到項目中。 具體方法就是用vs打開工程後,在工程上添加引用,在com頁籤上,選擇Microsoft Outlook 12.0 Object Library,如果安裝的不是outlook2007,則對應com的版本不一樣。注意下面描述的方法是在指令行模式或者winform模式下的,不是web模式下的。 在web模式下使用的方法稍有不同,不在此處讨論。
給outlook添加任務,代碼如下:
/// <summary>
/// 給outlook添加一個新的任務
/// </summary>
/// <param name="subject">新任務标題</param>
/// <param name="body">新任務正文</param>
/// <param name="dueDate">新任務到期時間</param>
/// <param name="importance">新任務優先級</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;
}
最簡單的發送郵件 下面是一個最簡單的發送郵件的例子,在該例子中,隻能給一個郵箱位址發郵件,而且還不能夠添加附件。代碼如下:
/// 一個最簡單的發送郵件的例子。同步方式。隻支援發送到一個位址,而且沒有附件。
/// <param name="server">smtp伺服器位址</param>
/// <param name="from">發送者郵箱</param>
/// <param name="to">接收者郵箱</param>
/// <param name="subject">主題</param>
/// <param name="body">正文</param>
/// <param name="isHtml">正文是否以html形式展現</param>
public static void SimpleSeedMail(string server, string from, string to, string subject, string body, bool isHtml)
MailMessage message = new MailMessage(from, to, subject, body);
message.IsBodyHtml = isHtml;
SmtpClient client = new SmtpClient(server);
client.Credentials = new NetworkCredential("發送者郵箱使用者名(即@前面的東東)","發送者郵箱密碼");
client.Send(message);
catch (System.Exception e)
向多人發郵件,并支援發送多個附件 代碼如下:
/// 支援向多人發郵件,并支援多個附件的一個發送郵件的例子。
/// <param name="to">接收者郵箱,多個接收者以;隔開</param>
/// <param name="subject">郵件主題</param>
/// <param name="body">郵件正文</param>
/// <param name="mailAttach">附件</param>
/// <param name="isHtml">郵件正文是否需要以html的方式展現</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("發送者郵箱使用者名(即@前面的東東)", "發送者郵箱密碼");
eMail.Subject = subject;
eMail.SubjectEncoding = Encoding.UTF8;
eMail.Body = body;
eMail.BodyEncoding = Encoding.UTF8;
eMail.From = new MailAddress(from);
string[] arrMailAddr;
#region 添加多個收件人
eMail.To.Clear();
if (!string.IsNullOrEmpty(to))
{
arrMailAddr = to.Split(';');
foreach (string strTo in arrMailAddr)
{
if (!string.IsNullOrEmpty(strTo))
{
eMail.To.Add(strTo);
}
}
}
#endregion
#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()));
#region 發送郵件
eClient.Send(eMail);
}//end of method
異步發送郵件的一個例子。以163的smtp伺服器為例。 代碼如下:
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("發送者郵箱使用者名", "發送者郵箱密碼");
MailAddress from = new MailAddress("[email protected]");
MailAddress to = new MailAddress("[email protected]");
MailMessage message = new MailMessage(from, to);
message.Body = "這是一封測試異步發送郵件的郵件 ";
message.BodyEncoding = System.Text.Encoding.UTF8;
message.Subject = "測試異步發郵件";
message.SubjectEncoding = System.Text.Encoding.UTF8;
// 設定回調函數
client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
// SendAsync方法的第二個參數可以是任何對象,這裡使用一個字元串辨別本次發送
//傳入的該對象可以在郵件發送結束觸發的回調函數中通路到。
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();
//清理工作
message.Dispose();
Console.WriteLine("Goodbye.");
Console.ReadLine();
}
}