这是代码。
namespace Email
{
public partial class Form1 : RibbonForm
{
//private EventHandler<WriteLogEventArgs> m_pLogCallback = null;
public static IMAP_Client m_pIMAP = null;
public Form1()
{
m_pConnect_Click();
//LoadFolders();
//LoadFolderMessages("Inbox");
//MessageBox.Show("Done!");
InitializeComponent();
InitSkinGallery();
ribbonControl.SelectedPage = homeRibbonPage1;
}
void InitSkinGallery()
{
SkinHelper.InitSkinGallery(rgbiSkins, true);
}
private void Form1_Load(object sender, EventArgs e)
{
//LoadFolderMessages("Inbox");
}
public void m_pImap_Fetch_MessageItems_UntaggedResponse(object sender, EventArgs<IMAP_r_u> e)
{
try
{
if (e.Value is IMAP_r_u_Fetch)
{
IMAP_r_u_Fetch fetchResp = (IMAP_r_u_Fetch)e.Value;
this.BeginInvoke(new MethodInvoker(delegate()
{
try
{
ListViewItem currentItem = new ListViewItem();
currentItem.ImageIndex = 0;
currentItem.Tag = fetchResp.UID.UID;
string from = "";
if (fetchResp.Envelope.From != null)
{
for (int i = 0; i < fetchResp.Envelope.From.Length; i++)
{
if (i == fetchResp.Envelope.From.Length - 1)
{
from += fetchResp.Envelope.From[i].ToString();
}
else
{
from += fetchResp.Envelope.From[i].ToString() + ";";
}
}
}
else
{
from = "<none>";
}
currentItem.Text = from;
currentItem.SubItems.Add(fetchResp.Envelope.Subject != null ? fetchResp.Envelope.Subject : "<none>");
currentItem.SubItems.Add(fetchResp.InternalDate.Date.ToString("dd.MM.yyyy HH:mm"));
currentItem.SubItems.Add(((decimal)(fetchResp.Rfc822Size.Size / (decimal)1000)).ToString("f2") + " kb");
listView1.Items.Insert(0, currentItem);
}
catch (Exception x)
{
MessageBox.Show("Error: " + x.ToString(), "Error:", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}));
}
}
catch (Exception x)
{
MessageBox.Show("Error: " + x.ToString(), "Error:", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
public static void m_pConnect_Click()
{
string m_pUserName = "user@hotmail.com";
string m_pPassword = "pass";
string m_pServer = "imap-mail.outlook.com";
string debug = "";
IMAP_Client imap = new IMAP_Client();
try
{
imap.Logger = new Logger();
//imap.Logger.WriteLog += m_pLogCallback;
imap.Connect(m_pServer, 993, true);
imap.Login(m_pUserName, m_pPassword);
m_pIMAP = imap;
debug = imap.GreetingText;
//MessageBox.Show(imap.AuthenticatedUserIdentity.Name);
}
catch (Exception x)
{
debug = x.Message;
//MessageBox.Show(this, "IMAP server returned: " + x.Message + " !", "Error:", MessageBoxButtons.OK, MessageBoxIcon.Error);
imap.Dispose();
}
//MessageBox.Show(debug);
}
public void LoadFolderMessages(string folder)
{
IMAP_Client imap = new IMAP_Client();
imap.Logger = new Logger();
//imap.Logger.WriteLog += m_pLogCallback;
imap.Connect("imap-mail.outlook.com", 993, true);
imap.Login("user@hotmail.com", "pass");
m_pIMAP = imap;
//m_pTabPageMail_MessagesToolbar.Items["refresh"].Enabled = true;
//m_pTabPageMail_Messages.Items.Clear();
//this.Cursor = Cursors.WaitCursor;
try
{
m_pIMAP.SelectFolder(folder);
IMAP_Client_SelectedFolder test = m_pIMAP.SelectedFolder;
int MessagesCount = test.MessagesCount;
string SeqParse = "0:10";
string[] parts = SeqParse.Split(':');
int A = Convert.ToInt32(parts[0]);
int B = Convert.ToInt32(parts[1]);
int C = A;
A = MessagesCount - B;
B = MessagesCount - C;
SeqParse = A.ToString() + ":" + B.ToString();
// Fletching.
m_pIMAP.Fetch(
false,
IMAP_t_SeqSet.Parse(SeqParse),
new IMAP_t_Fetch_i[]{
new IMAP_t_Fetch_i_Envelope(),
new IMAP_t_Fetch_i_Flags(),
new IMAP_t_Fetch_i_InternalDate(),
new IMAP_t_Fetch_i_Rfc822Size(),
new IMAP_t_Fetch_i_Uid()
},
this.m_pImap_Fetch_MessageItems_UntaggedResponse
);
}
catch (Exception x)
{
MessageBox.Show("Error: " + x.ToString());
}
//this.Cursor = Cursors.Default;
}
public void LoadFolders()
{
userControl11.treeView1.Nodes.Clear();
TreeNode nodeMain = new TreeNode("IMAP folders");
nodeMain.Tag = "";
//userControl11.treeView1.Nodes.Add(nodeMain);
userControl11.treeView1.Nodes.Add("test");
userControl11.treeView1.Nodes.Add("test2");
try
{
IMAP_r_u_List[] folders = m_pIMAP.GetFolders(null);
char folderSeparator = m_pIMAP.FolderSeparator;
foreach (IMAP_r_u_List folder in folders)
{
string[] folderPath = folder.FolderName.Split(folderSeparator);
// Conatins sub folders.
if (folderPath.Length > 1)
{
TreeNodeCollection nodes = nodeMain.Nodes;
string currentPath = "";
foreach (string fold in folderPath)
{
if (currentPath.Length > 0)
{
currentPath += "/" + fold;
}
else
{
currentPath = fold;
}
TreeNode node = FindNode(nodes, fold);
if (node == null)
{
node = new TreeNode(fold);
node.Tag = currentPath;
nodes.Add(node);
}
nodes = node.Nodes;
}
}
else
{
TreeNode node = new TreeNode(folder.FolderName);
node.ImageIndex = 0;
node.Tag = folder.FolderName;
nodeMain.Nodes.Add(node);
}
}
}
catch (Exception x)
{
MessageBox.Show(this, "Error:" + x.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
nodeMain.Expand();
}
public static TreeNode FindNode(TreeNodeCollection nodes, string nodeName)
{
if (nodes != null)
{
foreach (TreeNode node in nodes)
{
if (node.Text == nodeName)
{
return node;
}
}
}
return null;
}
}
}
我也有一个用户控件,我想从它的类中调用LoadFolderMessages()方法。 但是如果我像这样进行转换
var test = new Form1();
test.LoadFolderMessages("Inbox");
没有这个技巧,因为Form1()方法将运行并且其中的所有内容。
现在,如果我将方法转换为静态方法,那么在其中包含此命令的方法(this.BeginInvoke ...)将抛出此命令的异常处于静态方法中。
所以我的问题是:如何从另一个类调用LoadFolderMessages()方法?