SHOEISHA iD

※旧SEメンバーシップ会員の方は、同じ登録情報(メールアドレス&パスワード)でログインいただけます

CodeZine編集部では、現場で活躍するデベロッパーをスターにするためのカンファレンス「Developers Summit」や、エンジニアの生きざまをブーストするためのイベント「Developers Boost」など、さまざまなカンファレンスを企画・運営しています。

現役エンジニア直伝! 「現場」で使えるコンポーネント活用術(Secure iNetSuite)

Secure iNetSuite for .NET 4.0Jを最新環境で使ってみる

Windows 8.1+Visual Studio 2013+Secure Mail for.NET 4.0Jの組み合わせを確認

  • このエントリーをはてなブックマークに追加

メール受信ロジックの記述

 最後に、popコンポーネントを使ったメール受信一覧の取得ロジックを記述しましょう。

リスト4 MailModel
Imports System.ComponentModel
Imports System.Collections.ObjectModel
Imports System.Runtime.CompilerServices
Imports System.Threading

Public Class MailModel
    Implements INotifyPropertyChanged

    Public Class TMail
        Public Property From As String
        Public Property Subject As String
        Public Property SendDate As DateTime
    End Class

    Public Property Items As New ObservableCollection(Of TMail)

    Public Property Pop3Server As String = "popServer"
    Public Property UserId As String = "UserID"
    Public Property Password As String = "Password"

    Public Async Function GetMail() As Task
        Using pop As New Dart.Mail.Pop
            AddHandler pop.Progress, AddressOf PopProgress

            pop.Session.RemoteEndPoint = New Dart.Mail.IPEndPoint(Me.Pop3Server, 110)
            pop.Session.Username = Me.UserId
            pop.Session.Password = Me.Password
            pop.Connect()
            pop.Authenticate(True, True)
            ''ヘッダを取得します
            Await Task.Run(Sub()
                               Try
                                   For Each mes In pop.Messages
                                       mes.Get(0)
                                   Next

                               Catch ex As Exception
                                   Debug.Assert(ex.Message)
                               End Try
                           End Sub)
            pop.Close()
            RemoveHandler pop.Progress, AddressOf PopProgress
        End Using
    End Function

    Private Sub PopProgress(sender As Object, e As Dart.Mail.PopProgressEventArgs)
        If e.Final Then
            Me.Items.Add(New TMail With {
                             .From = e.Message.Message.From,
                             .Subject = e.Message.Message.Subject,
                             .SendDate = e.Message.Message.Date
                         })
        End If
    End Sub

    Public Event PropertyChanged(sender As Object, e As PropertyChangedEventArgs) _
        Implements INotifyPropertyChanged.PropertyChanged
    Private Sub OnPropertyChanged(<CallerMemberName> Optional propertyName As String = Nothing)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
    End Sub
End Class

 PopServerプロパティ、UserIdプロパティ、Passwordプロパティは使用しているメールサーバーの設定に合わせて値を設定しておいてください。

 このクラスで最重要なのはGetMailメソッドの中の動きなので、メソッドの中を少し詳しく見ていきたいと思います。

pop.Session.RemoteEndPoint = New Dart.Mail.IPEndPoint(Me.Pop3Server, 110)

 POP3サーバーのアドレスとポート番号を指定して、接続先をRemoteEndPointメソッドに設定します。

pop.Session.Username = Me.UserId / pop.Session.Password = Me.Password

 POP3接続用のユーザIDとパスワードを指定します。

pop.Connect()

 POP3サーバーに接続します。

pop.Authenticate(True, True)

 認証を行い、POP3サーバーにログインできたら、pop.Messagesにメールの情報を取得します。

Await Task.Run(Sub()…)

 Task.Runメソッドを使って、その中で指定しているコード(For Eachループ)を非同期実行しています。また、Awaitをつけてその非同期実行を待ち合せしているので、For Eachループがすべて回りきった後に次の行のpop.Close()やRemoveHandlerが実行されます。

 MainForm.vbにあるet_Button_Clickイベントプロシージャは、この非同期待ち合わせが完了すると処理が終わることになります。

For Each… mes.Get(0)…

 Task.Runの中で指定しているのは、pop.Messagesに格納されたメール情報を元に1通ずつ、Get(0)メソッドを実行して本文なし(0指定のため)でヘッダだけを受信しています。この受信のタイミングでProgressイベントが随時発生します。

 PopProgressイベントプロシージャの中では、取得できたヘッダ情報から、From、Subject、DateをMe.Itemsに追加しています。

実行結果の確認

 それでは、実行結果を確認してみましょう。

図4 実行結果の確認
図4 実行結果の確認

 日本語のFromやSubjectの文字化けもなく、一覧が取得できました。

次のページ
WPFアプリへの適用例

この記事は参考になりましたか?

  • このエントリーをはてなブックマークに追加
この記事の著者

初音玲(ハツネアキラ)

 国内SIerのSEでパッケージ製品開発を主に行っており、最近は、空間認識や音声認識などを応用した製品を手掛けています。 個人的には、仕事の内容をさらに拡張したHoloLensなどのMRを中心に活動しています。 Microsoft MVP for Windows Development ブログ:http://hatsune.hatenablog.jp/

※プロフィールは、執筆時点、または直近の記事の寄稿時点での内容です

この記事は参考になりましたか?

この記事をシェア

  • このエントリーをはてなブックマークに追加
CodeZine(コードジン)
https://codezine.jp/article/detail/7460 2016/03/14 11:42

おすすめ

アクセスランキング

アクセスランキング

イベント

CodeZine編集部では、現場で活躍するデベロッパーをスターにするためのカンファレンス「Developers Summit」や、エンジニアの生きざまをブーストするためのイベント「Developers Boost」など、さまざまなカンファレンスを企画・運営しています。

新規会員登録無料のご案内

  • ・全ての過去記事が閲覧できます
  • ・会員限定メルマガを受信できます

メールバックナンバー

アクセスランキング

アクセスランキング