Unicode C
Unicode C
Send Email With Attachments
See more SMTP Examples
Demonstrates how to send an email with attachments.Chilkat Unicode C Downloads
#include <C_CkMailManW.h>
#include <C_CkEmailW.h>
void ChilkatSample(void)
{
BOOL success;
HCkMailManW mailman;
HCkEmailW email;
const wchar_t *contentType;
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 (SMTP) and receiving (POP3) email.
mailman = CkMailManW_Create();
// Set the SMTP server.
CkMailManW_putSmtpHost(mailman,L"smtp.my-tls-mail-server.com");
// Set the SMTP login/password (if required)
CkMailManW_putSmtpUsername(mailman,L"MY_SMTP_USERNAME");
CkMailManW_putSmtpPassword(mailman,L"MY_SMTP_PASSWORD");
// Connect to SMTP port 465 using TLS.
CkMailManW_putSmtpSsl(mailman,TRUE);
CkMailManW_putSmtpPort(mailman,465);
// Create a new email object
email = CkEmailW_Create();
CkEmailW_putSubject(email,L"This is a test");
CkEmailW_putBody(email,L"This is a test");
CkEmailW_putFrom(email,L"Chilkat Support <support@chilkatsoft.com>");
success = CkEmailW_AddTo(email,L"Chilkat Admin",L"admin@chilkatsoft.com");
// To add more recipients, call AddTo, AddCC, or AddBcc once per recipient.
// Add some attachments.
// The AddFileAttachment method returns the value of the content-type it chose for the attachment.
contentType = CkEmailW_addFileAttachment(email,L"qa_data/jpg/starfish.jpg");
if (CkEmailW_getLastMethodSuccess(email) != TRUE) {
wprintf(L"%s\n",CkEmailW_lastErrorText(email));
CkMailManW_Dispose(mailman);
CkEmailW_Dispose(email);
return;
}
contentType = CkEmailW_addFileAttachment(email,L"qa_data/pdf/fishing.pdf");
if (CkEmailW_getLastMethodSuccess(email) != TRUE) {
wprintf(L"%s\n",CkEmailW_lastErrorText(email));
CkMailManW_Dispose(mailman);
CkEmailW_Dispose(email);
return;
}
// Call SendEmail to connect to the SMTP server and send.
// The connection (i.e. session) to the SMTP server remains
// open so that subsequent SendEmail calls may use the
// same connection.
success = CkMailManW_SendEmail(mailman,email);
if (success != TRUE) {
wprintf(L"%s\n",CkMailManW_lastErrorText(mailman));
CkMailManW_Dispose(mailman);
CkEmailW_Dispose(email);
return;
}
success = CkMailManW_CloseSmtpConnection(mailman);
if (success != TRUE) {
wprintf(L"Connection to SMTP server not closed cleanly.\n");
}
wprintf(L"Mail with attachments sent!\n");
CkMailManW_Dispose(mailman);
CkEmailW_Dispose(email);
}