可能重复:
delaying the sending of email messages without using Thread.Sleep c#
所以我试图在我的应用程序的for循环中启动一个新线程。我根本不熟悉线程,所以我需要一些信息。
我使用这种方法通过outlook发送电子邮件:
public void sendEMailThroughOUTLOOK(string recipient, string subject, string body)
{
try
{
// Create the Outlook application.
Outlook.Application oApp = new Outlook.Application();
// Create a new mail item.
Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
// Set HTMLBody.
//add the body of the email
oMsg.Body = body;
oMsg.Subject = subject;
// Add a recipient.
Outlook.Recipients oRecips = (Outlook.Recipients)oMsg.Recipients;
// Change the recipient in the next line if necessary.
Outlook.Recipient oRecip = (Outlook.Recipient)oRecips.Add(recipient);
oRecip.Resolve();
// Send.
oMsg.Send();
// Clean up.
oRecip = null;
oRecips = null;
oMsg = null;
oApp = null;
}//end of try block
catch (Exception ex)
{
}//end of catch
//end of Email Method
}
在foreach循环中,我每次迭代都会发送一封电子邮件。
但是我需要延迟这些电子邮件,但我不想睡觉UI线程。
几个小时前我问过并得到了一些答案,但却无法经常查看我的问题。
我尝试了他们的一些建议,比如使用这个:
Thread oThread = new Thread(new ThreadStart((sendEMailThroughOUTLOOK(recipient, subjectLine, finalbody)));
但是我得到了一个错误。它说它期待一个方法名称......我确实有一个方法名称。是因为我方法的论据吗?
答案 0 :(得分:0)
您可以这样做,也可以查看ParameterizedThreadStart:
Thread oThread = new Thread(new ThreadStart(() => { sendEMailThroughOUTLOOK(recipient, subjectLine, finalbody); }));