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;
}
}
最簡單的發送郵件
下面是一個最簡單的發送郵件的例子,在該例子中,只能給一個郵箱地址發郵件,而且還不能夠添加附件。代碼如下:
/// <summary>
/// 一個最簡單的發送郵件的例子。同步方式。只支援發送到一個地址,而且沒有附件。
/// </summary>
/// <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)
{
try
{
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)
{
throw e;
}
}
向多人發郵件,並支援發送多個附件
代碼如下:
/// <summary>
/// 支援向多人發郵件,並支援多個附件的一個發送郵件的例子。
/// </summary>
/// <param name="server">smtp伺服器位址</param>
/// <param name="from">寄件者郵箱</param>
/// <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;
try
{
#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()));
}
}
}
#endregion
#region 發送郵件
eClient.Send(eMail);
#endregion
}
catch (System.Exception e)
{
throw e;
}
}//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("softwarezxj@163.com");
MailAddress to = new MailAddress("lastBeachhead@gmail.com");
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();
}
}
}