Top / Programming / C++Builder / C++Builder2010とIndy10でメールを送信する

C++Builder2010とIndy10でメールを送信する

英文メールの送信

最初は簡単に英文のメールを送信する方法を紹介します。

unique_ptr<TIdSMTP> smtp(new TIdSMTP(NULL));
smtp->Host = "localhost"; //SMTPサーバ

unique_ptr<TIdMessage> msg(new TIdMessage(NULL));
//差出人
msg->From->Name = "From name";
msg->From->Address = "from@example.com";

//宛先
TIdEMailAddressItem* to = msg->Recipients->Add();
to->Name = "To name";
to->Address = "to@example.com";

//CC
TIdEMailAddressItem* cc = msg->CCList->Add();
to->Name = "CC name";
to->Address = "cc@example.com";

//件名
msg->Subject = "Subject";

//本文
msg->Body->Text = "This is e-mail.";

//送信
smtp->Connect(); //接続
smtp->Send(msg.get()); //送信
smtp->Disconnect(); //切断

日本語メールの送信

C++Builder2010では日本語メールの送信も簡単になりました。
TIdMessageのContentType、CharSet、ContentTransferEncodingを適切に設定するだけで、後は自動的に変換してくれます。
※ただしWindows2000ではWideCharToMultiByte関数の不具合のため、文字化けします。

波ダッシュ問題を回避するために「MECSUtils」のMecsMappingFix_UnicodeToJISX0208()関数を使用します。

MECSUtilsを使うには、プロジェクトにMECSUtils.pasを追加し、

#include "MECSUtils.hpp"

を追加して下さい。

日本語メールの送信するサンプルコードです。

unique_ptr<TIdSMTP> smtp(new TIdSMTP(NULL));
smtp->Host = "localhost"; //SMTPサーバ

unique_ptr<TIdMessage> msg(new TIdMessage(NULL));
msg->ContentType = "text/plain";
msg->CharSet  = "ISO-2022-JP";
msg->ContentTransferEncoding = "7bit";

//差出人
msg->From->Name = "差出人";
msg->From->Address = "from@example.com";

//宛先
TIdEMailAddressItem* to = msg->Recipients->Add();
to->Name = "宛先";
to->Address = "to@example.com";

//件名
UnicodeString subject = "件名";
msg->Subject = Mecsutils::MecsMappingFix_UnicodeToJISX0208(subject);

//本文
UnicodeString body = "本文";
msg->Body->Text = Mecsutils::MecsMappingFix_UnicodeToJISX0208(body);

smtp->Connect(); //接続
smtp->Send(msg.get()); //送信
smtp->Disconnect(); //切断

添付ファイルのあるメールの送信

添付ファイルがある場合、メールはマルチパートになります。
メールのヘッダと本文の設定が変わります。

unique_ptr<TIdSMTP> smtp(new TIdSMTP(NULL));
smtp->Host = "localhost"; 

unique_ptr<TIdMessage> msg(new TIdMessage(NULL));
msg->ContentType = "multipart/mixed"; //マルチパート
msg->CharSet  = "ISO-2022-JP";
msg->ContentTransferEncoding = "7bit";

msg->From->Name = "差出人";
msg->From->Address = "from@example.com";

TIdEMailAddressItem* to = msg->Recipients->Add();
to->Name = "宛先";
to->Address = "to@example.com";

UnicodeString subject = "件名";
msg->Subject = Mecsutils::MecsMappingFix_UnicodeToJISX0208(subject);

//メールの本文のパート
TIdText* text = new TIdText(msg->MessageParts, NULL);
text->ContentType = "text/plain";
text->CharSet = "ISO-2022-JP";
text->ContentTransfer = "7bit";

//本文
UnicodeString body = "本文";
text->Body->Text = Mecsutils::MecsMappingFix_UnicodeToJISX0208(body);

//添付ファイルのパート
TIdAttachmentFile* attached = new TIdAttachmentFile(
  msg->MessageParts,
  "C:\\test\\サンプル.jpg"); //ファイルのパス
attached->FileName = "サンプル画像.jpg"; //送信ファイル名
attached->ContentType = "image/jpeg"; //ファイルの種類によって変更します

smtp->Connect();
smtp->Send(msg.get());
smtp->Disconnect();

SMTP認証を使用したメールの送信

