Unicode C
Unicode C
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 Unicode C Downloads
#include <C_CkMailManW.h>
#include <C_CkEmailW.h>
#include <C_CkStringBuilderW.h>
void ChilkatSample(void)
{
BOOL success;
HCkMailManW mailman;
HCkEmailW email;
HCkStringBuilderW sbCss;
BOOL bCrlf;
const wchar_t *filenameInHtml;
const wchar_t *contentIdCss;
HCkStringBuilderW sbHtml;
int numReplacements;
success = FALSE;
// 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.
mailman = CkMailManW_Create();
// Use your SMTP server hostname. This example uses office365, but it could be any SMTP server..
CkMailManW_putSmtpHost(mailman,L"outlook.office365.com");
CkMailManW_putSmtpPort(mailman,587);
CkMailManW_putStartTLS(mailman,TRUE);
// Set the SMTP login/password
CkMailManW_putSmtpUsername(mailman,L"OFFICE365-SMTP-LOGIN");
CkMailManW_putSmtpPassword(mailman,L"OFFICE365-SMTP-PASSWORD");
// Create a new email object
email = CkEmailW_Create();
CkEmailW_putSubject(email,L"HTML Email with embedded CSS");
CkEmailW_putFrom(email,L"Chilkat Support <my-office365-user@mydomain.com>");
CkEmailW_AddTo(email,L"Chilkat Support",L"support@chilkatsoft.com");
sbCss = CkStringBuilderW_Create();
bCrlf = TRUE;
CkStringBuilderW_AppendLine(sbCss,L"body {",bCrlf);
CkStringBuilderW_AppendLine(sbCss,L" background-color: powderblue;",bCrlf);
CkStringBuilderW_AppendLine(sbCss,L"}",bCrlf);
CkStringBuilderW_AppendLine(sbCss,L"h1 {",bCrlf);
CkStringBuilderW_AppendLine(sbCss,L" color: blue;",bCrlf);
CkStringBuilderW_AppendLine(sbCss,L"}",bCrlf);
CkStringBuilderW_AppendLine(sbCss,L"p {",bCrlf);
CkStringBuilderW_AppendLine(sbCss,L" color: red;",bCrlf);
CkStringBuilderW_AppendLine(sbCss,L"}",bCrlf);
// It's possible to add a CSS file directly by calling AddRelatedFile.
// This example will add the CSS from a string.
filenameInHtml = L"styles.css";
contentIdCss = CkEmailW_addRelatedString(email,filenameInHtml,CkStringBuilderW_getAsString(sbCss),L"utf-8");
if (CkEmailW_getLastMethodSuccess(email) != TRUE) {
wprintf(L"%s\n",CkEmailW_lastErrorText(email));
CkMailManW_Dispose(mailman);
CkEmailW_Dispose(email);
CkStringBuilderW_Dispose(sbCss);
return;
}
// The src attribute for the image tag is set to the contentIdCss:
sbHtml = CkStringBuilderW_Create();
CkStringBuilderW_AppendLine(sbHtml,L"<!DOCTYPE html>",bCrlf);
CkStringBuilderW_AppendLine(sbHtml,L"<html>",bCrlf);
CkStringBuilderW_AppendLine(sbHtml,L"<head>",bCrlf);
CkStringBuilderW_AppendLine(sbHtml,L" <link rel=\"stylesheet\" href=\"cid:CONTENT_ID_CSS\">",bCrlf);
CkStringBuilderW_AppendLine(sbHtml,L"</head>",bCrlf);
CkStringBuilderW_AppendLine(sbHtml,L"<body>",bCrlf);
CkStringBuilderW_AppendLine(sbHtml,L"",bCrlf);
CkStringBuilderW_AppendLine(sbHtml,L"<h1>This is a heading</h1>",bCrlf);
CkStringBuilderW_AppendLine(sbHtml,L"<p>This is a paragraph.</p>",bCrlf);
CkStringBuilderW_AppendLine(sbHtml,L"",bCrlf);
CkStringBuilderW_AppendLine(sbHtml,L"</body>",bCrlf);
CkStringBuilderW_AppendLine(sbHtml,L"</html>",bCrlf);
numReplacements = CkStringBuilderW_Replace(sbHtml,L"CONTENT_ID_CSS",contentIdCss);
CkEmailW_SetHtmlBody(email,CkStringBuilderW_getAsString(sbHtml));
success = CkMailManW_SendEmail(mailman,email);
if (success != TRUE) {
wprintf(L"%s\n",CkMailManW_lastErrorText(mailman));
}
else {
wprintf(L"Mail Sent!\n");
}
CkMailManW_Dispose(mailman);
CkEmailW_Dispose(email);
CkStringBuilderW_Dispose(sbCss);
CkStringBuilderW_Dispose(sbHtml);
}