Sending Email via Gmail Using ASP

How to Send Email from an ASP Page Using a Gmail Account

Posted by Hüseyin Sekmenoğlu on August 12, 2012 Backend Development

You only need a Gmail account and some basic ASP knowledge to send emails through Gmail. This can be done in other languages too but might require adjustments.

Assume your Gmail username is account. Your email address will be [email protected] and the password StrongPass1 (make sure your actual password is secure and not easy to guess).


🔧 Step 1: Setup Configuration in an ASP Page

Create an ASP page with the following definitions and constant values:

Dim iMsg, iConf, Flds
Set iMsg = CreateObject("CDO.Message")
Set iConf = CreateObject("CDO.Configuration")
Set Flds = iConf.Fields

schema = "http://schemas.microsoft.com/cdo/configuration/"

Flds.Item(schema & "sendusing") = 2
Flds.Item(schema & "smtpserver") = "smtp.gmail.com"
Flds.Item(schema & "smtpserverport") = 465
Flds.Item(schema & "smtpauthenticate") = 1
Flds.Item(schema & "sendusername") = "[email protected]"
Flds.Item(schema & "sendpassword") = "StrongPass1"
Flds.Item(schema & "smtpusessl") = 1

Flds.Update
  • This sets up the SMTP server to Gmail with SSL and authentication.

  • Port 465 is used for secure SMTP connections.


📤 Step 2: Sending the Email

Use the following code block to send an email. This example sends a single email but you can wrap it inside a loop to send emails to multiple recipients without being marked as spam.

With iMsg
    .To = "[email protected]"
    .From = "[email protected]"
    .Subject = "Test Email"
    .HTMLBody = "This is the content of a test email..."
    .Sender = "Site Administrator"
    .Organization = "http://mywebsite.com"
    .ReplyTo = "[email protected]"
    Set .Configuration = iConf
    SendEmailGmail = .Send
End With

Set iMsg = Nothing
Set iConf = Nothing
Set Flds = Nothing
  • Line .Send actually sends the email.

  • The previous lines set the email details such as recipient, subject and body.

  • At the end, variables are cleared to free resources.


💡 Optional: Send Bulk Emails

By placing the above sending code inside a loop that changes .To for each recipient, you can send emails to many people automatically.