[英]Create a new email with c# and attach a file 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 = "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/zh-cn/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)
{
}
}
}
為此使用.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
固定的示例代碼應如下所示:
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
或在此處探索鏈接的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.