| |
JMail + POP3 เขียน Code สำหรับรับอีเมล์
Code JMail ตัวนี้เป็น code สำหรับการรับอีเมล์อืม
ฟังแล้วอาจจะไม่คุ้น คือเป็น Code สำหรับปูแนวทางไปสู่การเขียน
Webbase E-Mail นั้นเองโดย JMail นั้นสนับสนุนการดึงอีเมล์จาก
Mail Server ได้น่าสนใจทีเดียวแต่คงมีน้อยที่นักที่จะสนับสนุน
JMail ตัวนี้เพราะโดยปกติ JMail
ที่รองรับในแต่ละที่ก็ใช้สำหรับส่งอย่างเดียวไม่มีรับ
ก็ลองศึกษาไว้ดูเพื่อจะนำไปเขียน Webbase E-Mail ไว้ใช้เองนะ
<% @LANGUAGE=VBSCRIPT %>
<% Set pop3 = Server.CreateObject(
"JMail.POP3" )
pop3.Connect "username", "password",
"mail.mydomain.com"
Response.Write( "You have " &
pop3.count & " emails in your mailbox!<br><br>" )
if pop3.count
> 0 then Set msg = pop3.Messages.item(1)
' Note Array แรกเริ่มต้นที่ 1
นะไม่ใช่ 0 โดย POP3 Server เริ่มต้นที่ 1 ReTo = ""
ReCC = ""
Set Recipients = msg.Recipients
separator = ", "
'
ทำการดึงทุกอีเมล์ที่ส่งถึงเรา และ CC
มาถึงเราเก็บไว้ในตัวแปร
For i = 0 To
Recipients.Count - 1 If i = Recipients.Count - 1 Then
separator = "" End If
Set re =
Recipients.item(i) If re.ReType = 0 Then ReTo = ReTo
& re.Name & " (" & re.EMail & ")" &
separator else ReCC = ReTo & re.Name &
" (" & re.EMail & ")" & separator End If
Next
' ต่อมาเป็น Function
สำหรับดึงส่วนของอีเมล์ที่มีการ Attach File มา
Function getAttachments() Set Attachments =
msg.Attachments separator = ", "
For i = 0 To
Attachments.Count - 1 If i = Attachments.Count - 1 Then
separator = "" End If
Set at = Attachments(i)
at.SaveToFile( "c:\attachments\" & at.Name )
getAttachments = getAttachments & "<a href=""/attachments/" &_ at.Name
&""">" & at.Name & "(" & at.Size
& " bytes)" &_ "</a>" & separator Next End
Function
%> <html> <body> <TABLE> <tr> <td>Subject</td> <td><%=
msg.Subject %></td>
<!-- แสดงหัวเรื่องอีเมล์ -->
</tr> <tr> <td>From</td> <td><%=
msg.FromName %></td>
<!-- แสดงว่าอีเมล์จากใคร -->
</tr> <tr> <td>Recipients To</td> <td><%= ReTO
%></td> <!-- อีเมล์ถึงใคร --> </tr> <tr> <td>Recipients CC</td> <td><%= ReCC
%></td> <!-- อีเมล์ CC ถึงใคร --> </tr> <tr> <td>Attachments</td> <td><%=
getAttachments %></td>
<!-- File ที่แทบมาด้วย -->
</tr> <tr> <td>Body</td> <td><pre><%= msg.Body
%></pre></td> <!--
แสดงข้อความในอีเมล์ --> </tr> </TABLE> </body> </html> <%
end if pop3.Disconnect %>
ขอบคุณ Code จากเว็บ Dimac ที่ให้บริการ Component JMail
นี่เป็นตัวอย่าง Code จากผู้ให้บริการ Component
ตัวนี้ท่านจึงมั่นใจว่าใช้งานได้แน่นอน แต่อย่างที่บอก Web
Hosting ของท่านต้องรองรับด้วยนะครับ
|