C#.NET
private System.Windows.Forms.TextBox logText;
public void ShowMailSubject()
{
string server = "mail.example.jp";
int port = 110;
string userid = "userid";
string pass = "pass";
TKMP.Net.IPopLogon logon = new TKMP.Net.BasicPopLogon(userid, pass);
TKMP.Net.PopClient pop = new TKMP.Net.PopClient(logon, server, port);
pop.MessageLog.MessageNew += new EventHandler(MessageLog_MessageNew);
if (!pop.Connect())
{
System.Windows.Forms.MessageBox.Show("接続失敗");
return;
}
foreach (TKMP.Net.MailData m in pop.MailDatas)
{
m.ReadBodyAnsync ();
}
int MailNumber = pop.MailDatas.Length - 1;
TKMP.Net.MailData Mail = pop.MailDatas[MailNumber];
if (!Mail.ReadHeader())
{
System.Windows.Forms.MessageBox.Show("データ受信に失敗しました");
}
else
{
System.IO.Stream Header = Mail.HeaderStream;
TKMP.Reader.MailReader reader = new TKMP.Reader.MailReader(Header, true);
string subject = reader.HeaderCollection["Subject"];
System.Windows.Forms.MessageBox.Show("メールの件名は「" + subject + "」です");
}
pop.CloseAnsync();
}
private void MessageLog_MessageNew(object sender, EventArgs e)
{
if (this.InvokeRequired)
{
this.Invoke(new EventHandler(MessageLog_MessageNew) , new object[] { sender , e } );
return;
}
TKMP.Net.MessageLog MessageLog = (TKMP.Net.MessageLog)sender;
while (!MessageLog.Eof())
{
TKMP.Net.MessageArgs args = MessageLog.Dequeue();
logText.AppendText(args.Message);
}
}
VB.NET
Private logText As System.Windows.Forms.TextBox
Public Sub ShowMailSubject()
Dim server As String = "mail.example.jp"
Dim port As Integer = 110
Dim userid As String = "userid"
Dim pass As String = "pass"
Dim logon As New TKMP.Net.BasicPopLogon(userid, pass)
Dim pop As New TKMP.Net.PopClient(logon, server, port)
AddHandler pop.MessageLog.MessageNew, AddressOf MessageLog_MessageNew
If Not pop.Connect() Then
System.Windows.Forms.MessageBox.Show("接続失敗")
Return
End If
Dim m As TKMP.Net.MailData
For Each m In pop.MailDatas
m.ReadBodyAnsync()
Next m
Dim MailNumber As Integer = pop.MailDatas.Length - 1
Dim Mail As TKMP.Net.MailData = pop.MailDatas(MailNumber)
If Not Mail.ReadHeader() Then
System.Windows.Forms.MessageBox.Show("データ受信に失敗しました")
Else
Dim Header As System.IO.Stream = Mail.HeaderStream
Dim reader As New TKMP.Reader.MailReader(Header, True)
Dim subject As String = reader.HeaderCollection("Subject")
System.Windows.Forms.MessageBox.Show(("メールの件名は「" + subject + "」です"))
End If
pop.CloseAnsync()
End Sub
Private Sub MessageLog_MessageNew(ByVal sender As Object, ByVal e As EventArgs)
If Me.InvokeRequired Then
Me.Invoke(New EventHandler(AddressOf MessageLog_MessageNew), New Object() {sender, e})
Return
End If
Dim MessageLog As TKMP.Net.MessageLog = CType(sender, TKMP.Net.MessageLog)
While Not MessageLog.Eof()
Dim args As TKMP.Net.MessageArgs = MessageLog.Dequeue()
logText.AppendText(args.Message)
End While
End Sub
VB.NETのコードはC#.NETを元に「
C#.NET to VB.NET Translator」によって作成したものです
TKMP.Net.MessageLogクラスはPopClientで行った通信のログを受け取るバッファの役目を果たします。TKMP.Net.MessageLogクラスのMessageNewイベントを利用し、新しい通信ログが追加される毎に、貯まっている全てのログを表示するようにすることで、非同期読み出しや、複数のスレッドで処理した場合も実際の通信と矛盾することなく処理することが可能になります。