Skip to main content
 首页 » 编程设计

c#之在列表框 Imap/pop3 的文本框中显示电子邮件正文

2024年02月24日35kenshinobiy

所以当我选择电子邮件的主题时,我有一个显示电子邮件主题的列表框(我使用 chilkat imap 客户端)我想在文本框中显示邮件正文,但我不知道该怎么做,显然我使用了列表框 selectindexchangaged 事件,但我将如何处理它

到目前为止的代码

// Create an object, connect to the IMAP server, login, 
        // and select a mailbox. 
        Chilkat.Imap imap = new Chilkat.Imap(); 
        imap.UnlockComponent("UnlockCode"); 
        imap.Connect("Imap URL"); 
        imap.Login("email address", "password"); 
        imap.SelectMailbox("Inbox"); 
 
        // Get a message set containing all the message IDs 
        // in the selected mailbox. 
        Chilkat.MessageSet msgSet; 
        msgSet = imap.Search("ALL", true); 
 
        // Fetch all the mail into a bundle object. 
        Chilkat.EmailBundle bundle = new Chilkat.EmailBundle(); 
        bundle = imap.FetchBundle(msgSet); 
 
        // Loop over the bundle and display the From and Subject. 
        Chilkat.Email email; 
        int i; 
        for (i = 0; i < bundle.MessageCount - 1; i++) 
        { 
 
            email = bundle.GetEmail(i); 
            listBox1.Items.Add(email.From + ": " + email.Subject); 
            textBox1.Text = email.Body ; 
 
        } 
 
        // Save the email to an XML file 
        bundle.SaveXml("bundle.xml"); 
 
 
 
        // Disconnect from the IMAP server. 
        // This example leaves the email on the IMAP server. 
        imap.Disconnect(); 
    } 
} 

提前致谢

请您参考如下方法:

假设电子邮件索引保持不变(我认为确保这一点的最安全方法是在表单中缓存获取的包),我将改为使用 ListView ListBox,然后我将索引添加到列表中,作为单独的列或在项目的 Tag 中。

将 ListView 设置为您需要的外观后(ListView.View = View.Details; 和 ListView.MultiSelect = false; 可能是主要的)而不是:

listBox1.Items.Add(email.From + ": " + email.Subject); 

你可以做类似的事情(如果你用 Tag 方式来做,这稍微容易一些,但有些人认为不好):

listView1.Items.Add(email.From + ": " + email.Subject).Tag = i; 

然后当用户在列表中选择一个主题时,如您所说,您处理 ListView.SelectedIndexChanged 事件,然后执行如下操作:

if(ListView.SelectedItems.Count > 0) 
{ 
    textBox1.Text = bundle.GetEmail((int)ListView.SelectedItems[0].Tag).Body; 
} 

或者,如果您确定只想从电子邮件中提取文本,则可以将文本插入标签而不是索引。