使用 Outlook 生成多封邮件
Generate Multiple Mails with Outlook
我想用 Outlook 向多个客户发送电子邮件。为此,我的程序中有一个方法可以迭代收件人、编写消息正文并将第一条消息显示为预览。
这是该方法的简化版本:
public void CreateMails(List<InfoMailRecipient> recipients)
{
Microsoft.Office.Interop.Outlook.Application outlook = new Microsoft.Office.Interop.Outlook.Application();
foreach (InfoMailRecipient recipient in recipients)
{
MailItem mail = outlook.CreateItem(OlItemType.olMailItem);
mail.SentOnBehalfOfName = "Sending User";
mail.BCC = recipient.EMailAddress;
mail.Subject = "TEST";
mail.BodyFormat = OlBodyFormat.olFormatHTML;
mail.HTMLBody = "<html><body>test</body></html>";
mail.Display(true);
}
}
当显示Outlook消息window时,无论我只是关闭window还是点击"Send",都会立即创建下一个MailItem
我得到一个异常 "RPC Server unavailable"。显然是因为 Outlook 已关闭。我发现当我删除行时
mail.Display(true);
只需调用 .Send();
,所有消息都已正确发送。但随后 Outlook 保持打开状态。即使我在 foreach
循环之后调用 .Quit()
。
如何正确处理这个 Outlook 实例?
更新 1 - 手动 GC 调用
public void CreateMails(List<InfoMailRecipient> recipients)
{
Microsoft.Office.Interop.Outlook.Application outlook = new Microsoft.Office.Interop.Outlook.Application();
foreach (InfoMailRecipient recipient in recipients)
{
MailItem mail = outlook.CreateItem(OlItemType.olMailItem);
mail.SentOnBehalfOfName = "Sending User";
mail.BCC = recipient.EMailAddress;
mail.Subject = "TEST";
mail.BodyFormat = OlBodyFormat.olFormatHTML;
mail.HTMLBody = "<html><body>test</body></html>";
mail.Send();
}
outlook.Quit();
GC.Collect();
GC.WaitForPendingFinalizers();
}
Outlook 保持 运行.
这似乎有效 - 你能告诉我们它是否也适合你吗?
public static void Main(string[] args)
{
CreateMails(new List<string>() {"emailaddresshere"});
Console.WriteLine("finished");
Console.ReadLine();
}
public static void CreateMails(List<string> recipients)
{
Microsoft.Office.Interop.Outlook.Application outlook = new Microsoft.Office.Interop.Outlook.Application();
foreach (string recipient in recipients)
{
MailItem mail = outlook.CreateItem(OlItemType.olMailItem);
mail.SentOnBehalfOfName = "Sending User";
mail.BCC = recipient;
mail.Subject = "TEST";
mail.BodyFormat = OlBodyFormat.olFormatPlain;
mail.HTMLBody = "Hello";
mail.Send();
System.Runtime.InteropServices.Marshal.ReleaseComObject(mail); // key change
}
GC.Collect();
GC.Collect();
GC.WaitForPendingFinalizers();
outlook.Application.Quit();
outlook.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(outlook); // key change
outlook = null;
GC.Collect();
GC.Collect();
GC.WaitForPendingFinalizers();
}
另请阅读 https://ausdotnet.wordpress.com/category/technical/com-interop/ and 。
我想用 Outlook 向多个客户发送电子邮件。为此,我的程序中有一个方法可以迭代收件人、编写消息正文并将第一条消息显示为预览。
这是该方法的简化版本:
public void CreateMails(List<InfoMailRecipient> recipients)
{
Microsoft.Office.Interop.Outlook.Application outlook = new Microsoft.Office.Interop.Outlook.Application();
foreach (InfoMailRecipient recipient in recipients)
{
MailItem mail = outlook.CreateItem(OlItemType.olMailItem);
mail.SentOnBehalfOfName = "Sending User";
mail.BCC = recipient.EMailAddress;
mail.Subject = "TEST";
mail.BodyFormat = OlBodyFormat.olFormatHTML;
mail.HTMLBody = "<html><body>test</body></html>";
mail.Display(true);
}
}
当显示Outlook消息window时,无论我只是关闭window还是点击"Send",都会立即创建下一个MailItem
我得到一个异常 "RPC Server unavailable"。显然是因为 Outlook 已关闭。我发现当我删除行时
mail.Display(true);
只需调用 .Send();
,所有消息都已正确发送。但随后 Outlook 保持打开状态。即使我在 foreach
循环之后调用 .Quit()
。
如何正确处理这个 Outlook 实例?
更新 1 - 手动 GC 调用
public void CreateMails(List<InfoMailRecipient> recipients)
{
Microsoft.Office.Interop.Outlook.Application outlook = new Microsoft.Office.Interop.Outlook.Application();
foreach (InfoMailRecipient recipient in recipients)
{
MailItem mail = outlook.CreateItem(OlItemType.olMailItem);
mail.SentOnBehalfOfName = "Sending User";
mail.BCC = recipient.EMailAddress;
mail.Subject = "TEST";
mail.BodyFormat = OlBodyFormat.olFormatHTML;
mail.HTMLBody = "<html><body>test</body></html>";
mail.Send();
}
outlook.Quit();
GC.Collect();
GC.WaitForPendingFinalizers();
}
Outlook 保持 运行.
这似乎有效 - 你能告诉我们它是否也适合你吗?
public static void Main(string[] args)
{
CreateMails(new List<string>() {"emailaddresshere"});
Console.WriteLine("finished");
Console.ReadLine();
}
public static void CreateMails(List<string> recipients)
{
Microsoft.Office.Interop.Outlook.Application outlook = new Microsoft.Office.Interop.Outlook.Application();
foreach (string recipient in recipients)
{
MailItem mail = outlook.CreateItem(OlItemType.olMailItem);
mail.SentOnBehalfOfName = "Sending User";
mail.BCC = recipient;
mail.Subject = "TEST";
mail.BodyFormat = OlBodyFormat.olFormatPlain;
mail.HTMLBody = "Hello";
mail.Send();
System.Runtime.InteropServices.Marshal.ReleaseComObject(mail); // key change
}
GC.Collect();
GC.Collect();
GC.WaitForPendingFinalizers();
outlook.Application.Quit();
outlook.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(outlook); // key change
outlook = null;
GC.Collect();
GC.Collect();
GC.WaitForPendingFinalizers();
}
另请阅读 https://ausdotnet.wordpress.com/category/technical/com-interop/ and