LoginSignup
0
0

More than 5 years have passed since last update.

c++ builder XE4, 10.2 Tokyo > String > 複数のメールアドレスを"<xxx>,...,<zzz>"の形式にする

Last updated at Posted at 2016-05-17
動作確認
C++ Builder XE4
RAD Studio 10.2 Tokyo Update 2 (追記: 2017/12/27)

メール送信機能において複数のアドレスを指定する場合、そのアドレスを<7of9@xxx.com>,<7of9@yyy.com>,<7of9@zzz.com>のように連結することで複数アドレスへ送信できることは確認した。

ユーザがメールアドレスを入力するTEditが複数ある場合、角括弧をつけてもらうのは不親切なUIなので、自動でつける処理を以下のように実装した。

角括弧をつけて入力された場合もきちんと処理されるよう、角括弧を先に削除してからフォーマット調整している。

Unit1.cpp
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include <memory>
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    String allAdr = Edit1->Text + L"," + Edit2->Text + L"," + Edit3->Text;
    allAdr = FormatMailAddresses(allAdr);
}
//---------------------------------------------------------------------------

String __fastcall TForm1::FormatMailAddresses(String srcStr)
{
    std::unique_ptr<TStringList> mlist(new TStringList);

    mlist->CommaText = srcStr;

    String itmstr;
    String resstr = L"";
    for(int idx = 0; idx < mlist->Count; idx++) {
        itmstr = mlist->Strings[idx];
        // remove angle brackets
        itmstr = StringReplace(itmstr, L"<", L"", TReplaceFlags()<<rfReplaceAll);
        itmstr = StringReplace(itmstr, L">", L"", TReplaceFlags()<<rfReplaceAll);

        if (resstr.Length() > 0) {
            resstr = resstr + L",";
        }
        resstr = resstr + L"<" + itmstr + L">";
    }

    return resstr;
}
0
0
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
0
0