想要使用C#创建一个Windows应用程序来使用outlook读取和发送邮件 [英] Want to create a windows application using C# to read and send mail using outlook

查看:90
本文介绍了想要使用C#创建一个Windows应用程序来使用outlook读取和发送邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用以下代码发送邮件:

 private void btn_Send_Click(object sender,EventArgs e)
{
try
{
Outlook._Application _app = new Outlook.Application();
Outlook.MailItem mail =(Outlook.MailItem)_app.CreateItem(Outlook.OlItemType.olMailItem);
mail.To = txt_To.Text;
mail.Subject = txt_Subject.Text;
mail.Body = txt_Message.Text;
mail.Importance = Outlook.OlImportance.olImportanceNormal;
((Outlook._MailItem)mail).Send();
MessageBox.Show(您的消息已成功发送。,消息,MessageBoxButtons.OK,MessageBoxIcon.Information);
}
catch(exception ex)
{
MessageBox.Show(ex.Message,Message,MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}





但我在阅读来自outlook的邮件时遇到错误,下面是我阅读邮件的代码:

 private void btn_Receive_Click(object sender,EventArgs e)
{
try
{
Outlook._Application _app = new Outlook.Application();
Outlook._NameSpace _ns = _app.GetNamespace(MAPI);
Outlook.MAPIFolder inbox = _ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
_ns.SendAndReceive(true);
dt = new DataTable(收件箱);
dt.Columns.Add(Subject,typeof(string));
dt.Columns.Add(Sender,typeof(string));
dt.Columns.Add(Body,typeof(string));
dt.Columns.Add(Date,typeof(string));
dataGrid.DataSource = dt;
foreach(inbox.Items中的Outlook.MailItem项)
{
dt.Rows.Add(new object [] {item.Subject,item.SenderName,item.HTMLBody,item.SentOn .ToLongDateString()++ item.SentOn.ToLongDateString()});
}

}
catch(exception ex)
{
MessageBox.Show(ex.Message,Message,MessageBoxButtons.OK,MessageBoxIcon。错误);
}
}





错误是:无法转换'System'类型的COM对象。 _ComObject'到接口类型'Microsoft.Office.Interop.Outlook.MailItem'。此操作失败,因为对于IID为{00063034-0000-0000-C000-000000000046}的接口的COM组件的QueryInterface调用由于以下错误而失败:不支持此类接口(HRESULT异常:0 * 80004002(E_NOINTERFACE) ))。



我尝试过:



谷歌搜索后,我发现我们必须检查If Item.Class = 43,但是当我尝试将if If :( if(item.Class = 43))条件时,它显示一个错误,说明

属性或索引器'_MailItem.Class'无法分配 - 它是只读的





请帮我解决这个问题。

解决方案

简单:

  if (item.Class =  43 



属性或索引器'_MailItem .Class'无法分配 - 它是只读的

一个等于是一个赋值。二是比较。尝试:

  if (item.Class ==  43 


I am able to send the mail using below code:

private void btn_Send_Click(object sender, EventArgs e)
       {
           try
           {
               Outlook._Application _app = new Outlook.Application();
               Outlook.MailItem mail = (Outlook.MailItem)_app.CreateItem(Outlook.OlItemType.olMailItem);
               mail.To = txt_To.Text;
               mail.Subject = txt_Subject.Text;
               mail.Body = txt_Message.Text;
               mail.Importance = Outlook.OlImportance.olImportanceNormal;
               ((Outlook._MailItem)mail).Send();
               MessageBox.Show("Your Message has been successfully sent.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
           }
       }



But i am facing an error while reading the mails from outlook, below is my code to read the mail:

private void btn_Receive_Click(object sender, EventArgs e)
       {
           try
           {
               Outlook._Application _app = new Outlook.Application();
               Outlook._NameSpace _ns = _app.GetNamespace("MAPI");
               Outlook.MAPIFolder inbox = _ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
               _ns.SendAndReceive(true);
               dt = new DataTable("Inbox");
               dt.Columns.Add("Subject", typeof(string));
               dt.Columns.Add("Sender", typeof(string));
               dt.Columns.Add("Body", typeof(string));
               dt.Columns.Add("Date", typeof(string));
               dataGrid.DataSource = dt;
               foreach (Outlook.MailItem item in inbox.Items)
               {
                   dt.Rows.Add(new object[] { item.Subject, item.SenderName, item.HTMLBody, item.SentOn.ToLongDateString() + "" + item.SentOn.ToLongDateString() });
               }

           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
           }
       }



Error is: Unable to cast COM object of type 'System._ComObject' to interface type 'Microsoft.Office.Interop.Outlook.MailItem'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{00063034-0000-0000-C000-000000000046}' failed due to the following error: No such interface supported (Exception from HRESULT: 0*80004002 (E_NOINTERFACE)).

What I have tried:

After googling i found that we have to check If Item.Class=43, but when I try putting that If like: (if(item.Class=43)) condition, it shows a error stating 

Property or indexer '_MailItem.Class' cannot be assigned to -- it is read only 



Kindly help me to fix this.

解决方案

Simple:

if(item.Class=43)


Property or indexer '_MailItem.Class' cannot be assigned to -- it is read only

One equals is an assignment. two is a comparison. Try:

if(item.Class == 43)


这篇关于想要使用C#创建一个Windows应用程序来使用outlook读取和发送邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