Showing posts with label Mail Server. Show all posts
Showing posts with label Mail Server. Show all posts

Sending email using CDOSYS

If you are using a Windows 2000 / 2003 Server, or even XP Pro chances are that CDOSYS is your best bet for sending email from Active Server Pages. That is because CDOSYS is installed on all of them by default. Gone are the days of using CDONTS which was the old way of sending email from ASP. CDOSYS is it's replacement.

That being said there are actually a lot of ways to configure and use CDOSYS. When I 1st started using CDOSYS I assumed the CDOSYS code I was using would work in any situation, but that is not the case. This is something most articles about CDOSYS do not mention so I am going to show you 3 different CDOSYS examples each sending email using a slightly different method.
  1. Method 1 involves sending email using a local pickup directory. Meaning you have the IIS SMTP Virtual Server Running. If you are on a local development machine this is probably for you. Under this scenario any emails you send from your scripts put a ".eml" file in the local pickup directory. Then hopefully the SMTP Virtual Server grabs the file and sends it off. The Virtual SMTP server is however known to hiccup and not send out the emails right away.
  2. Method 2 involves port forwarding. I am not exactly sure how you set that up on the server but the code I wrote for it works under that scenario. The hosting company known as Verio actually implements this with CDOSYS on their servers. I actually implemented this method in some of my software because of a customer that couldn't get emails to send on one of their servers.
  3. Method 3 involves sending the email using a remote mail server. This supports outgoing SMTP authentication should your server require that for outgoing emails. Many do these days. This method is also the best method to use because you are using a real email server with valid MX records. Many modern email systems block emails that do not have valid MX records and you want your emails to reach the recipients.
Method 1 ( Local Pickup Directory where server is running SMTP Virtual Server )

<%
Dim ObjSendMail
Dim iConf
Dim Flds

Set ObjSendMail = Server.CreateObject("CDO.Message")
Set iConf = CreateObject("CDO.Configuration")
Set Flds = iConf.Fields

Flds("http://schemas.microsoft.com/cdo/configuration/sendusing") = 1

'**** Path below may need to be changed if it is not correct
Flds("http://schemas.microsoft.com/cdo/configuration/smtpserverpickupdirectory") = "c:\inetpub\mailroot\pickup"
Flds.Update

Set ObjSendMail.Configuration = iConf
ObjSendMail.To = "someone@someone.net"
ObjSendMail.Subject = "this is the subject"
ObjSendMail.From = "someone@someone.net"

' we are sending a text email.. simply switch the comments around to send an html email instead
'ObjSendMail.HTMLBody = "this is the body"
ObjSendMail.TextBody = "this is the body"

ObjSendMail.Send

Set ObjSendMail = Nothing
%>



Method 2 ( Using mail forwarding on port 25 )
Include this metatype library code on the page you use this emailing with code because there are some things in it this method needs. You can probably get rid of these two lines if you figure out what it references but I didn't take the time to look.





<%
Dim ObjSendMail
Dim iConf
Dim Flds

Set ObjSendMail = Server.CreateObject("CDO.Message")
Set iConf = Server.CreateObject("CDO.Configuration")

Set Flds = iConf.Fields
With Flds
.Item(cdoSendUsingMethod) = 2
.Item(cdoSMTPServer) = "mail-fwd"
.Item(cdoSMTPServerPort) = 25
.Item(cdoSMTPconnectiontimeout) = 10
.Update
End With

Set ObjSendMail.Configuration = iConf

Set ObjSendMail.Configuration = iConf
ObjSendMail.To = "someone@someone.net"
ObjSendMail.Subject = "this is the subject"
ObjSendMail.From = "someone@someone.net"

' we are sending a text email.. simply switch the comments around to send an html email instead
'ObjSendMail.HTMLBody = "this is the body"
ObjSendMail.TextBody = "this is the body"

ObjSendMail.Send

Set ObjSendMail = Nothing
Set iConf = Nothing
Set Flds = Nothing
%>


Method 3 ( Using remote mail server )

<%
Dim ObjSendMail
Set ObjSendMail = CreateObject("CDO.Message")

'This section provides the configuration information for the remote SMTP server.

ObjSendMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 'Send the message using the network (SMTP over the network).
ObjSendMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") ="mail.yoursite.com"
ObjSendMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
ObjSendMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = False 'Use SSL for the connection (True or False)
ObjSendMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60

' If your server requires outgoing authentication uncomment the lines bleow and use a valid email address and password.
'ObjSendMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1 'basic (clear-text) authentication
'ObjSendMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusername") ="somemail@yourserver.com"
'ObjSendMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendpassword") ="yourpassword"

ObjSendMail.Configuration.Fields.Update

'End remote SMTP server configuration section==

ObjSendMail.To = "someone@someone.net"
ObjSendMail.Subject = "this is the subject"
ObjSendMail.From = "someone@someone.net"

' we are sending a text email.. simply switch the comments around to send an html email instead
'ObjSendMail.HTMLBody = "this is the body"
ObjSendMail.TextBody = "this is the body"

ObjSendMail.Send

Set ObjSendMail = Nothing
%>

In addition to what you see here there are plenty of properties you can add to these examples.
Here are a few examples.

Carbon Copy
ObjSendMail.CC = "someone@someone.net"

Blind Carbon Copy
ObjSendMail.BCC = "someone@someone.net"

Send Attachment (we hard code it here, but you could specify the file path using Server.Mappath as well)
ObjSendMail
.AddAttachment "c:\myweb\somefile.jpg"

