我正在创建一个程序,它发送一个文本消息,然后根据答复执行一个特定的操作。不管怎样,这是我的代码:
using Microsoft.Office.Interop.Outlook;
using Outlook = Microsoft.Office.Interop.Outlook;
static void Main()
{
var outlook = new Microsoft.Office.Interop.Outlook.Application();
// fire event when a new email arives
outlook.NewMailEx += new ApplicationEvents_11_NewMailExEventHandler(oApp_NewMailEx);
// etc
}
static void oApp_NewMailEx(string EntryIDCollection)
{
var outlook = new Microsoft.Office.Interop.Outlook.Application();
MailItem temp = (MailItem)outlook.Session.GetItemFromID(EntryIDCollection, Missing.Value);
var body = temp.Body; // the body of the email is null! I tried waiting and it is null until I open it...
Console.WriteLine(body);
}这个部分并不重要,但是我用这个功能发送了“文本消息”:
// send text message "att.txt.net only works with at&t phones"
public static int SendEmail(string recipeint = "9546543930@att.txt.net")
{
try
{
// Create the Outlook application by using inline initialization.
Outlook.Application oApp = new Outlook.Application();
//Create the new message by using the simplest approach.
Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
Outlook.Accounts accounts = oMsg.Session.Accounts;
foreach (Outlook.Account account in accounts)
{
// When the e-mail address matches, send the mail.
if ( account.SmtpAddress.Contains("gmail") )
{
oMsg.SendUsingAccount = account;
break;
}
}
// If you want to, display the message.
oMsg.Display(true); //modal
//Add a recipient.
Outlook.Recipient oRecip = (Outlook.Recipient)oMsg.Recipients.Add(recipeint);
oRecip.Resolve();
//Set the basic properties.
oMsg.Subject = "This is the subject of the test message";
oMsg.Body = "This is the text in the message.";
//Add an attachment.
// TODO: change file path where appropriate
//String sSource = "C:\\setupxlg.txt";
//String sDisplayName = "MyFirstAttachment";
//int iPosition = (int)oMsg.Body.Length + 1;
//int iAttachType = (int)Outlook.OlAttachmentType.olByValue;
//Outlook.Attachment oAttach = oMsg.Attachments.Add(sSource, iAttachType, iPosition, sDisplayName);
//Send the message.
oMsg.Save();
oMsg.Send();
//Explicitly release objects.
oRecip = null;
//oAttach = null;
oMsg = null;
oApp = null;
}
// Simple error handler.
catch (System.Exception e)
{
Console.WriteLine("{0} Exception caught: ", e);
}
//Default return value.
return 0;
}因此,我可以用我的gmail帐户发送一封电子邮件,当我回复文本消息时,函数oApp_NewMailEx将使用新邮件的id执行。我能够得到的主题,但身体不会被下载,直到我悬停我的鼠标在电子邮件或打开电子邮件。我已经在另一条线上放弃了2分钟,然后试着去看身体,结果仍然是空的。
编辑备注使我的工作:

它看起来是灰色的,因为我没有作为管理员运行outlook。如果您以管理员身份运行outlook,您将能够更新安全设置。
我还导入了MicrosoftOutlook14.0对象库作为参考。
发布于 2013-03-06 23:01:49
一旦收到电子邮件,我就调用“发送和接收”方法。等一下,然后我就能读到身体了。
outlook.Session.SendAndReceive(false);https://stackoverflow.com/questions/15080617
复制相似问题