【问题标题】:C# Exchange -> Outlook Item AttachmentC# Exchange -> Outlook 项目附件
【发布时间】:2015-03-24 21:14:14
【问题描述】:

我正在尝试(从我的 Exchange Server Outlook)获取我的 Outlook 联系人。 我使用 C# 和 email.Attachments.AddItemAttachment(variable); 我已经连接到 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);

?

【问题讨论】:

    标签: c# email outlook exchange-server


    【解决方案1】:

    Attachments 类的Add 函数在附件集合中创建一个新附件。您只需将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 了解更多信息。

    【讨论】:

    • 我不知道我必须在哪里添加你的代码。你能从我的代码中发布一些我知道我必须在哪里添加的东西吗?
    猜你喜欢
    • 1970-01-01
    • 2012-02-26
    • 1970-01-01
    • 2016-11-10
    • 1970-01-01
    • 1970-01-01
    • 2015-03-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多