delphi调用OUTLOOK发送DBGRID中的数据(注意不是OUTLOOK EXPRESS ),带显示发送邮件界面。

{在网上搜了很久,发现DELPHI7.0调用OUTLOOK都没有提如何显示发送邮件界面,后来发现WINDOWS自带的VBAOUTL9.CHM 里面有详细的说明。

示范代码:

}

unit SendMailForm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, ComCtrls, StdCtrls, Menus,OriFormat, DB, ADODB, jpeg,printers,Mapi,Math,
  OleServer, Excel2000,clipbrd,StrUtils, Grids, DBGrids,ComObj, ActiveX,
  NavatorPanel, MapiControl;

type
  TSendMailForm= class(TForm)

button1:Tbutton;
    DataSource1: TDataSource;
    DBGrid1: Tdbgrid;
    Adoquery1:Tadoquery;
    procedure button1onclick(sender:Tobject);
  private
    { Private declarations }

  public
    { Public declarations }
  
    procedure exportdbgridtoemail(dbgrid: Tdbgrid);
 

  end;

var
  SendMailForm: TSendMailForm;

 

implementation


{$R *.dfm}

procedure TSendMailForm.exportdbgridtoemail(dbgrid: Tdbgrid);
var
  Outlook, NmSpace, Folder: OleVariant;
  miMail: Variant;
  s:string;

  MapiControl1:TMapiControl;

  const
  olMailItem = 0;


  function dbgridtohtmls(dbgrid:tdbgrid):string;
  var s:string;
  i:integer;
  begin

    s:='<html><body>'; //
    s:=s+'<hr>' ;
    s:=s+'<table /border="1"  bordercolor="#000080">'; //
    with dbgrid.DataSource.DataSet do
    begin
        DisableControls;
        first;
      
       
        s:=s+'<tr align="left">';//

        for i:=0 to dbgrid.FieldCount-1 do
        if dbgrid.findcolumn(dbgrid.Fields[i].FieldName).Visible then
        if  ((dbgrid.DataSource.DataSet.Fields[i].DataType<>ftGuid) and (dbgrid.DataSource.DataSet.Fields[i].DataType<>ftBlob)) then
            s:=s+'<td>'+dbgrid.DataSource.DataSet.Fields[i].DisplayLabel+'</td>';
            // nowrap
        s:=s+'</tr>'  ;

        while not eof do
        begin
           s:=s+''+'<tr  align="left">';<br>
           for i:=0 to dbgrid.FieldCount-1 do
           begin
                 if  dbgrid.findcolumn(dbgrid.Fields[i].FieldName).Visible then
                 if  ((dbgrid.DataSource.DataSet.Fields[i].DataType<>ftGuid) and (dbgrid.DataSource.DataSet.Fields[i].DataType<>ftBlob)) then
                 begin
                     if  dbgrid.DataSource.DataSet.Fields[i].IsNull then
                        s:=s+'<td nowrap>&nbsp;</td>'
                     else
                     if  dbgrid.DataSource.DataSet.Fields[i].AsString='' then
                        s:=s+'<td nowrap>&nbsp;</td>'
                     else
                       s:=s+'<td nowrap>'+dbgrid.DataSource.DataSet.Fields[i].asstring+'</td>';
                 end;
           end;

           //exit;
           s:=s+'</tr>';
           next;
        end;

        //s:=s+'<tr><td>'+BottomMemo+'</td></tr>';
        EnableControls;
    end;

    //s:=s+'<tr>xx</tr>';
    s:=s+'</table>';
    s:=s+'</body></html>';
    //

    result:=s;
  end;

begin
  inherited;

 

  //showmessage('begin to create');
  try
     Outlook := CreateOleObject('Outlook.Application');
  except
     on e:exception do
     begin
         alert('请检查您电脑上是否安装了Outlook:'+e.Message);
         exit;
     end;
  end;


  //showmessage('create ok');

  miMail := Outlook.CreateItem(olMailItem);
  //miMail.Recipients.Add(INPUTBOX('请输入邮箱地址!','',''));
  miMail.Subject := ((self as tform).caption) ;

  // use this to send a plain text email (all versions of Outlook)
  //miMail.Body := 'Attached is the list of email addresses.';

  // alternatively send an HTML email (not in Outlook 97)
  //showmessage('make html');
  miMail.HTMLBody :=dbgridtohtmls(dbgrid);
  //showmessage('html maked');
  //'<font color=red>Attached is the <b>list of email</b> addresses.</font>';

  //miMail.Attachments.Add('C:/temp/list.txt', EmptyParam, EmptyParam, EmptyParam);
  //miMail.Send;
  //显示发送邮件的界面。
  miMail.Display;

  //showmessage('发送完毕!');
 
end;

procedure TSendMailForm.Button1Click(Sender: TObject);
begin
 exportdbgridtoemail(dbgrid1)
end;

end.

//希望对大家有所帮助


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值