C#.NET
private void ReadMail()
{
string server = "mail.example.jp";
int port = 110;
string userid = "userid";
string pass = "pass";
System.Net.IPAddress address = System.Net.Dns.GetHostByName(server).AddressList[0];
TKMP.Net.IPopLogon logon = new TKMP.Net.BasicPopLogon(userid , pass);
TKMP.Net.PopClient pop = new TKMP.Net.PopClient(logon , address , port);
pop.MessageSend +=new TKMP.Net.MessageSendHandler(pop_MessageSend);
pop.MessageReceive+=new TKMP.Net.MessageReceiveHandler(pop_MessageReceive);
if(!pop.Connect())
{
return ;
}
foreach(TKMP.Net.MailData mail in pop.MailDatas)
{
mail.ReadBody();
}
pop.Close();
}
private void pop_MessageSend(object sender, TKMP.Net.MessageArgs e)
{
System.Diagnostics.Debug.WriteLine(">" + e.Message );
}
private void pop_MessageReceive(object sender, TKMP.Net.MessageArgs e)
{
System.Diagnostics.Debug.WriteLine(e.Message );
}
VB.NET
Private Sub ReadMail()
Dim server As String = "mail.example.jp"
Dim port As Integer = 110
Dim userid As String = "userid"
Dim pass As String = "pass"
Dim address As System.Net.IPAddress = System.Net.Dns.GetHostByName(server).AddressList(0)
Dim logon As New TKMP.Net.BasicPopLogon(userid, pass)
Dim pop As New TKMP.Net.PopClient(logon, address, port)
AddHandler pop.MessageSend, AddressOf pop_MessageSend
AddHandler pop.MessageReceive, AddressOf pop_MessageReceive
If Not pop.Connect() Then
Return
End If
Dim mail As TKMP.Net.MailData
For Each mail In pop.MailDatas
mail.ReadBody()
Next mail
pop.Close()
End Sub
Private Sub pop_MessageSend(ByVal sender As Object, ByVal e As TKMP.Net.MessageArgs)
System.Diagnostics.Debug.WriteLine((">" + e.Message))
End Sub
Private Sub pop_MessageReceive(ByVal sender As Object, ByVal e As TKMP.Net.MessageArgs)
System.Diagnostics.Debug.WriteLine(e.Message)
End Sub
VB.NETのコードはC#.NETを元に「
C#.NET to VB.NET Translator」によって作成したものです
次のサンプルでは、POPサーバーへの接続から切断までの通信内容をデバッグウインドウに表示しています。 実際にはメールの受信時にメール内容の通信が発生しますが、MessageSendイベントにはメール内容は通知されません。 パスワードの送信時にログに平分が出てしまうので、通信ログをファイルへ保存するような場合は注意してください。