C#Exchange - > Outlook项目附件

时间:2015-03-24 09:08:37

标签: c# email outlook exchange-server

我试图(从我的Exchange Server Outlook)获取我的Outlook联系人。 即使用C#和email.Attachments.AddItemAttachment(变量); 我已经连接到Outlook,可以使用我的脚本发送。我加载了所有Outlook文件夹。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Exchange.WebServices.Data;
using Microsoft.Office.Interop.Outlook;
//using Microsoft.Office.Interop.Outlook.MAPIFolder;
//Imports outlook = Microsoft.Office.Interop.Outlook

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP1);
     RedirectionUrlValidationCallback);
            service.Url = new Uri("*******");

          service.Credentials = new WebCredentials("******","******");  service.TraceEnabled = true;
          service.TraceFlags = TraceFlags.All;
          EmailMessage email = new EmailMessage(service);


          /////////////////////////////////////////////////////////////////////
            var outlookApplication = new Microsoft.Office.Interop.Outlook.Application();
            NameSpace mapiNamespace = outlookApplication.GetNamespace("MAPI");
            MAPIFolder contacts = mapiNamespace.GetDefaultFolder(OlDefaultFolders.olFolderContacts);
            for (int i = 1; i < contacts.Items.Count + 1; i++)
            {
                var contact = (ContactItem)contacts.Items[i];
                itemAttachment.Name="contact.FullName";
                Console.WriteLine(contact.Email1Address);
                Console.WriteLine();


            }
              var testcontact = "adasd";
            /////////////////////////////////////////////////////////////////////

            email.ToRecipients.Add("********");
            email.Sender = new   Microsoft.Exchange.WebServices.Data.EmailAddress("*****");

            email.Subject = "Test subj";
            email.Body = new MessageBody("Test txt");
            email.Attachments.AddItemAttachment(testcontact);

            email.SendAndSaveCopy();

        }
        private static bool RedirectionUrlValidationCallback(string redirectionUrl)
        {
            // The default for the validation callback is to reject the URL.
            bool result = false;

            Uri redirectionUri = new Uri(redirectionUrl);

            // Validate the contents of the redirection URL. In this simple validation
            // callback, the redirection URL is considered valid if it is using HTTPS
            // to encrypt the authentication credentials. 
            if (redirectionUri.Scheme == "https")
            {
                result = true;
            }
            return result;
        }

    }
}

是否有人知道正确的语法是什么

  

email.Attachments.AddItemAttachment(testcontact);

1 个答案:

答案 0 :(得分:0)

Add类的Attachments功能会在“附件”集合中创建新附件。您只需将olEmbeddeditem值作为第二个参数传递:

    attachments = mailContainer.Attachments;
    attachment = attachments.Add(mailToAttach, 
       Outlook.OlAttachmentType.olEmbeddeditem, 1, "The attached e-mail");

有关详细信息,请参阅How To: Add an existing Outlook e-mail message as an attachment

此外,我注意到以下代码行:

 contacts.Items.Count

不要在单行代码中使用多个点。我总是建议打破调用链并在单独的代码行上声明每个属性或方法调用。它允许立即释放底层COM对象并避免可能的问题。有关详细信息,请参阅Systematically Releasing Objects