【问题标题】:Create a new email with c# and attach a file Outlook 2016使用 c# 创建新电子邮件并附加文件 Outlook 2016
【发布时间】:2017-10-28 19:27:34
【问题描述】:

我有一份文件和电子邮件地址列表。

我正在尝试创建一种方法,我可以使用它来迭代抛出 emailAddresses 列表,并为每个创建一个新电子邮件,我 Outlook 并附加相应的文档。

创建 MailItem 是我卡住了。 在 MSDN 上找到了一个示例,该示例应该适用于 Office 2013 及更高版本。

这是我目前所拥有的:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Office.Interop.Outlook;

namespace Test_Invoice
{
    class SendviaMail
    {
        string demofile = @"C:\Desktop\test-inv\1_Test Testesen_1_010.pdf";
        public void Send()
        {
            MailItem eMail = new MailItem();

            eMail.Subject = "Here is Your Invoice";
            eMail.To = "test@testesen.dk";
            eMail.Body = "Dette er en test mail for TestMailApp";

        }

        public void Send(string email, string filename)
        {

        }
    }
}

我一直在尝试理解 MSDN 上的文档 并阅读这里的一些帖子。

据我所知,下一步是添加附件(演示文件) 如果我理解正确,我需要类似的东西

eMail.AttachmentAdd = demofile;

但这不起作用。

可能是我没有正确理解库。 查看来自 MSDN https://msdn.microsoft.com/en-us/library/bb644320.aspx 的这个示例

这段代码的结果:

using Microsoft.Office.Interop.Outlook;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Outlook = Microsoft.Office.Interop.Outlook;

namespace Test_Invoice
{
    class SendviaMail
    {
        string demofile = @"C:\Desktop\test-inv\1_Test Testesen_1_010.pdf";
        public void Send()
        {
            Outlook.MailItem mail = Application.CreateItem(Outlook.OlItemType.olMailItem) as Outlook.MailItem;
            mail.Subject = "Quarterly Sales Report FY06 Q4";

            //MailItem eMail = new MailItem();

            //eMail.Subject = "tilmelding og faktura";
            //eMail.To = "test@testesen.dk";
            //eMail.Body = "Dette er en test mail for TestMailApp";
            //eMail.AttachmentAdd
        }

        public void Send(string email, string filename)
        {

        }
    }
}

【问题讨论】:

  • MSDN 似乎提供了一个不错的例子。
  • 我实际上已经看过了。可能只是我不明白。但 VS 无法识别 Application.CreateItem
  • 这是我得到的错误:错误 CS0120 非静态字段、方法或属性“_Application.CreateItem(OlItemType)”需要对象引用
  • 你能用Application.CreateItem更新你的代码吗?
  • 你是对的 - 链接的例子是错误的 - 你需要构造一个 Outlook.Application 实例来让它工作。准备好您正在运行的 Outlook 实例将询问您的应用是否可以使用打开的实例。

标签: c# email outlook


【解决方案1】:

固定的示例代码应如下所示:

var ol = new Outlook.Application();
Outlook.MailItem mail = ol.CreateItem(Outlook.OlItemType.olMailItem) as Outlook.MailItem;

请注意,您需要使用 using 声明声明 Outlook 别名:

using Outlook = Microsoft.Office.Interop.Outlook;

(就像你已经做过的那样)......但也必须删除

using Microsoft.Office.Interop.Outlook;

以避免 Visual Studio 混淆您的引用。

MSDN 示例的其余部分应该可以按预期工作我没有任何其他示例可以指点您,因此您最好搜索Microsoft.Office.Interop.Outlook 或在此处探索相关问题。

【讨论】:

【解决方案2】:

为此使用 .Net 类:

public static void CreateMessageWithAttachment(string server,string filePath)
{
    // Specify the file to be attached and sent.
    // This example assumes that a file named Data.xls exists in the
    // current working directory.
    string file = filePath;
    // Create a message and set up the recipients.
    MailMessage message = new MailMessage(
       "jane@contoso.com",
       "ben@contoso.com",
       "Quarterly data report.",
       "See the attached spreadsheet.");

    // Create  the file attachment for this e-mail message.
    Attachment data = new Attachment(file, MediaTypeNames.Application.Octet);
    // Add time stamp information for the file.
    ContentDisposition disposition = data.ContentDisposition;
    disposition.CreationDate = System.IO.File.GetCreationTime(file);
    disposition.ModificationDate = System.IO.File.GetLastWriteTime(file);
    disposition.ReadDate = System.IO.File.GetLastAccessTime(file);
    // Add the file attachment to this e-mail message.
    message.Attachments.Add(data);

    //Send the message.
    SmtpClient client = new SmtpClient(server);
    // Add credentials if the SMTP server requires them.
    client.Credentials = CredentialCache.DefaultNetworkCredentials;

    try {
      client.Send(message);
    }
    catch (Exception ex) {
      Console.WriteLine("Exception caught in CreateMessageWithAttachment(): {0}", 
                  ex.ToString() );            
    }
    data.Dispose();
}

之后只需从循环中迭代列表并将其发送到函数

更多示例,您可以使用https://www.codeproject.com/Tips/286952/create-a-simple-smtp-server-in-csharp

【讨论】:

  • 这会在 Windows 中使用当前用户的电子邮件配置文件吗?它是一个 wpf 应用程序,需要使用当前用户的 Outlook 来生成电子邮件。
  • 如果你的电脑上有 smpt 服务器,你需要发送到函数或者只是在函数中硬编码初始化它
  • 很好的例子。但在我看来,通过将 SMTP 服务器写入应用程序以便能够发送电子邮件,我们使事情变得过于复杂。由于 Filburt 通过使用 Outlook 接缝进行了很好的记录,并且上面有很多帖子和示例,因此超越了其他方法。但我似乎错过了这个难题的某些部分。
  • @Jonas 通过 smtp 发送消息不涉及 编程 开销 - 但是很难说服您的管理员允许为每个用户进行 smtp 中继。此外,如果您的要求是在您的 Exchange 帐户中跟踪您的邮件项目,那么引用的示例就是要走的路。
  • 这会将电子邮件从应用程序主机提交到 SMTP 服务器。原始发帖人要求创建一封带有附件的新电子邮件,以便从 Outlook 2016 发送。这是一个有用的替代方案,但没有回答问题。
猜你喜欢
  • 1970-01-01
  • 2013-07-30
  • 1970-01-01
  • 2016-05-15
  • 2023-03-22
  • 2020-11-21
  • 2010-11-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多