Sending an e-mail via CDOSys using ASP

Hello, everybody! Today I’m going to talk a little bit about web programming. In this tutorial I’ll teach how to send an e-mail using CDOSys and, of course, ASP.

For you who’s willing to send an e-mail, and think this is such a hard job, forget everything you know about it. CDOSys is one of the easiest ways to send e-mail using ASP.

email

If you’re not familiar with ASP components, first you should take a look at CDOSYS page and get some extra knowledge.

On the other hand, if you have heard about it, but it’s not clear in your mind yet. CDOSys is a Microsoft component that allows you to send e-mails via Windows servers. It’s the updated version of the old CDOnts, which is no longer supported by new servers.

Well, since you’re now familiar with CDOSys, let’s get some code work!

First of all, we must create a form. Let’s call it “form.asp”.

1 <form name="formInsc" method="post" 2 action="send_email.asp"> 3 <div align="center"> 4 <table width="85%" border=1 5 align=center bordercolor="00000"> 6 7 <p><font size="3"><strong> Name.</strong> 8 </font></p> 9 <input type=text name="txtEmail" value="" 10 size=15 maxlength=50 11 style="{<font-family:Arial; font-size:12}"> 12 13 <p><font size="3"><strong> E-mail.</strong> 14 </font></p> 15 <input type=text name="txtEmail" value="" 16 size=15 maxlength=50 17 style="{<font-family:Arial; font-size:12}"> 18 19 <p><font size="3"><strong> Description. 20 </strong></font></p> 21 <textarea rows="4" name="txtDesc" cols="48" 22 style="font-family: Verdana; font-size: 8 pt;COLOR: 000000;"> 23 </textarea> 24 </table> 25 </div> 26 <p>&nbsp;</p> 27 28 <input type=submit name=submit value=Insert 29 style="{<font-family:Arial; font-size:12}"> 30 </form>

After this code, you should get something like this.

form

Okay, we have already set up our form, and our fields. Pay attention on the line “… action="send_email.asp">” and create an ASP page with the same name.

I’m not going to paste the whole code, I went for coding line by line and explaining it.

So, first line should be our variables. Since in our form there’re 3 fields, let’s go for 3 of them.

1 <% 2 Dim strName, strEmail, strDesc

Now, let’s set up our variables with information that came from our form, previously filled by the user.

1 strName = Request.Form("txtName") 2 strEmail = Request.Form("txtEmail") 3 strDesc = Request.Form("txtDesc")

Okay, until now there’s nothing new. From here, we’re going to start working with CDOSys, next code will be the simplest way to send an email, after done, I’ll add some extra, like attatchment, send a web page, etc.

So, let’s start configuing CDOSys.

1 Set Mailer=CreateObject("CDO.Message") 2 Mailer.Subject = "Mail Subject" 3 Mailer.From = "felipe@gmail.com" 4 Mailer.To = "gentil@gmail.com"

Everything set up. We must now build the e-mail body, and you have something like this.

1 Mailer.TextBody = "Name: " & strNome & vbcrlf &_ 2 "Email: " & strData & vbcrlf &_ 3 "Description: " & strEscola & vbcrlf

Everything done, so call the method “Send”.

1 Mailer.Send 2 set Mailer=nothing

If there’s anything wrong, it should work. To finish, let’s redirect the page with the following code.

1 if err = 0 Then 2 'If everything went okay 3 response.redirect "sent_email.asp" 4 end if 5 %>

Creating a new page called “sent_email.asp” just add a message warning the user.

1 <table width="680" border="0" align="center"> 2 <tr> 3 <td align="center" bgcolor="#CCCCCC"> 4 <p class="right">E-mail sent!</p> 5 </tr> 6 </table>

Well, that’s it! Pretty simple, huh? But, I’m not going to finish now, as I said before, there’ll be a few extra lines of code.

If you’d like to send using CC or BCC.

1 Mailer.Cc = "youremail@gmail.com" 2 Mailer.Bcc = "anotheremail@gmail.com"

If you’d like to send a web page.

1 Mailer.CreateMHTMLBody "http://www.YOURWEBPAGE.com"

If you’d like to send a HTML Mail instead of a simple text.

1 Mailer.HTMLBody = "<h1>This is a message.</h1>"

And if you’d like to send an attatchment with your e-mail.

1 Mailer.AddAttachment "c:\attatch.txt"

If any of this helped you, and you still can’t send an e-mail using CDOSys, I’m going to give you the last try, you can manually configure your Mailer.

1 'Manual configuration of CDOSys 2 Set MailerConf = Server.CreateObject ("CDO.Configuration") 3 4 'SMTP Server 5 MailerConf.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "localhost" 6 7 'SMTP Port 8 MailerConf.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport")= 25 9 10 'CDOSys Port 11 MailerConf.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 12 13 'Timeout 14 MailerConf.Fields("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 30 15 MailerConf.Fields.update 16 17 'Update manual configuration 18 Set objCDOSYSMail.Configuration = MailerConf

Well, hope everything works swell now! Now I can say, that’s it!

If you have anything to comment, add or criticize, please use the comment are bellow!

 

PS. I’m not an ASP.NET programmer, so I think it could work with .NET plataform too, although I’m not sure. (That’s why I’m not including this tutorial in .NET category)

Thanks for all the attention. Alegre

Advertisements

4 comments

    1. Hello Jamie,

      Well, to add more recipients to the email all you have to do is:
      myMail.Bcc=”another_person@email.com”
      myMail.Cc=”another_another_person@email.com”

      Hope it helps.
      Regards,
      Felipe

  1. Forgive me, I’m new to mailers. How would I change this code?

    If TempTest = TRUE then
    Else
    Mailer.AddRecipient siteOwner, ContactEmail
    If ContactCC “” then
    Mailer.Cc siteOwner, ContactCC
    End If
    End If
    If DesignerEmail “” then
    Mailer.Bcc DesignerEmail, DesignerEmail
    End If

    1. Hello Jamie

      Are you sure you are using CDOSys? Can you explain to me what do you want to do? Perhaps I might be able to help you better. Are you using ASP or ASP.NET? Have you tried to follow the tutorial above?

      Regards,
      Felipe

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s