Chilkat ActiveX Components

  Chilkat .NET Components

  Chilkat C++ Libraries

Email SMTP .NET Component .NET SMTP / POP3 Email Component

ASP.NET SMTP Send Example Source Code Snippets

The full source code for these examples are provided in the ChilkatDotNet.msi download at http://www.chilkatsoft.com/downloads.asp

ASP.NET SMTP: Send HTML Email with Auto-Zipped Attachments

    '=======================================================================================
    '
    ' Example 2
    '
    ' Send an HTML email with Zipped attachments.
    '
    '=======================================================================================

    ' Create a Chilkat.MailMan object for sending,
    ' and set the necessary properties and unlock the component.
    Dim mailman as New Chilkat.MailMan()
    mailman.UnlockComponent(Request.form("unlockCode"))

    ' Tell the MailMan the SMTP server to be used for sending email.
    mailman.SmtpHost = Request.form("smtpServer")

    ' Optionally provide the SMTP server with a login and password.
    ' The MailMan will automatically choose the most secure SMTP authorization
    ' method available.
    if Request.form("smtpLogin") <> "" then
    	mailman.SmtpUsername = Request.form("smtpLogin")
    	mailman.SmtpPassword = Request.form("smtpPassword")
    end if

    ' Create an Email object.  The mailman will send the email.
    Dim email As New Chilkat.Email()
   
    ' Give our email a subject, recipient, and return address.
    email.From = Request.form("fromAddr")
    email.AddTo("",Request.form("recipient"))
    email.Subject = "Chilkat ASP.NET " + Request.form("exampleSelected")

    ' Create some HTML
    Dim htmlText As String
    htmlText = "<html>" + _
    "<head>" + _
    "<title>Sample HTML Email</title>" + _
    "</head>" + _
    "<body bgcolor=""#FFFFFF"">" + _
    "<h4 align=""center""><img src=""cid:IMAGE_CID""></h4>" + _
    "<h4 align=""center""><span><a href=""dotNetEmail.asp""><font face=""Microsoft Sans Serif"">Email.NET</font></a></span></h4>" + _
    "<h4 align=""center""><font face=""Microsoft Sans Serif""><a href=""http://www.chilkatsoft.com/dotNetZip.asp"">Zip.NET</a> " + _
    "  </font></h4>" + _
    "<h4 align=""center""><font face=""Microsoft Sans Serif""><a href=""http://www.chilkatsoft.com/dotNetCrypt.asp"">Crypt.NET</a></font></h4>" + _
    "<h4 align=""center""><font face=""Microsoft Sans Serif""><a href=""http://www.chilkatsoft.com/dotNetXml.asp"">XML.NET</a></font></h4>" + _
    "<h4 align=""center""><font face=""Microsoft Sans Serif""><a href=""http://www.chilkatsoft.com/dotNetCharset.asp"">Charset.NET</a></font></h4>" + _
    "<h4 align=""center""><font face=""Microsoft Sans Serif""><a href=""http://www.chilkatsoft.com/dotNetSmime.asp"">S/MIME.NET</a></font></h4>" + _
    "<h4 align=""center""><font face=""Microsoft Sans Serif""><a href=""http://www.chilkatsoft.com/dotNetMht.asp"">MHT.NET</a></font></h4>" + _
    "</body>" + _
    "</html>"

    ' Now add the GIF image.  This is not added as an attachment, but as related data because it is
    ' part of the HTML.  
    ' We can use the Crypt object to convert the GIF data from base64 to binary because the base64
    ' encoding/decoding methods do not require unlock codes.
    Dim crypt As New Chilkat.Crypt
    Dim imageCid As String
    imageCid = email.AddRelatedData("chilkat.gif",crypt.DecodeBase64(gifData))

    ' Replace IMAGE_CID in the HTML with the imageCid returned.
    htmlText = htmlText.Replace("IMAGE_CID",imageCid)

    ' Now set the HTML body of the email
    email.SetHtmlBody(htmlText)
    
    ' Create some data for the attachment files.
    dim idx as Integer
    dim aStr as String
    aStr = "A"
    for idx = 1 to 100
    aStr = aStr + str(idx) + " 1234567890" & vbcrlf
    next

    email.AddStringAttachment("text1.txt",aStr)
    email.AddStringAttachment("text2.txt",aStr)

    ' Replace the attachments with a single Zip with the attached files contained within.
    email.ZipAttachments("a.zip")

    ' If you want to examine the guts of the email, save it to an XML file.
    ' But make sure you have directory permissions setup such that you 
    ' can write the XML log.
    'email.SaveXml(Server.MapPath("\chilkatLogs")+"\email.xml")

    ' Connect to the SMTP server and send mail.
    if (mailman.SendEmail(email)) then

    	Response.write("Mail sent!")

    else

    	Response.write("Errors in sending email!")
    	Response.write(mailman.LastErrorHtml)

    end if

 

Copyright 2000-2005 Chilkat Software, Inc. All rights reserved.
Components for Microsoft Windows XP, 2000, NT, 95/98/ME