未打开Outlook时Microsoft.Office.Interop.Outlook._MailItem.get_Recipients()提供错误 [英] Microsoft.Office.Interop.Outlook._MailItem.get_Recipients() givng error when outlook is not opened

查看:92
本文介绍了未打开Outlook时Microsoft.Office.Interop.Outlook._MailItem.get_Recipients()提供错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的平台:
.net framewrok 1.1
Windows XP专业版
MS Office 2007 .
Visual Studio2003.
基于窗口的应用程序.


我做了以下提到的步骤:
1.右键点击C#项目中,选择添加引荐"
2.转到"COM"标签
3.选择"MS Office 12.0对象库".和"Microsoft Outlook 12.0对象库"并点击确定".
4.我已经添加了以下代码.

使用 Outlook = Microsoft.Office.Interop.Outlook;

请参见下面的代码

public static void SendMail(string[] MailTo, string MailSubject, string MailBody, bool OpenEmailEditor,params string[] attachmentSource)

		{				

			bool bResolve ;			

			string EmailDomain;	

			int position;

			int attachType;

			string displayName;

			FileInfo fi;

			EmailDomain = "@" + MyConfig.Get("EMail","Domain");

					Outlook.Recipient oRecipient;		

			Outlook.ApplicationClass myOutlook = new Outlook.ApplicationClass();

			Outlook.MailItem newMail = (Outlook.MailItem)myOutlook.CreateItem(Outlook.OlItemType.olMailItem);		

			//Extract each recipient from Mailto and resolve them if they are valid id's

			foreach (string s in MailTo) 

			{

				if (s.Trim() != "")

				{

					if(s.IndexOf("@")== -1)

					{

						string str="";

						str = s + EmailDomain;

						oRecipient = newMail.Recipients.Add(str);

					}

					else

					{

						oRecipient = newMail.Recipients.Add(s); /* getting error */ 				

					}

					

					bResolve = oRecipient.Resolve();

					if (!bResolve || oRecipient.Name.EndsWith(EmailDomain))

					{						

						if (OpenEmailEditor == false)

						{							

							throw new MySystemException(ConstFrameworkError.ERRCD_INVALID_EMAIL_ID,new object[] {"",""});						

						}

						else

						{						

							newMail.Recipients.Remove(newMail.Recipients.Count);

							newMail.Recipients.Add(s);

						}	

					}					

				}				

			}		

			if(MailSubject != null)

			{

				newMail.Subject = MailSubject;

			}

			else

			{

				newMail.Subject = "";

			}

			if(MailBody != null)

			{

				newMail.Body = MailBody;

			}

			else

			{

				newMail.Body = "";

			}

            position = (int)MailBody.Length + 1;//Position of the attachment in the outlook editor.			

			attachType = (int)Outlook.OlAttachmentType.olByValue;	

			//Add all the attachments to new mail.

			foreach(string path in attachmentSource)

			{

				//Add the attachment if file is present in the specified path.

				if (File.Exists(path))				

				{

					fi = new FileInfo(path);

                    displayName = fi.Name ;							

					newMail.Attachments.Add(path,attachType,position,displayName);

				}

			}

			if (OpenEmailEditor == true)				

			{				

				newMail.Display(true);

			}

			else

			{	

					

					newMail.Send();								

								}	

			oRecipient = null;

			newMail = null;

			myOutlook = null;			



		}

解决方案

Hi
上面的任何更新.在不依赖于外观实例是否运行"的情况下,有任何其他选择可以使它起作用.
如上所述,我的平台是:
  ; .net framewrok 1.1
  Windows Xp专业版
MS Office2007.
Visual Studio 2003.


Here is my platform:
 .net framewrok 1.1
 Windows Xp Propfessional
 MS Office 2007.
Visual Studio 2003.
Window based application.


I did below mentioned steps:
1. Right click on  C# projeect, select "Add Referece"
2. Go to "COM" tab
3. Select "MS Office 12.0 Object Library" and "Microsoft Outlook 12.0 Object Library" and click on "OK".
4. i have added below code.

using Outlook=Microsoft.Office.Interop.Outlook;

pls see code below

public static void SendMail(string[] MailTo, string MailSubject, string MailBody, bool OpenEmailEditor,params string[] attachmentSource)

		{				

			bool bResolve ;			

			string EmailDomain;	

			int position;

			int attachType;

			string displayName;

			FileInfo fi;

			EmailDomain = "@" + MyConfig.Get("EMail","Domain");

					Outlook.Recipient oRecipient;		

			Outlook.ApplicationClass myOutlook = new Outlook.ApplicationClass();

			Outlook.MailItem newMail = (Outlook.MailItem)myOutlook.CreateItem(Outlook.OlItemType.olMailItem);		

			//Extract each recipient from Mailto and resolve them if they are valid id's

			foreach (string s in MailTo) 

			{

				if (s.Trim() != "")

				{

					if(s.IndexOf("@")== -1)

					{

						string str="";

						str = s + EmailDomain;

						oRecipient = newMail.Recipients.Add(str);

					}

					else

					{

						oRecipient = newMail.Recipients.Add(s); /* getting error */ 				

					}

					

					bResolve = oRecipient.Resolve();

					if (!bResolve || oRecipient.Name.EndsWith(EmailDomain))

					{						

						if (OpenEmailEditor == false)

						{							

							throw new MySystemException(ConstFrameworkError.ERRCD_INVALID_EMAIL_ID,new object[] {"",""});						

						}

						else

						{						

							newMail.Recipients.Remove(newMail.Recipients.Count);

							newMail.Recipients.Add(s);

						}	

					}					

				}				

			}		

			if(MailSubject != null)

			{

				newMail.Subject = MailSubject;

			}

			else

			{

				newMail.Subject = "";

			}

			if(MailBody != null)

			{

				newMail.Body = MailBody;

			}

			else

			{

				newMail.Body = "";

			}

            position = (int)MailBody.Length + 1;//Position of the attachment in the outlook editor.			

			attachType = (int)Outlook.OlAttachmentType.olByValue;	

			//Add all the attachments to new mail.

			foreach(string path in attachmentSource)

			{

				//Add the attachment if file is present in the specified path.

				if (File.Exists(path))				

				{

					fi = new FileInfo(path);

                    displayName = fi.Name ;							

					newMail.Attachments.Add(path,attachType,position,displayName);

				}

			}

			if (OpenEmailEditor == true)				

			{				

				newMail.Display(true);

			}

			else

			{	

					

					newMail.Send();								

								}	

			oRecipient = null;

			newMail = null;

			myOutlook = null;			



		}

解决方案

Hi
Any update on above. IS tehre any alternative to make it work without depending on "outlook instance running or not".
as mentioned above my platform is:
 .net framewrok 1.1
 Windows Xp Propfessional
 MS Office 2007.
Visual Studio 2003.


这篇关于未打开Outlook时Microsoft.Office.Interop.Outlook._MailItem.get_Recipients()提供错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