お知らせ

電子会議

ライブラリ

パレット

Delphi FAQ検索

Delphi FAQ一覧

サンプル蔵





FDelphi FAQ
16番会議室「玉石混淆みんなで作るSample蔵」に寄せられたサンプル

"MAPISendMail でメール送信"






■概要
 MAPISendMail の使用法の極簡単なサンプルです。

■コンポーネント
 Form1 に、Edit1 〜 Edit5、Memo1、Button1 を適当に配置して、Button1の
OnClick 他を以下の様に設定して下さい。

■注意事項
 ・Windows98SE + Outlook Express 5.5 + Delphi5 Pro で動作確認。
 ・MAPI が使用できない環境、 標準メールソフトが MAPISendMail に未対応
  である場合には当然ですが動作しません。((^^;)
 ・必要に応じて MAPILogOn, MAPILogOff 他を補って下さい。
 ・本サンプルは、ご自身の責任においてお試し下さい。m(_"_)m

■サンプルコード
//=========================================================================
{...略...}
implementation

{$R *.DFM}

uses
  Registry, MAPI;
{*************************************************************************
   型宣言
//***********************************************************************}
type
  TMailData = record
    ToName,     ToAddress,
    CCName,     CCAddress,
    BCCName,    BCCAddress,
    FromName,   FromAddress,
    SubjectStr, DataStr: String;
    fDialog: Boolean;
  end;{TMailData}
{*************************************************************************
   SendMail( MAPISendMail使用 )
//***********************************************************************}
function SendMail( var MailData: TMailData ): Boolean;
const
  DlgFlag: array[Boolean] of Cardinal = ( 0, MAPI_DIALOG );
var
  MAPIMsg: TMapiMessage;
  fSender: TMAPIRecipDesc;
  fRcpnt : array[0..2] of TMAPIRecipDesc;
  WndList: Pointer;

  procedure SetMAPIRecipDesc( var RcpDsc: TMAPIRecipDesc;
                              const RcpClass: Cardinal;
                              const fName, fAddress: String );
  begin
    FillChar( RcpDsc, SizeOf( RcpDsc ), 0 );
    with RcpDsc do begin
      ulRecipClass := RcpClass;
      lpszName     := PChar( fName );
      lpszAddress  := PChar( fAddress );
    end;{with RcpDsc do}
  end;

begin
  Result := False;
  with MAPIMsg, MailData do begin
    if ToAddress = '' then Exit;
    {* MAPIMsg初期化 *}
    FillChar( MAPIMsg, SizeOf( MAPIMsg ), 0 );
    {* 送信者情報 *}
    SetMAPIRecipDesc( fSender, MAPI_ORIG, FromName, FromAddress );
    {* 宛先情報 *}
    SetMAPIRecipDesc( fRcpnt[nRecipCount], MAPI_TO, ToName, ToAddress );
    Inc( nRecipCount );
    if CCAddress <> '' then
      begin
        SetMAPIRecipDesc( fRcpnt[nRecipCount], MAPI_CC,
                                                    CCName, CCAddress );
        Inc( nRecipCount );
      end;{if CCAddress <> '' then}
    if BCCAddress <> '' then
      begin
        SetMAPIRecipDesc( fRcpnt[nRecipCount], MAPI_BCC,
                                                  BCCName, BCCAddress );
        Inc( nRecipCount );
      end;{if BCCAddress <> '' then}
    {* MAPIMsgを設定 *}
    lpszSubject  := PChar( SubjectStr );
    lpszNoteText := PChar( DataStr );
    flFlags      := MAPI_RECEIPT_REQUESTED;
    lpOriginator := @fSender;
    lpRecips     := @fRcpnt[0];
    {* 添付ファイル情報 *}
    nFileCount   := 0;
    lpFiles      := nil;
    {* メールデータを送る *}
    WndList := DisableTaskWindows(0);
    try
      Result := ( SUCCESS_SUCCESS
                    = MAPISendMail( 0, 0, MAPIMsg, DlgFlag[fDialog], 0 ));
    finally
      EnableTaskWindows( WndList );
    end;{try..finally..}
  end;{with MAPIMsg, MailData do}
end;
{*************************************************************************
   Button1 OnClick
//***********************************************************************}
procedure TForm1.Button1Click(Sender: TObject);
var
  MailData: TMailData;
begin
  FillChar( MailData, SizeOf( MailData ), 0 );
  with MailData do begin
    ToName      := Edit1.Text;
    ToAddress   := Edit2.Text;
    CCName      := Edit3.Text;
    CCAddress   := Edit4.Text;
    SubjectStr  := Edit5.Text;
    DataStr     := Memo1.Text;
    fDialog     := True;
  end;{with MailData do}
  if not SendMail( MailData ) then ShowMessage( 'Error!' );
end;
{...略...}
//=========================================================================

                              01/03/12(Mon) 02:50pm  Fermion [KHF03264]

 



Original document by Fermion         氏 ID:(KHF03264)


ここにあるドキュメントは NIFTY SERVEの Delphi Users' Forum の16番会議室「玉石混淆みんなで作るSample蔵」に投稿されたサンプルです。これらのサンプルはボーランド株式会社がサポートする公式のものではありません。また、必ずしも動作が検証されているものではありません。これらのサンプルを使用したことに起因するいかなる損害も投稿者、およびフォーラムスタッフはその責めを負いません。使用者のリスクの範疇でご使用下さい。

Copyright 1996-2002 Delphi Users' Forum