and a ton of other things you can do...
See Microsoft's CDOSYS Documentation


Mail Server Settings (POP3 & SMTP)

Incoming and Outgoing Mail Server Settings for Hotmail, Yahoo! Mail, GMail, AOL and more

*Update, 29th of August 2006: the Google Gmail service offers a SMTP server (outgoing mail server) for Gmail accounts. To use the Google Gmail SMTP server, use the following information:

Google Gmail Outgoing Mail Server (SMTP): smtp.gmail.com

The Gmail SMTP server requires authentication (use the same settings as for the incoming mail server)

The Google Gmail SMTP Server requires an encrypted connection (SSL) on port 465.

dotsWhy & when do I need these settings?

Hotmail, Yahoo! Mail, GMail and other providers are basically email services designed to provide you with email mailbox accesss directly from the web. However, going online and logging on to their sites is not always the most convenient way for reading and sending emails.

On the other hand, you have the alternative to send and receive emails through such a mailbox by using a local email client software, such as Outlook Express, Microsoft Outlook, Thunderbird, Thunderbird, etc. In order to properly use it, you need to configure your email software with the incoming and outgoing mail servers of your email provider (Hotmail, Gmail, Yahoo! Mail or else).

dotsMail Server Settings

· Hotmail Settings
As other web based email services, Hotmail is using the HTTP protocol for connecting you to your mailbox. If you want to send and receive Hotmail emails using an email client software, then your software must support Hotmail HTTP access for your email account. Some email clients, such as Outlook Express or Microsoft Outlook, offer builtin support for Hotmail accounts, so you only have to select HTTP when you are asked to select your email account type and select Hotmail as the HTTP Mail Service Provider.

· Yahoo! Mail Settings
Unlike Hotmail, Yahoo! Mail offers standard POP3 access for receiving emails incoming through your Yahoo mailbox, by using your favorite email client software. To setup your email client for working with your Yahoo account, you need to select the POP3 protocol and use the following mail server settings:

Yahoo Incoming Mail Server (POP3) - pop.mail.yahoo.com (port 110)

Yahoo Outgoing Mail Server (SMTP) - smtp.mail.yahoo.com (port 25)

· Google GMail Settings
The Google GMail service offers email client access for retrieving and sending emails through your Gmail account. However, for security reasons, GMail uses POP3 over an SSL connection, so make sure your email client supports encrypted SSL connections.

Google Gmail Incoming Mail Server (POP3) - pop.gmail.com (SSL enabled, port 995)

Outgoing Mail Server - use the SMTP mail server address provided by your local ISP or smtp.gmail.com (SSL enabled, port 465)

· Lycos Mail Settings
The Lycos Mail Plus service allows you to use POP3 and SMTP servers for accessing your Lycos mailbox.

Lycos Mail Incoming Mail Server (POP3) - pop.mail.lycos.com (port 110)

Outgoing Mail Server - smtp.mail.lycos.com or use your local ISP SMTP mail server

· AOL Mail Settings
The AOL email service is a web based system, designed for managing your AOL mailbox via HTTP IMAP access. Unlike Hotmail, you can use any email client to access your AOL mailbox, as long as it supports the IMAP protocol.

AOL Incoming Mail Server (IMAP) - imap.aol.com (port 143)

AOL Outgoing Mail Server - smtp.aol.com or use your local ISP SMTP mail server

· Mail.com Mail Settings
The Mail.com email service allows you to use POP3 and SMTP servers for accessing your Mail.com mailbox.

Mail.com Mail Incoming Mail Server (POP3) - pop1.mail.com (port 110)

Outgoing Mail Server - use your local ISP SMTP mail server

· Netscape Internet Service Mail Settings
The Netscape e-mail system is web-based, which means you can access their e-mail from any Internet connection. Netscape Internet Service also supports AOL® Communicator, Microsoft® Outlook, Microsoft® Outlook Express, and other POP3 e-mail software. The outgoing mail server needs SSL support, so make sure your email client software supports SSL connections over the SMTP protocol.

Netscape Internet Service Incoming Mail Server (POP3) - pop.3.isp.netscape.com (port 110)

Netscape Internet Service Outgoing Mail Server - smtp.isp.netscape.com (port 25, using a secure SSL connection)

· Tiscali Mail Settings
The Tiscali email service allows you to use POP3 and SMTP servers for accessing your Tiscali mailbox.

Tiscali Incoming Mail Server (POP3) - pop.tiscali.com (port 110)

Outgoing Mail Server - use your local ISP SMTP mail server

· Freeserve Mail Settings
The Freeserve email service allows you to use POP3 and SMTP servers for accessing your Freeserve mailbox.

Freeserve Incoming Mail Server (POP3) - pop.freeserve.com (port 110)

Outgoing Mail Server - use your local ISP SMTP mail server

· Supanet Mail Settings
The Supanet email service allows you to use POP3 and SMTP servers for accessing your Supanet mailbox.

Supanet Incoming Mail Server (POP3) - pop.supanet.com (port 110)

Outgoing Mail Server - use your local ISP SMTP mail server

If your email client does not support Hotmail as a Mail Service Provider or if it simply doesn't work with your mail server settings, you can use a 3rd party solution like Hotmail Popper, IzyMail, POP Peeper or Email2Pop. When using such tools, you should define your Hotmail account as a POP3 account and you will need to define your incoming mail server will as 'localhost' (or 127.0.0.1).