我写了一个课程,通过outlook自动发送电子邮件。在小案件旁边一切都很好,邮件生成,发送到"传出"文件夹,然后Outlook已关闭。收盘是如此之快,以至于我下次开始展望时会收到邮件。
这是我的代码:
public class MyMail
{
private const double WaitingForSending = 30.0;
#region Local variables
public bool SSL_Encryption = true;
public System.Collections.Generic.List<System.Net.Mail.MailAddress> Address = null;
public System.Collections.Generic.List<System.Net.Mail.MailAddress> CC = null;
public System.Collections.Generic.List<System.Net.Mail.MailAddress> BCC = null;
public System.Collections.Generic.List<string> AttachmentFileName = null;
public string Body = "";
public string Subject = "";
#endregion
public void SendMail()
{
double Waited = .0;
string fAddress = string.Empty;
if (this.Address == null) { return; }
Microsoft.Office.Interop.Outlook.Application OL = new Microsoft.Office.Interop.Outlook.Application();
Microsoft.Office.Interop.Outlook.MailItem Mail = (Microsoft.Office.Interop.Outlook.MailItem)OL.CreateItem(OlItemType.olMailItem);
Mail.Subject = this.Subject;
if (this.Address != null) { foreach (System.Net.Mail.MailAddress MA in this.Address) { fAddress += MA.Address + "; "; } Mail.To = fAddress; fAddress = string.Empty; }
if (this.CC != null) { foreach (System.Net.Mail.MailAddress MA in this.CC) { fAddress += MA.Address + "; "; } Mail.CC = fAddress; fAddress = string.Empty; }
if (this.BCC != null) { foreach (System.Net.Mail.MailAddress MA in this.BCC) { fAddress += MA.Address + "; "; } Mail.BCC = fAddress; fAddress = string.Empty; }
Mail.Body = this.Body;
if (this.AttachmentFileName != null) { foreach (string Att in this.AttachmentFileName) { if (System.IO.File.Exists(Att)) { Mail.Attachments.Add(Att, Microsoft.Office.Interop.Outlook.OlAttachmentType.olByValue, Type.Missing, Type.Missing); } } }
Mail.Display(false);
try
{
Mail.Send();
} catch (System.Exception ex) { throw ex; }
/*
while (!Mail.Sent && Waited < WaitingForSending)
{
System.Threading.Thread.Sleep(500);
Waited += 0.5;
}
*/
}
}
&#34;等待循环&#34;,我评论说不起作用,因为前景正在关闭
在函数Mail.Send()
中。
有没有人有想法,我怎么能让Outlook等到发送邮件?
问候, 扬
答案 0 :(得分:0)
Outlook会在最后一个资源管理器或检查器关闭后关闭。为了防止这种情况,要么确保显示资源管理器或检查器,要么至少保留对其中一个对象的引用(它们不必显示)。可以从MailItem.GetInspector
或MAPIFolder.GetExplorer
检索检查员。
您可能还想使用Namespace.SendAndRecive
发送邮件。请记住,SendAndRecive
是异步的,因此您需要等待SyncObject.SyncEnd
事件触发(此时您可以安全地释放资源管理器或检查器并让Outlook关闭)。可以从Namespace.SyncObjects
集合中检索SyncObject。
答案 1 :(得分:0)
我现在找到了另一个解决方案。请找到附带的工作解决方案:
public static class MAPIOutlook
{
public static OL.Application GetOutlook(out bool StillRunning)
{
OL.Application OLApp = null;
OL.NameSpace nameSpace = null;
if (System.Diagnostics.Process.GetProcessesByName("OUTLOOK").Count() > 0)
{
StillRunning = true;
try
{
OLApp = System.Runtime.InteropServices.Marshal.GetActiveObject("Outlook.Application") as Microsoft.Office.Interop.Outlook.Application;
}
catch {
KillOutlook();
OLApp = new OL.Application();
nameSpace = OLApp.GetNamespace("MAPI");
nameSpace.Logon("", "", System.Reflection.Missing.Value, System.Reflection.Missing.Value);
}
}
else
{
StillRunning = false;
OLApp = new OL.Application();
nameSpace = OLApp.GetNamespace("MAPI");
nameSpace.Logon("", "", System.Reflection.Missing.Value, System.Reflection.Missing.Value);
}
return OLApp;
}
public static void KillOutlook()
{
foreach (System.Diagnostics.Process p in System.Diagnostics.Process.GetProcessesByName("OUTLOOK")) { p.Kill(); }
}
public static void SendMailAdv(string[] To, string[] CC, string[] BCC, string Subject, string Body, string[] Attachment)
{
bool StillRunning = false;
OL.Application OlApp = GetOutlook(out StillRunning);
OL.NameSpace NS = OlApp.GetNamespace("MAPI");
OL.MAPIFolder MFold = NS.GetDefaultFolder(OL.OlDefaultFolders.olFolderOutbox);
OL.MailItem MI = (OL.MailItem)OlApp.CreateItem(OL.OlItemType.olMailItem);
OL.Recipients oReci = MI.Recipients;
foreach(string str in To)
{
OL.Recipient Rec = MI.Recipients.Add(str);
Rec.Type = (int)OL.OlMailRecipientType.olTo;
}
foreach (string str in CC)
{
OL.Recipient Rec = MI.Recipients.Add(str);
Rec.Type = (int)OL.OlMailRecipientType.olCC;
}
foreach (string str in To)
{
OL.Recipient Rec = MI.Recipients.Add(str);
Rec.Type = (int)OL.OlMailRecipientType.olBCC;
}
MI.Subject = Subject;
MI.Body = Body;
foreach(string str in Attachment)
{
if (System.IO.File.Exists(str.Trim()))
{
MI.Attachments.Add(str.Trim(), OL.OlAttachmentType.olByValue, System.Reflection.Missing.Value, System.Reflection.Missing.Value);
}
}
int nOutItems = MFold.Items.Count;
MI.Send();
while(nOutItems != MFold.Items.Count)
{
System.Threading.Thread.Sleep(250);
}
if (!StillRunning)
{
OlApp.Application.Quit();
OlApp.Quit();
KillOutlook();
}
}
}
答案 2 :(得分:0)
首先,我要遵循Dmitry在那里列出的建议。
此外,我注意到在调用MailItem类的Send方法之前显示了一个检查器窗口。这不是必需的,可能会导致您当前看到的行为。需要承认所有Outlook对象都在方法范围内声明,这限制了方法的生命周期。
import numpy as np
import matplotlib.pyplot as plt
Edge_matrix=[[269, 270], [270, 269], [274, 275], [275, 274], [341, 342],
[342, 341], [711, 712], [712, 711]]
x,y = zip(*Edge_matrix)
limits = [0,821]
sec_lim = 243
bp = 576
ax = plt.gca()
ax.set_xlim(limits)
ax.set_ylim(limits)
ax.scatter(x,y, s=1, c='blue', alpha=1)
ax.axhline(bp, linewidth=0.3, color='blue', label='zorder=2', zorder=2)
ax.axvline(bp, linewidth=0.3, color='blue', label='zorder=2', zorder=2)
ax2 = ax.twinx()
ax2.yaxis.tick_right()
ax2.set_ylim(ax.get_ylim())
yticks = ax.get_yticks()
ax2.set_yticks(np.append(yticks[yticks<bp], [bp,bp+sec_lim]) )
ax2.set_yticklabels(np.append(yticks[yticks<bp], [0,sec_lim]).astype(int) )
ax3 = ax.twiny()
ax3.xaxis.tick_top()
ax3.set_xlim(ax.get_xlim())
xticks = ax.get_xticks()
ax3.set_xticks(np.append(xticks[xticks<bp], [bp,bp+sec_lim]) )
ax3.set_xticklabels(np.append(xticks[xticks<bp], [0,sec_lim]).astype(int) )
plt.show()
确保成功发送邮件的最佳方法是处理“已发送邮件”文件夹中的ItemAdd事件。将一个或多个项目添加到指定集合时会触发该事件。