最近公司有个项目要使用exchange发送邮件,我在使用代码调用OutLook测试下来一切是正常的,但是当使用windows服务去启动程序的时候就会报错,(服务在走到olNS.Logon(Missing.Value, Missing.Value, false, false); 后就报错,大致意思是‘MAPI正在登陆时,不能重复登陆。’),请教各位高手应该如何解决或者有没有其他可行的方法?谢谢!!
以下是我的代码:public static void OutLookSend(string mailBody, string subject, string toAddress, string attachmentPath)
{
try
{
Microsoft.Office.Interop.Outlook.Application olApp = new Microsoft.Office.Interop.Outlook.Application(); Microsoft.Office.Interop.Outlook.NameSpace olNS = olApp.GetNamespace("MAPI");
olNS.Logon(Missing.Value, Missing.Value, false, false);
SafeMailItem sItem = new Redemption.SafeMailItem(); Microsoft.Office.Interop.Outlook.MailItem olItem = olApp.CreateItem(0) as Microsoft.Office.Interop.Outlook.MailItem;
olItem.Subject = subject;
olItem.HTMLBody = mailBody;
if (!string.IsNullOrEmpty(attachmentPath))
{
olItem.Attachments.Add(attachmentPath);
}
sItem.Item = olItem;
sItem.Recipients.Add(toAddress);
sItem.Recipients.ResolveAll();
SetPropValue(sItem, subject, subject);
sItem.Send();
olApp = null;
sItem = null;
}
catch(System.Exception ex)
{
//ILog log = LogManager.GetLogger("filelog");
//log.Error(ex.Message);
}
}windows服务exchange邮件
以下是我的代码:public static void OutLookSend(string mailBody, string subject, string toAddress, string attachmentPath)
{
try
{
Microsoft.Office.Interop.Outlook.Application olApp = new Microsoft.Office.Interop.Outlook.Application(); Microsoft.Office.Interop.Outlook.NameSpace olNS = olApp.GetNamespace("MAPI");
olNS.Logon(Missing.Value, Missing.Value, false, false);
SafeMailItem sItem = new Redemption.SafeMailItem(); Microsoft.Office.Interop.Outlook.MailItem olItem = olApp.CreateItem(0) as Microsoft.Office.Interop.Outlook.MailItem;
olItem.Subject = subject;
olItem.HTMLBody = mailBody;
if (!string.IsNullOrEmpty(attachmentPath))
{
olItem.Attachments.Add(attachmentPath);
}
sItem.Item = olItem;
sItem.Recipients.Add(toAddress);
sItem.Recipients.ResolveAll();
SetPropValue(sItem, subject, subject);
sItem.Send();
olApp = null;
sItem = null;
}
catch(System.Exception ex)
{
//ILog log = LogManager.GetLogger("filelog");
//log.Error(ex.Message);
}
}windows服务exchange邮件
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货