2017-05-28 110 views
1

我有一个文档和电子邮件地址列表。用c#创建一封新邮件并附上一个文件Outlook 2016

我想创建一个方法,我可以使用迭代抛出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 = "[email protected]"; 
      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 = "[email protected]"; 
      //eMail.Body = "Dette er en test mail for TestMailApp"; 
      //eMail.AttachmentAdd 
     } 

     public void Send(string email, string filename) 
     { 

     } 
    } 
} 
+1

[MSDN](https://msdn.microsoft.com/en-us/library/bb644320.aspx)似乎提供了一个体面的例子。 – Filburt

+0

我已经看过那个。这可能是我不理解它。但VS不能识别Application.CreateItem – Jonas

+0

这是我得到的错误:错误CS0120非静态字段,方法或属性'_Application.CreateItem(OlItemType)'需要对象引用' – Jonas

回答

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或在此处探索链接问题。

+0

找到此参考和示例以使用应用程序或获取活动进程https://msdn.microsoft.com/en-us/library/office/ff462097.aspx – Jonas

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(
     "[email protected]", 
     "[email protected]", 
     "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

+0

将使用当前用户的电子邮件在Windows中的配置文件 它是一个wpf应用程序,需要使用当前用户的outlook来生成电子邮件。 – Jonas

+0

如果你有smpt服务器上的电脑,你需要发送到该功能或只是初始化硬编码功能 –

+0

很好的例子。但是,通过将SMTP服务器写入应用程序以发送电子邮件,它使我感到困惑。由于Filburt通过使用Outlook接口进行了充分的文档记录,并且其上有许多帖子和示例,因此Filburt超过了其他方法。但我缝隙失踪的部分难题。 – Jonas

相关问题