WM短信读取(C#)

//采用DataGrid现实

//读取收件箱内信息
private void button1_Click(object sender, EventArgs e)
        
{
            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())
            
{

                NetMAPI mapi 
= new NetMAPI();
                
if (mapi.Login())
                
{
                    
if (mapi.OpenMessageStore())
                    
{
  
                        
if (mapi.OpenInbox())
                        
{

                            
if (mapi.GetContents())
                            
{
                                
int count = mapi.GetRowCounts();
                                SmsMessage msg;
                                StringBuilder s 
= new StringBuilder(140);
                                
int i = 0;
                                DataRow dr;
                                
while (mapi.GetNextMessage(out msg, false))
                                
{
                                    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();
                                    
////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)
                                    
{
                                        
break;
                                    }


                                }


                            }

                        }


                    }

                    
                    mapi.Logout();
                }

            }

            
else
            
{
                MessageBox.Show(
"faile");
            }

            NetMAPI.Term();
            
this.dataGrid1.DataSource = mydt;
        }


//读取发件箱内短信
private void button2_Click(object sender, EventArgs e)
        
{
            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())
            
{

                NetMAPI mapi 
= new NetMAPI();
                
if (mapi.Login())
                
{
                    
if (mapi.OpenMessageStore())
                    
{

                        
if (mapi.OpenSentItems())
                        
{

                            
if (mapi.GetContents())
                            
{

                                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))
                                
{
                                    i
++;

                                    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();
                                    
////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;
                                    
//}

                                }


                            }

                        }


                    }


                    mapi.Logout();
                }

            }

            
else
            
{
                MessageBox.Show(
"faile");
            }

            NetMAPI.Term();
            
this.dataGrid1.DataSource = mydt;

        }

//这两段代码读取的是收件箱和发件箱里全部短信,还不是很理想.....

posted @ 2009-05-06 08:27  王长喜  阅读(957)  评论(0编辑  收藏  举报