LoginSignup
10
12

More than 5 years have passed since last update.

c++ builder XE4 > smtp.gmail.comでの送信 | 10.2 Tokyoの場合

Last updated at Posted at 2015-05-14
動作確認
C++ Builder XE4

関連情報

関連情報は以下。

Also keep in mind that setting the UseTLS property may change the Port property, so it is best to set UseTLS before setting Port rather than the other way around.

google側の設定変更

安全性の低いアプリのアクセスをONにしないと失敗する。
google設定の変更

本文の文字化け (C++ Builder)

メール本文の文字化けについては以下が参考になる。
C++Builder XEのIndy10のメール送信の動作がおかしい(Delphi XEのIndy10は問題ない)

下記のコードを追加して、プロジェクトオプションの「実行時パッケージを使って構築」をチェックを外すと、問題が発生しなくなります。

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

サンプルコード (XE4で動作確認済み)

#include <Idglobal.hpp>
#include <IdSMTP.hpp>
#include <IdSSLOpenSSL.hpp>
#include <IdHeaderCoder2022JP.hpp>
#pragma link "IdHeaderCoder2022JP"
...
void __fastcall TForm1::IdMessage1InitializeISO(System::WideChar &VHeaderEncoding,
          UnicodeString &VCharSet)
{
    VHeaderEncoding = L'B';
    VCharSet = "ISO-2022-JP";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{

    TIdSMTP* smtp = new TIdSMTP(NULL);
    TIdSSLIOHandlerSocketOpenSSL * sslHandler = new TIdSSLIOHandlerSocketOpenSSL(NULL);

    smtp->Host = L"smtp.gmail.com";
//  sslHandler->Host = smtp->Host;
//  sslHandler->Port = smtp->Port;
//  sslHandler->Destination = sslHandler->Host + L":"
//      + IntToStr(sslHandler->Port);
    smtp->IOHandler = sslHandler;
    smtp->Username = kMyUserName;
    smtp->Password = kMyPassword;
    smtp->Port = 587;
    smtp->UseTLS = utUseExplicitTLS;

    try {
        smtp->Connect();
    } catch (const Exception &e) {
        String msg = e.Message;
        int nop=1;
    }
    int nop38=1;

    TIdMessage* msg = new TIdMessage(NULL);
    msg->OnInitializeISO = IdMessage1InitializeISO;
    msg->ContentType = "text/plain";
    msg->CharSet = "ISO-2022-JP";
    msg->ContentTransferEncoding = "BASE64";
    msg->From->Name = kFromName;
    msg->From->Address = kFromAddr;
    msg->Recipients->EMailAddresses = kRecipients;
    msg->Subject = "テストメール";
    msg->Body->Text = "テストメールの本文です";

    try {
        smtp->Send(msg);
    } catch (const Exception &e) {
        String msg = e.Message;
        int nop69=1;
    }
    int nop71=1;

    smtp->Disconnect();
    delete msg;
    delete smtp;

    int nop=1;
}

kMyUserName, kMyPassword, kSendTo, kFromName, kFromAddr, kRecipientsはString型でそれぞれ定義しておいた。

sslHandlerのHost, Port, DestinationはSOの以下を見ると、コメントにしていてもよさそう。実際にコメント状態でメール送信がされることは確認済み。

Also, You do not need to set the IOHandler's Host, Port, or Destination properties. Connect() will handle that for you.

10.2 Tokyo対応

(追記 2018/01/10)

10.2 Tokyoの場合は以下の方法で送信できた。

10
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
12