C#交换->;Outlook项目附件
我正在尝试(从我的Exchange Server Outlook)获取我的Outlook联系人。 im使用C#和email.Attachments.AddItemAttachment(变量); 我已经与Outlook建立了连接,可以使用脚本发送。我加载所有Outlook文件夹C#交换->;Outlook项目附件,c#,email,outlook,exchange-server,C#,Email,Outlook,Exchange Server,我正在尝试(从我的Exchange Server Outlook)获取我的Outlook联系人。 im使用C#和email.Attachments.AddItemAttachment(变量); 我已经与Outlook建立了连接,可以使用脚本发送。我加载所有Outlook文件夹 using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Exchange.
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;
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用Microsoft.Exchange.WebServices.Data;
使用Microsoft.Office.Interop.Outlook;
//使用Microsoft.Office.Interop.Outlook.MAPIFolder;
//导入outlook=Microsoft.Office.Interop.outlook
名称空间HelloWorld
{
班级计划
{
静态void Main(字符串[]参数)
{
ExchangeService服务=新的ExchangeService(ExchangeVersion.Exchange2010_SP1);
RedirectionUrlValidationCallback);
Url=newURI(“*******”);
service.Credentials=newWebCredentials(“*******”,“*******”);service.TraceEnabled=true;
service.TraceFlags=TraceFlags.All;
EmailMessage email=新的EmailMessage(服务);
/////////////////////////////////////////////////////////////////////
var outlookApplication=new Microsoft.Office.Interop.Outlook.Application();
NameSpace mapiNamespace=outlookApplication.GetNamespace(“MAPI”);
MAPIFolder contacts=mapiNamespace.GetDefaultFolder(OlDefaultFolders.olFolderContacts);
对于(int i=1;i
有人知道正确的语法是什么吗
email.Attachments.AddItemAttachment(testcontact)
类的函数在Attachments集合中创建一个新附件。您只需将该值作为第二个参数传递:
attachments = mailContainer.Attachments;
attachment = attachments.Add(mailToAttach,
Outlook.OlAttachmentType.olEmbeddeditem, 1, "The attached e-mail");
有关更多信息,请参阅
我还注意到以下代码行:
contacts.Items.Count
不要在一行代码中使用多个点。我总是建议打破调用链,在单独的代码行中声明每个属性或方法调用。它允许立即释放底层COM对象并避免可能的问题。有关更多信息,请参阅。我不知道要在哪里添加您的代码。你能从我的代码中发布一些我知道该在哪里添加的东西吗?