gpt4 book ai didi

c# - 将文件附加到电子邮件 C#

转载 作者:行者123 更新时间:2023-12-01 22:17:35 26 4
gpt4 key购买 nike

我已经尝试了本网站提供的许多解决方案来将文件附加到电子邮件,但无论我尝试什么,我总是在我尝试将文件附加到我的 outlook mailitem 的行。

  try
{
App = new Microsoft.Office.Interop.Outlook.Application();

MailItem mailItem = App.CreateItem(OlItemType.olMailItem);

mailItem.Subject = Subject;
mailItem.To = To;
mailItem.CC = CC;
mailItem.BCC = BCC;
mailItem.Body = Body;

// make sure a filename was passed
if (string.IsNullOrEmpty(FileAtachment) == false)
{
// need to check to see if file exists before we attach !
if (!File.Exists(FileAtachment))
MessageBox.Show("Attached document " + FileAtachment + " does not exist", "File Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
else
{
System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment(FileAtachment);
mailItem.Attachments.Add(attachment, Microsoft.Office.Interop.Outlook.OlAttachmentType.olByValue, Type.Missing, Type.Missing);
}
}
mailItem.Display(); // display the email
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}

任何人都可以提供任何想法如何让它工作吗?我可以毫无问题地发送电子邮件,但是当我尝试添加附件时它不起作用:(

最佳答案

Attachments.Add方法接受构成附件的文件(由带文件名的完整文件系统路径表示)或 Outlook 项目,但不接受附件对象:

        App = new Microsoft.Office.Interop.Outlook.Application();

MailItem mailItem = App.CreateItem(OlItemType.olMailItem);

mailItem.Subject = Subject;
mailItem.To = To;
mailItem.CC = CC;
mailItem.BCC = BCC;
mailItem.Body = Body;

// make sure a filename was passed
if (string.IsNullOrEmpty(FileAtachment) == false)
{
// need to check to see if file exists before we attach !
if (!File.Exists(FileAtachment))
MessageBox.Show("Attached document " + FileAtachment + " does not exist", "File Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
else
{
Attachment attachment = mailItem.Attachments.Add("D:\\text.txt", Microsoft.Office.Interop.Outlook.OlAttachmentType.olByValue, Type.Missing, Type.Missing);
}
}
mailItem.Display(); // display the email

关于c# - 将文件附加到电子邮件 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43530026/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com