C# ts未使用outlook exchange发送,以及其他不使用的功能…必须使用我的学校电子邮件地址发送邮件。关于写作问题:发帖前,请查看你的帖子中是否有“似乎不起作用”或“不起作用”或“完全失败”,并替换为实际失败+消息+预期行为。这将使问题更具可操作性。
C# ts未使用outlook exchange发送,以及其他不使用的功能…必须使用我的学校电子邮件地址发送邮件。关于写作问题:发帖前,请查看你的帖子中是否有“似乎不起作用”或“不起作用”或“完全失败”,并替换为实际失败+消息+预期行为。这将使问题更具可操作性。,c#,multithreading,sleep,C#,Multithreading,Sleep,ts未使用outlook exchange发送,以及其他不使用的功能…必须使用我的学校电子邮件地址发送邮件。关于写作问题:发帖前,请查看你的帖子中是否有“似乎不起作用”或“不起作用”或“完全失败”,并替换为实际失败+消息+预期行为。这将使问题更具可操作性。我使用visual studio 2012。我可以使用Thread.Sleep(),但我真正需要做的是oThread.Sleep。oThread是我用来发送电子邮件的线程,是代码的第一行。我的意思是看代码,将thread.sleep放在发送电子
ts未使用outlook exchange发送,以及其他不使用的功能…必须使用我的学校电子邮件地址发送邮件。关于写作问题:发帖前,请查看你的帖子中是否有“似乎不起作用”或“不起作用”或“完全失败”,并替换为实际失败+消息+预期行为。这将使问题更具可操作性。我使用visual studio 2012。我可以使用Thread.Sleep(),但我真正需要做的是oThread.Sleep。oThread是我用来发送电子邮件的线程,是代码的第一行。我的意思是看代码,将thread.sleep放在发送电子邮件的方法中,使当前线程休眠。因为我在新的线程中调用了这个方法…我想它会休眠1分钟…但是电子邮件根本不会发送。即时消息无法发送!=我不睡觉。在休眠线程的行上放置一个断点,在随后的行上放置一个断点。然后你就可以清楚地知道这根线已经睡了60多岁了。一旦您确认了这一点,您就可以继续使用visual studio 2012找出电子邮件不发送的原因了。我可以使用Thread.Sleep(),但我真正需要做的是oThread.Sleep。oThread是我用来发送电子邮件的线程,是代码的第一行。我的意思是看代码,将thread.sleep放在发送电子邮件的方法中,使当前线程休眠。因为我在新的线程中调用了这个方法…我想它会休眠1分钟…但是电子邮件根本不会发送。即时消息无法发送!=我不睡觉。在休眠线程的行上放置一个断点,在随后的行上放置一个断点。然后你就可以清楚地知道这根线已经睡了60多岁了。一旦你确认了这一点,你就可以继续研究电子邮件为什么不发送了。
Thread oThread = new Thread(new ThreadStart(() => { sendEMailThroughOUTLOOK(recipientAddress, subjectLine, finalbody); }));
oThread.Start();
public void sendEMailThroughOUTLOOK(string recipient, string subject, string body)
{
Thread.Sleep(60000);
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
}
public void sendEMailThroughOUTLOOK(string recipient, string subject, string body)
{
Thread.Sleep(60000);
...
}
Thread oThread = new Thread(new ThreadStart(() => { sendEMailThroughOUTLOOK(recipientAddress, subjectLine, finalbody); }));
oThread.Start();
Thread t = new Thread();
t.Start();
t.Sleep(60000);
using System;
using System.ComponentModel;
using System.Windows.Forms;
using Outlook = Microsoft.Office.Interop.Outlook;
namespace Emailer
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void SendButton_Click(object sender, EventArgs e)
{
this.sendEmailBackgroundWorker.RunWorkerAsync(new _Email
{
Recipient = this.recipientTextBox.Text,
Subject = this.subjectTextBox.Text,
Body = this.emailToSendTextBox.Text
});
}
private class _Email
{
public string Body { get; set; }
public string Subject { get; set; }
public string Recipient { get; set; }
}
private void sendEmailBackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
var email = (_Email)e.Argument;
try
{
Outlook.Application oApp = new Outlook.Application();
Outlook.MailItem oMsg = (Microsoft.Office.Interop.Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
oMsg.Body = email.Body;
oMsg.Subject = email.Subject;
Outlook.Recipients oRecips = (Outlook.Recipients)oMsg.Recipients;
Outlook.Recipient oRecip = (Outlook.Recipient)oRecips.Add(email.Recipient);
oRecip.Resolve();
oMsg.Send();
oRecip = null;
oRecips = null;
oMsg = null;
oApp = null;
}
catch (Exception ex)
{
e.Result = ex;
}
e.Result = true;
}
private void sendEmailBackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
string message;
if (e.Result is Exception)
message = "Error sending email: " + (e.Result as Exception).Message;
else if (e.Result is bool && (bool)e.Result)
message = "Email is sent";
else
throw new Exception("Internal Error: not expecting " + e.Result.GetType().FullName);
MessageBox.Show(message);
}
}
}