تابع ارسال ایمیل CDO
جهت ارسال ایمیل از طریق کدهای برنامه نویسی ASP.Net می توانید از تابع CDO به شرح زیر استفاده نمایید.
جهت اینکار نیاز به Collaborative Data Object می باشد که از طریق قابلیت NT Pack 4 قابل نصب می باشد. جهت اطمینان از نصب آن می توانید به مسیر زیر رفته:
Start / Control Panel / Add/Remove Programs / NT Option Pack 4
و بررسی نمایید که سرویس SMTP نصب شده باشد.
جهت ایجاد instance مورد نیاز CDO در کدهای ASP می توانید به شرح زیر اقدام کنید:
<%
Dim objCDO
Set objCDO = Server.CreateObject("CDONTS.NewMail")
%>
تابع CDONTS.NewMail قابلیت های بسیار ساده ای جهت این کار در اختیار شما قرار می دهد. بطور مثال به کد های زیر توجه نمایید:
<%
' This code assumes the above CDO
'instantiation code is included
objCDO.To = "yourdestination@destination.com"
objCDO.From = "test@yourdomain.com"
objCDO.cc = "test@destination.com,jobs@apple.com"
Dim txtSubject
txtSubject = "Hello Scott! We were wanting your advice on some programming issues. Please come to Redmond at your earliest convenience for a very fat check!"
objCDO.Subject = "Attn: Scott, we need you!!"
objCDO.Body = txtSubject
objCDO.Send
%>
همچنین با استفاده از CDO امکان ارسال فایل های پیوست، ارسال ایمیل به گروهی از دریافت کنندگان، ارسال ایمیل در زمان های تعریف شده و... (نظیر نرم افزار Outlook) می باشد.
بعنوان مثال دیگر به کدهای زیر توجه نمایید:
<%
objCDO.To = "someone@xyz.com (John Doe)"
objCDO.From = "me@abc.com (Jane Doe)"
objCDO.bcc = "janedoe@aol.com" 'Blind cc
objCDO.Subject = "My Resume, per Request"
objCDO.Body = "Hello John. Here is a copy of my resume"
objCDO.Importance = 2 'High importance!
objCDO.AttachFile "\\server\jane\resume.doc","Resume.doc"
objCDO.Send 'Send off the email!
'Cleanup
Set objCDO = Nothing
%>
همچنین جهت SMTP Authentication می توانید از کدهای مشابه زیر استفاده نمایید:
<%
set objMessage = createobject("cdo.message")
set objConfig = createobject("cdo.configuration")
Set Flds = objConfig.Fields
Flds.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
Flds.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") ="[Mail Server Name]"
' ' Passing SMTP authentication
Flds.Item ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1 'basic (clear-text) authentication
Flds.Item ("http://schemas.microsoft.com/cdo/configuration/sendusername") ="postmaster@yourdomain.com"
Flds.Item ("http://schemas.microsoft.com/cdo/configuration/sendpassword") ="password"
Flds.update
Set objMessage.Configuration = objConfig
objMessage.To = "postmaster@mydoamin.com"
objMessage.From = "postmaster@yourdomain.com"
objMessage.Subject = "New Task"
objMessage.fields.update
objMessage.HTMLBody = "This is a test sent from CDO using smtp authentication."
objMessage.Send
%>
جهت استفاده از basic authentication نیز میتوانید از کدهای مشابه زیر استفاده نمایید:
<%
Const cdoSendUsingPickup = 1 'Send message using the local SMTP service pickup directory.
Const cdoSendUsingPort = 2 'Send the message using the network (SMTP over the network).
Const cdoAnonymous = 0 'Do not authenticate
Const cdoBasic = 1 'basic (clear-text) authentication
Const cdoNTLM = 2 'NTLM
Set objMessage = CreateObject("CDO.Message")
objMessage.Subject = "Example CDO Message"
objMessage.From = """Me"" <me@my.com>"
objMessage.To = "test@paulsadowski.com"
objMessage.TextBody = "This is some sample message text.." & vbCRLF & "It was sent using SMTP authentication."
'==This section provides the configuration information for the remote SMTP server.
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
'Name or IP of Remote SMTP Server
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "mail.your.com"
'Type of authentication, NONE, Basic (Base64 encoded), NTLM
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = cdoBasic
'Your UserID on the SMTP server
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusername") = "youruserid"
'Your password on the SMTP server
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "yourpassword"
'Server port (typically 25)
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
'Use SSL for the connection (False or True)
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = False
'Connection Timeout in seconds (the maximum time CDO will try to establish a connection to the SMTP server)
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
objMessage.Configuration.Fields.Update
'==End remote SMTP server configuration section==
objMessage.Send
%>
جهت ارسال ایمیل با فرمت HTML می توانید مشابه زیر اقدام نمایید:
<%
Set objMessage = CreateObject("CDO.Message")
objMessage.Subject = "Example CDO Message"
objMessage.From = "me@yourdomain.com"
objMessage.To = "test@destination.com"
'The line below shows how to send using HTML included directly in your script
objMessage.HTMLBody = "<h1>This is some sample message html.</h1>"
'The line below shows how to send a webpage from a remote site
'objMessage.CreateMHTMLBody "http://www.paulsadowski.com/wsh/"
'The line below shows how to send a webpage from a file on your machine
'objMessage.CreateMHTMLBody "file://c|/temp/test.htm"
objMessage.Bcc = "you@your.com"
objMessage.Cc = "you2@your.com"
objMessage.Send
%>