SMTP認証を使用してメールを送信するには、SSL関連のDLLが必要になります。
次のページからファイルをダウンロードして展開し、「libeay32.dll」と「ssleay32.dll」を実行ファイル(exeファイル)と同じディレクトリに配置します。

ソースコードには、SMTP認証の設定を追加します。

unique_ptr<TIdSMTP> smtp(new TIdSMTP(NULL));
unique_ptr<TIdSASLLogin> login(new TIdSASLLogin(smtp.get()));
unique_ptr<TIdUserPassProvider> provider(new TIdUserPassProvider(login.get()));
unique_ptr<TIdSSLIOHandlerSocketOpenSSL> sslHander(new TIdSSLIOHandlerSocketOpenSSL(NULL));
smtp->Host = "localhost"; //SMTPサーバ
smtp->Port = 465; //ポート番号
smtp->Username = "username"; //SMTP認証のユーザー名
smtp->Password = "passowrd"; //SMTP認証のパスワード
sslHander->Host = smtp->Host;
sslHander->Port = smtp->Port;
sslHander->Destination = sslHander->Host + ":" + IntToStr(sslHander->Port);
smtp->IOHandler = sslHander.get();    //TIdSSLIOHandlerSocketOpenSSL
smtp->UseTLS = utUseExplicitTLS; // Explict

//以下、メールの作成は同じ
unique_ptr<TIdMessage> msg(new TIdMessage(NULL));

Gmailを使ってメールを送信する

Gmailを使ってメールを送信する方法です。
SMTP認証を使います。ポート番号に気をつけて下さい。

unique_ptr<TIdSMTP> smtp(new TIdSMTP(NULL));
unique_ptr<TIdSASLLogin> login(new TIdSASLLogin(smtp.get()));
unique_ptr<TIdUserPassProvider> provider(new TIdUserPassProvider(login.get()));
unique_ptr<TIdSSLIOHandlerSocketOpenSSL> sslHander(new TIdSSLIOHandlerSocketOpenSSL(NULL));
//Gmailの設定
smtp->Host = "smtp.gmail.com";
smtp->Port = 587;
smtp->Username = "username@gmail.com"; //Gmailのユーザ名
smtp->Password = "password";

sslHander->Host = smtp->Host;
sslHander->Port = smtp->Port;
sslHander->Destination = sslHander->Host + ":" + IntToStr(sslHander->Port);
smtp->IOHandler = sslHander.get();    //TIdSSLIOHandlerSocketOpenSSL
smtp->UseTLS = utUseExplicitTLS; // Explict

//以下、メールの作成は同じ
unique_ptr<TIdMessage> msg(new TIdMessage(NULL));

TIdMessageBuilderPlainを使う

TIdMessageBuilderPlainを簡潔にコードを記述できます。

#include <IdMessageBuilder.hpp> //TIdMessageBuilderPlainを使うために必要なヘッダ

TIdMessageBuilderPlain* builder = new TIdMessageBuilderPlain();
builder->PlainTextCharSet = "ISO-2022-JP";
builder->PlainTextContentTransfer = "7bit";
builder->PlainText->Text = L"メールの本文";
builder->Attachments->Add(L"C:\test\test.jpg"); //添付ファイル
TIdMessage* msg = builder->NewMessage(NULL);
delete builder;

msg->CharSet = "ISO-2022-JP";
msg->ContentTransferEncoding = "7bit";
TIdEMailAddressItem* to = msg->Recipients->Add();
to->Name = "宛先";
to->Address = "to@example.com";
msg->From->Address = "from@example.com";
msg->From->Name = "差出人";
msg->Subject = "メールの件名";

C++Builder XEの不具合?

C++Builder XEで、宛先や差出人や件名に日本語を使うと文字が消えてしまいます。
下記のコードを追加して、プロジェクトオプションの「実行時パッケージを使って構築」をチェックを外すと、問題が発生しなくなります。

#include <IdHeaderCoder2022JP.hpp>
#pragma link "IdHeaderCoder2022JP"

最後に

サンプルコードでは「using namespace std;」を省略しています。

ヘッダファイルも省略していますが、Indyのヘッダはヘルプファイルを調べて下さい。
例えば「TIdText」の場合、ヘルプを見ると、

File
  IdText

とありますから、拡張子「.hpp」をつけて

#include "IdText.hpp"

となります。

関連

更新履歴