使用OutLook发送邮件
电脑版发表于:2018/12/20 14:25
public static void Outlook(string Subject, string TextBody, string FromAdd, string FromPass, string To, string CC, List<string> attach)
{
try
{
System.Diagnostics.Process[] MyProcess = System.Diagnostics.Process.GetProcessesByName("outlook");
if (MyProcess.Length == 0)
{
throw new System.Exception("请先启动OutLook!");
}
}
catch (System.Exception)
{
throw;
}
Microsoft.Office.Interop.Outlook.Application app = null;
NameSpace ns = null;
Microsoft.Office.Interop.Outlook._MailItem message = null;
try
{
app = new Microsoft.Office.Interop.Outlook.Application();
ns = app.GetNamespace("mapi");
ns.Logon(FromAdd, FromPass, false, true);
message = (Microsoft.Office.Interop.Outlook._MailItem)app.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
message.BodyFormat = Microsoft.Office.Interop.Outlook.OlBodyFormat.olFormatHTML;
message.To = To;
message.CC = CC;
message.Subject = Subject;
message.HTMLBody = TextBody;
if (attach != null)
{
foreach (string att in attach)
{
message.Attachments.Add(att);
}
}
message.Send();
ns.Logoff();
}
catch
{
throw;
}
finally
{
message = null;
ns = null;
app = null;
}
}
}
}