Xbase++
Xbase++
Send HTML Email with External CSS as Related Item
See more SMTP Examples
Demonstrates how to compose an HTML email with an external CSS file included as a related item and referenced by CID (Content-ID).Chilkat Xbase++ Downloads
LOCAL nSuccess
LOCAL oMailman
LOCAL oEmail
LOCAL oSbCss
LOCAL nBCrlf
LOCAL cFilenameInHtml
LOCAL cContentIdCss
LOCAL oSbHtml
LOCAL nNumReplacements
nSuccess := 0
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// The mailman object is used for sending and receiving email.
oMailman := CreateObject("Chilkat.MailMan")
// Use your SMTP server hostname. This example uses office365, but it could be any SMTP server..
oMailman:SmtpHost := "outlook.office365.com"
oMailman:SmtpPort := 587
oMailman:StartTLS := 1
// Set the SMTP login/password
oMailman:SmtpUsername := "OFFICE365-SMTP-LOGIN"
oMailman:SmtpPassword := "OFFICE365-SMTP-PASSWORD"
// Create a new email object
oEmail := CreateObject("Chilkat.Email")
oEmail:Subject := "HTML Email with embedded CSS"
oEmail:From := "Chilkat Support <my-office365-user@mydomain.com>"
oEmail:AddTo("Chilkat Support", "support@chilkatsoft.com")
oSbCss := CreateObject("Chilkat.StringBuilder")
nBCrlf := 1
oSbCss:AppendLine("body {", nBCrlf)
oSbCss:AppendLine(" background-color: powderblue;", nBCrlf)
oSbCss:AppendLine("}", nBCrlf)
oSbCss:AppendLine("h1 {", nBCrlf)
oSbCss:AppendLine(" color: blue;", nBCrlf)
oSbCss:AppendLine("}", nBCrlf)
oSbCss:AppendLine("p {", nBCrlf)
oSbCss:AppendLine(" color: red;", nBCrlf)
oSbCss:AppendLine("}", nBCrlf)
// It's possible to add a CSS file directly by calling AddRelatedFile.
// This example will add the CSS from a string.
cFilenameInHtml := "styles.css"
cContentIdCss := oEmail:AddRelatedString(cFilenameInHtml, oSbCss:GetAsString(), "utf-8")
IF (oEmail:LastMethodSuccess != 1)
? oEmail:LastErrorText
oMailman:destroy()
oEmail:destroy()
oSbCss:destroy()
RETURN
ENDIF
// The src attribute for the image tag is set to the contentIdCss:
oSbHtml := CreateObject("Chilkat.StringBuilder")
oSbHtml:AppendLine("<!DOCTYPE html>", nBCrlf)
oSbHtml:AppendLine("<html>", nBCrlf)
oSbHtml:AppendLine("<head>", nBCrlf)
oSbHtml:AppendLine(' <link rel="stylesheet" href="cid:CONTENT_ID_CSS">', nBCrlf)
oSbHtml:AppendLine("</head>", nBCrlf)
oSbHtml:AppendLine("<body>", nBCrlf)
oSbHtml:AppendLine("", nBCrlf)
oSbHtml:AppendLine("<h1>This is a heading</h1>", nBCrlf)
oSbHtml:AppendLine("<p>This is a paragraph.</p>", nBCrlf)
oSbHtml:AppendLine("", nBCrlf)
oSbHtml:AppendLine("</body>", nBCrlf)
oSbHtml:AppendLine("</html>", nBCrlf)
nNumReplacements := oSbHtml:Replace("CONTENT_ID_CSS", cContentIdCss)
oEmail:SetHtmlBody(oSbHtml:GetAsString())
nSuccess := oMailman:SendEmail(oEmail)
IF (nSuccess != 1)
? oMailman:LastErrorText
ELSE
? "Mail Sent!"
ENDIF
oMailman:destroy()
oEmail:destroy()
oSbCss:destroy()
oSbHtml:destroy()