c# - 无法将附件添加到 C# 电子邮件
问题描述
我似乎很难让以下代码正常工作。如果我使用下面的代码,它表明该文件不存在,即使它刚刚创建(我可以在 explorer.xml 中看到它)。
string path = @"LicenceFile.stslic";
File.WriteAllText(path, LicenceKey.ToString() + ";" + CRCValue + ";" + dateExpiry.Value.ToString("dd MMMM yyyy") );
Outlook.Application _app = new Outlook.Application();
Outlook.MailItem mail = (Outlook.MailItem)_app.CreateItem(Outlook.OlItemType.olMailItem);
mail.To = txtEmail.Text;
mail.Subject = "Your System Licence Key";
mail.Body = "Hello " + Environment.NewLine + Environment.NewLine + "Please find attached your most recent licencing file for the use of the system." + Environment.NewLine + "Please tranfer this file to one of the STS machines." + Environment.NewLine + "Please double click on this file which should then return if the file has been installed succesfully." + Environment.NewLine + Environment.NewLine + "Many Thanks" + Environment.NewLine + Environment.NewLine + "Me";
mail.Importance = Outlook.OlImportance.olImportanceHigh;
mail.ReadReceiptRequested = true;
mail.Attachments.Add((path));
((Outlook._MailItem)mail).Send();
解决方案
mail.Attachments.Add() 方法有四个参数,可以参考下面的代码。
mail.Attachments.Add(path, Outlook.OlAttachmentType.olByValue, 1, path);
path 参数可以是一个文件(由带有文件名的完整文件系统路径表示)或构成附件的 Outlook 项目。可以参考以下代码示例
string path = @"C:\Users\v-zhzhen\Desktop\study notes\exchange.docx";
string str = "sadie@contoso.com";
Outlook.Application _app = new Outlook.Application();
Outlook.MailItem mail = (Outlook.MailItem)_app.CreateItem(Outlook.OlItemType.olMailItem);
mail.Subject = "Your System Licence Key";
mail.Body = "Hello " + Environment.NewLine + Environment.NewLine + "Please find attached your most recent licencing file for the use of the system." + Environment.NewLine + "Please tranfer this file to one of the STS machines." + Environment.NewLine + "Please double click on this file which should then return if the file has been installed succesfully." + Environment.NewLine + Environment.NewLine + "Many Thanks" + Environment.NewLine + Environment.NewLine + "Me";
mail.Importance = Outlook.OlImportance.olImportanceHigh;
mail.ReadReceiptRequested = true;
MessageBox.Show(path);
if (path.Length > 0)
{
mail.Attachments.Add(path, Outlook.OlAttachmentType.olByValue, 1, path);
}
mail.To = str;
mail.Display();
((Outlook._MailItem)mail).Send();
推荐阅读
- hibernate - 使用 hibernate.hbm2ddl.auto=update 将表添加到模式而不影响现有表?
- python - 如何通过新数据的标题将一列拆分为更多
- python - 我怎样才能对 Python 进行沙箱处理,这样一个项目就不会破坏我的其他项目?
- windows - 通过cygwin在windows中使用ranger正确打开文件(rifle.py)
- filtering - AdvancedDataGridView - 以编程方式设置自定义过滤器
- python - 如何获取具有人类令牌不和谐的成员列表
- reactjs - 捕获 Material UI 弃用警告
- nginx - 使用多个图像 NGINX 配置 HAproxy 负载平衡,但访问没有端口的 url
- r - 添加相邻列的交替对
- symfony5 - Symfony 5.3 如何从前置过滤器(EventSubscriberInterface)返回 JSON 响应并将变量设置为在控制器中访问?