//
采用DataGrid现实
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//
读取收件箱内信息
private
void
button1_Click(
object
sender, EventArgs e)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
DataTable mydt = new DataTable();
mydt.TableName = "InBox";
mydt.Columns.Add(new DataColumn("SenderName", typeof(string)));
mydt.Columns.Add(new DataColumn("SenderAddress", typeof(string)));
mydt.Columns.Add(new DataColumn("Subject", typeof(string)));
mydt.Columns.Add(new DataColumn("SendTime", typeof(string)));
mydt.Columns.Add(new DataColumn("ReceiveTime", typeof(string)));
mydt.Columns.Add(new DataColumn("SendTime1", typeof(DateTime)));
mydt.Columns.Add(new DataColumn("ReceiveTime1", typeof(DateTime)));
if (NetMAPI.MAPIInit())
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
NetMAPI mapi = new NetMAPI();
if (mapi.Login())
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (mapi.OpenMessageStore())
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (mapi.OpenInbox())
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (mapi.GetContents())
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int count = mapi.GetRowCounts();
SmsMessage msg;
StringBuilder s = new StringBuilder(140);
int i = 0;
DataRow dr;
while (mapi.GetNextMessage(out msg, false))
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
i++;
dr = mydt.NewRow();
msg.GetSenderName(s);
dr["SenderName"] = s.ToString();
//MessageBox.Show(s.ToString());
msg.GetSenderEmail(s);
dr["SenderAddress"] = s.ToString();
//MessageBox.Show(aa);
//MessageBox.Show(s.ToString());
msg.GetSubject(s);
dr["Subject"] = s.ToString();
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**/////MessageBox.Show(s.ToString());
msg.GetSubmitTime(s);
dr["SendTime"] = s.ToString();
msg.GetReceivedTime(s);
dr["ReceiveTime"] = s.ToString();
msg.Dispose();
mydt.Rows.Add(dr);
if (i == count - 1)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
break;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
mapi.Logout();
}
}
else
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
MessageBox.Show("faile");
}
NetMAPI.Term();
this.dataGrid1.DataSource = mydt;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//
读取发件箱内短信
private
void
button2_Click(
object
sender, EventArgs e)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
DataTable mydt = new DataTable();
mydt.TableName = "SendedBox";
mydt.Columns.Add(new DataColumn("SenderName", typeof(string)));
mydt.Columns.Add(new DataColumn("SenderAddress", typeof(string)));
mydt.Columns.Add(new DataColumn("Subject", typeof(string)));
mydt.Columns.Add(new DataColumn("SendTime", typeof(string)));
mydt.Columns.Add(new DataColumn("ReceiveTime", typeof(string)));
mydt.Columns.Add(new DataColumn("SendTime1", typeof(DateTime)));
mydt.Columns.Add(new DataColumn("ReceiveTime1", typeof(DateTime)));
if (NetMAPI.MAPIInit())
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
NetMAPI mapi = new NetMAPI();
if (mapi.Login())
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (mapi.OpenMessageStore())
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (mapi.OpenSentItems())
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (mapi.GetContents())
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
SmsMessage msg;
StringBuilder s = new StringBuilder(140);
StringBuilder s1 = new StringBuilder(25);
StringBuilder s2 = new StringBuilder(25);
int i = 0;
DataRow dr;
while (mapi.GetNextMessage(out msg, false))
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
i++;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
dr = mydt.NewRow();
msg.GetRecipients();
SmsMessage.RecipientType tt;
msg.GetNextRecipient(s1, s2, out tt);
dr["SenderName"] = s1.ToString();
dr["SenderAddress"] = s2.ToString();
//MessageBox.Show(aa);
//MessageBox.Show(s.ToString());
msg.GetSubject(s);
dr["Subject"] = s.ToString();
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**/////MessageBox.Show(s.ToString());
msg.GetSubmitTime(s);
dr["SendTime"] = s.ToString();
msg.GetReceivedTime(s);
dr["ReceiveTime"] = s.ToString();
msg.Dispose();
mydt.Rows.Add(dr);
//if (i > 230)
//{
// break;
//}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
mapi.Logout();
}
}
else
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
MessageBox.Show("faile");
}
NetMAPI.Term();
this.dataGrid1.DataSource = mydt;
}![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//这两段代码读取的是收件箱和发件箱里全部短信,还不是很理想.....