我正在为公司开发网站,他们需要从邮件收件箱中提取 from_address,subject,some contents (mob_no,place等)等邮件内容并显示在网站上(网格视图)。我试图用Pop3连接到 gmail ,代码如下
我使用的代码是:
Imports OpenPop.Pop3
Imports OpenPop.Mime
Protected Sub Read_Emails(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim pop3Client As Pop3Client
If (Session("Pop3Client") Is Nothing) Then
pop3Client = New Pop3Client
pop3Client.Connect("pop.gmail.com", 110, False)
pop3Client.Authenticate("mymail@gmail.com", "trustme")
Session("Pop3Client") = pop3Client
Else
pop3Client = CType(Session("Pop3Client"), Pop3Client)
MsgBox(pop3Client)
End If
Dim count As Integer = pop3Client.GetMessageCount
Dim dtMessages As DataTable = New DataTable
dtMessages.Columns.Add("MessageNumber")
dtMessages.Columns.Add("From")
dtMessages.Columns.Add("Subject")
dtMessages.Columns.Add("DateSent")
Dim counter As Integer = 0
Dim i As Integer = count
Do While (i >= 1)
Dim message As Message = pop3Client.GetMessage(i)
dtMessages.Rows.Add()
dtMessages.Rows((dtMessages.Rows.Count - 1))("MessageNumber") = i
dtMessages.Rows((dtMessages.Rows.Count - 1))("From") = message.Headers.From.Address
dtMessages.Rows((dtMessages.Rows.Count - 1))("Subject") = message.Headers.Subject
dtMessages.Rows((dtMessages.Rows.Count - 1))("DateSent") = message.Headers.DateSent
counter = counter + 1
i = i - 1
If counter = 5 Then
Exit Do
End If
Loop
gvEmails.DataSource = dtMessages
gvEmails.DataBind()
End Sub
但问题是:它会导致错误“找不到服务器”
通过引用https://stackoverflow.com/users/1750602/khaksar-weqar 回答我与gmail收件箱建立了联系,使用上述代码提取了一些内容。现在我想将正文内容作为文本提取到我的asp文本框,这是他们的任何可能吗?积极的回应表示赞赏。
答案 0 :(得分:2)
只需更改代码的下方部分:
pop3Client.Connect("pop.gmail.com", 110, False)
pop3Client.Authenticate("mymail@gmail.com", "trustme")
进入:
pop3Client.Connect("pop.gmail.com", 995, True)
pop3Client.Authenticate("mymail@gmail.com", "trustme", AuthenticationMethod.UsernameAndPassword)