使用日历项目中的附件 - Outlook - C#
问题描述:
我正在尝试使用包含在计划中的日历项目中的附件。 我有一个从前一个对话框中选择日历主题行的列表,并且当主题正确传输时,主体运行不正常(完全是另一个问题),但附件不起作用。 这里是我的foreach循环当附件被放入附件阵列以备后用:使用日历项目中的附件 - Outlook - C#
string[] subjects = new string[dialog.chosen.Count];
string[] bodies = new string[dialog.chosen.Count];
Attachments[] attach = new Attachments[dialog.chosen.Count];
foreach (Outlook.AppointmentItem appt in rangeAppts)
{
foreach (string text in dialog.chosen)
{
if (text == appt.Subject)
{
subjects[i] = appt.Subject;
bodies[i] = Convert.ToString(appt.Body);
attach[i] = appt.Attachments;
i = i + 1;
}
}
}
然后在此处,我居然调用方法:
sendEmailTemplate(bodies[i], subject, to, "", attachment: attach[i]);
,然后方法本身:
public void sendEmailTemplate(string body, string subject, string to, string cc , Attachments attachment = null)
{
Microsoft.Office.Interop.Outlook.Application oApp = new Microsoft.Office.Interop.Outlook.Application();
Microsoft.Office.Interop.Outlook._MailItem oMailItem = (Microsoft.Office.Interop.Outlook._MailItem)oApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
oMailItem.HTMLBody = body;
oMailItem.Subject = subject;
try
{
oMailItem.Attachments.Add(attachment);
}
catch {}
oMailItem.To = to;
oMailItem.CC = cc;
oMailItem.Display(false);
oMailItem.Application.ActiveInspector().WindowState = Microsoft.Office.Interop.Outlook.OlWindowState.olNormalWindow;
}
我试过几件事,但是当我真的去发送电子邮件时,我最终得到:
例外:找不到会员。 HRESULT:0x80020003
然后我一直无法得到任何其他工作。该方法的try/catch循环是为了防止上述异常,因为无论是否存在附件,我都会收到该异常,现在附件不会被添加。
我使用的Office随C#一起提供的Interop。 Winforms如果有所作为。
答
MailItem.Attachments接受一个字符串(完全限定文件名)或另一个Outlook项目(MailItem,ContactItem等)。
您无法将Attachments对象作为参数传递。如果您需要复制附件,请循环访问附件集合中的所有附件,为每个附件调用Attachment.SaveAsFile,将文件名传递给MailItem.Attachments.Add,删除您的临时文件。
非常感谢 - 有没有办法根据它的文件类型来命名文件,因此它是可以打开的?这些文件已被正确下载,但他们没有得到正确的文件扩展名。使用Attachment.FileName/DisplayName/FilePath /其他任何东西似乎都不起作用。该程序需要能够下载所有类型的文件并保持可打开状态。 – Maelo
不使用Outlook对象模型 - 您可以在调用MailItem.Attachments之前正确命名(或重命名)该文件,也可以使用Redemption来设置文件名。 –
有没有办法检测文件类型,所以我可以在文件扩展名中进行重命名编程? – Maelo