原文:
使用c#给outlook添加任务、发送邮件
c#在使用outlook提供的一些API时,需要将outlook相关的com引用到项目中。 具体方法就是用vs打开工程后,在工程上添加引用,在com选项卡上,选择Microsoft Outlook 12.0 Object Library,如果安装的不是outlook2007,则对应com的版本不一样。注意下面描述的方法是在命令行模式或者winform模式下的,不是web模式下的。 在web模式下使用的方法稍有不同,不在此处讨论。
-
-
-
-
-
-
-
- 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;
- }
- }
下面是一个最简单的发送邮件的例子,在该例子中,只能给一个邮箱地址发邮件,而且还不能够添加附件。代码如下:
-
-
-
-
-
-
-
-
-
- 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;
- }
- }
代码如下:
-
-
-
-
-
-
-
-
-
-
- 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;
- }
- }
- 异步发送邮件的一个例子。以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)
- {
-
- 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);
-
-
- 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();
- }
- }
- }
使用c#给outlook添加任务、发送邮件
原文:http://www.cnblogs.com/lonelyxmas/p/4003808.html