C++寄信

本文我要記錄一下在C++中,使用CHILKAT所提供的函式來寄送電子郵件。
首先必須先到官網下載C/C++ Libraries,並且解壓縮下載的檔案,可以看到include、libs兩個資料夾,再來就是開啟我們建立好的Win32主控台專案,在本範例當中使用的Visual Studio 2010來操作。
為了要正常的使用人家提供的API我們先要把include加進來,點選專案屬性(下圖),選擇C/C++ -> 其他Include目錄,加入剛剛下載下來的include資料夾。


再來點選專案屬性,選擇連結器 -> 其他相依性,加入以下語法,其中ChikatRelDll.lib為剛剛下載下來libs資料夾內的dll檔。
crypt32.lib
ws2_32.lib
D:\尚未加入SourceSafe\C 語言\C\ChilkatVC10\libs\ChilkatRelDll.lib


再來就可以正常使用了,如下範例,更多使用方式可參閱官方網站

//  The mailman object is used for sending and receiving email.
CkMailMan mailman;

//  Any string argument automatically begins the 30-day trial.
bool success;
success = mailman.UnlockComponent("30-day trial");
if (success != true) {
 printf("%s\n",mailman.lastErrorText());
 return;
}

//  Set the SMTP server.
mailman.put_SmtpHost("smtp.com.tw");

//  Set the SMTP login/password (if required)
mailman.put_SmtpUsername("username");
mailman.put_SmtpPassword("password");


//  Create a new email object
CkEmail email;

email.put_Subject("This is a test");
email.put_Body("This is a test");
email.put_From("xxxxxx");
email.AddTo("Chilkat Admin","lawrence8358@hotmail.com");
//  To add more recipients, call AddTo, AddCC, or AddBcc once per recipient.

//  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 = mailman.SendEmail(email);
if (success != true) {
 printf("%s\n",mailman.lastErrorText());
 return;
}

//  Some SMTP servers do not actually send the email until
//  the connection is closed.  In these cases, it is necessary to
//  call CloseSmtpConnection for the mail to be  sent.
//  Most SMTP servers send the email immediately, and it is
//  not required to close the connection.  We'll close it here
//  for the example:
success = mailman.CloseSmtpConnection();
if (success != true) {
 printf("Connection to SMTP server not closed cleanly.\n");
}

printf("Mail Sent!\n");
附件是網路上一個人的ppt檔,裡面有一些設定的說明,雖然是RSA加密的,不過也可以參考一下。

本文附件 : 
RSA Library.pptx

留言