使用c#创建新电子邮件并附加Outlook 2016文件

时间:2017-05-28 10:41:40

标签: c# email outlook

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

我正在尝试创建一个方法,我可以用来迭代抛出emailAddresses列表,并为每个创建一个新的电子邮件I 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)
        {

        }
    }
}

2 个答案:

答案 0 :(得分:1)

使用.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

答案 1 :(得分:1)

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

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

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

using Outlook = Microsoft.Office.Interop.Outlook;

(正如您已经做过的那样)......但也必须删除

using Microsoft.Office.Interop.Outlook;

避免Visual Studio混淆您的引用。

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