发送电子邮件时候如何带附件发送?谢谢

解决方案 »

  1.   

    就这个问题,都还没有解决,你是采用什么方式发送,Indy组件?
    还是直接使用APIfunction TFrmSendEmail.SendEmail(EmailHost:String;MailTitle:String;MailUsername,MailPassword:String;
                       EmailServerPort,EmailAuthType:Integer;EmailUserEmail:String): boolean;
    var
     i,j,k,fgfIndex:Integer;
     AttachmentFileName:String;
     S:AnsiString;
     tmpEMailItem,tmpCCEMailItem : TIdEMailAddressItem;
     AttachmentFileSize:Integer;
    begin
      With IdMessage1 Do
      begin
         Body.Clear;
         Body.Assign(MailBody.Lines);
         {CharSet:='utf-8';//gb2312';
         ContentType := CharSet;
         ContentTransferEncoding := ContentType;}//影响正文     From.Address:=MailUsername; //你要发送邮件的完整地址'
         From.Name   :=MailUsername;//'发件人';
         From.Text:=EmailUserEmail;
         //Recipients.Add.Address:=Trim(RecvieMemo.Lines.Text);
         Recipients.EMailAddresses:=Trim(RecvieMemo.Lines.Text);//'发送者地址';   
         for I := 0 to RecvieMemo.Lines.Count - 1 do    //收件人实现群发
         begin
            S:=S+RecvieMemo.Lines.Strings[i];
         end;
         k:=0;
         j:=Pos(';',S);
         if j>0 then
             tmpEMailItem := IdMessage1.BccList.Add;  //添加一个地址列表
         while j>0 do
         begin
            tmpEMailItem.Address:=Copy(S,0,j-1);
            S:=Copy(S,j+1,MaxInt);
            j:=Pos(';',S);
            Inc(k);
         end;
         if k>0 then
            tmpEMailItem.Address:=S; //最后一个     S:='';
         for I := 0 to ReciveSecMemo.Lines.Count - 1 do    //抄写人实现群发
         begin
            S:=S+ReciveSecMemo.Lines.Strings[i];
         end;
         k:=0;
         j:=Pos(';',S);
         if j>0 then
             tmpCCEMailItem := IdMessage1.CCList.Add;  //添加一个地址列表
         while j>0 do
         begin
            tmpCCEMailItem.Address :=Copy(S,0,j-1);
            S:=Copy(S,j+1,MaxInt);
            j:=Pos(';',S);
            Inc(k);
         end;
         if k>0 then
            tmpCCEMailItem.Address:=S; //最后一个     Subject:=MailTitle;//主题
         with TIdText.Create(IdMessage1.MessageParts, MailBody.Lines) do
         begin
              ContentType := 'text/plain';
         end;
         IdMessage1.MessageParts.Clear;
         for I := 0 to MailAppen.Items.Count - 1 do
         begin
             AttachmentFileName := MailAppen.Items.Strings[i];
             if fileExists(AttachmentFileName) then
             begin
                  AttachmentFileSize:=AttachmentFileSize+GetSendAttchmentFileSizeof(AttachmentFileName);
                  if i > 0 then MessageParts.Add;
                  if FileExists(AttachmentFileName) then
                     TIdAttachmentFile.Create(IdMessage1.MessageParts, AttachmentFileName);
             end;
         end;
      end;
      ProgressBar1.Max:=AttachmentFileSize;
      With IdSMTP1 Do
      begin
         try
             Disconnect;
             {authentication settings}
             case SmtpAuthType of
               0: AuthType := atNone;
               1: AuthType := atDefault; {Simple Login}
             end;
             Username := MailUsername;
             Password := MailPassword;
             IdSMTP1.PipeLine:=False;//可以避开瑞星的监控
             {General setup}
             Host := EmailHost;
             Port := SmtpServerPort;
             ConnectTimeout:=300000;
             Application.ProcessMessages;
             Connect();  //连接SMTP服务器
             Application.ProcessMessages;
             Authenticate;
             try
             Application.ProcessMessages;
             Send(IdMessage1);  //向服务器发送邮箱
             Except
             end;
         Finally
             Disconnect;
         end;
      end;end;
      

  2.   

    以下是我正在使用的一只程序。可以正常发邮件。如果不能发,请检查一下防火墙,有可以是防火墙挡住了。unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, IdBaseComponent, IdComponent, IdTCPConnection,
      IdTCPClient, IdMessageClient, IdNNTP, IdSMTP, IdMessage, inifiles,
      ExtCtrls, DateUtils;type
      TForm1 = class(TForm)
        IdSMTP1: TIdSMTP;
        IdMessage1: TIdMessage;
        Label1: TLabel;
        procedure FormCreate(Sender: TObject);
        procedure Timer1Timer(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}function writelog(const logstr: string): integer;
    var
      logFilestr: textfile;
      FileHandle: thandle;
    begin
      try
        if not FileExists('c:\errorlog.log') then
        begin
          FileHandle := filecreate('c:\errorlog.log');
          FileClose(FileHandle);
        end;
        AssignFile(logFilestr, 'c:\errorlog.log');
        Reset(logFilestr);
        if FileSize(logfilestr) > 1000 * 8 then begin
          closefile(logfilestr);
          deletefile('c:\errorlog.log');
          FileHandle := filecreate('c:\errorlog.log');
          FileClose(FileHandle);
          AssignFile(logFilestr, 'c:\errorlog.log');
        end;
        append(logFilestr);
        WriteLn(logFilestr, logstr);
        //WriteLn(logFilestr, '');
        Closefile(logFilestr);
      finally  end;
      result := 0;
    end;procedure TForm1.FormCreate(Sender: TObject);
    var
      IniFile: TIniFile;
      FileHandle: Thandle;
      Filedate: TDateTime;
    begin
      label1.Caption := 'Start at ' + datetimetostr(now());
      IniFile := TIniFile.Create('.\mailer.ini');
      if inifile.ReadString('mailhead', 'checkfiledate', '') = '1' then
      begin
        if fileexists(inifile.ReadString('mailhead', 'Attachment', '')) then
        begin
          FileHandle := FileOpen(inifile.ReadString('mailhead', 'Attachment', ''), fmOpenRead);
          FileDate := FileDateToDateTime(FileGetDate(FileHandle));
          FileClose(filehandle);
          if dateof(filedate) <> dateof(now()) then begin
            writelog(DateTimeToStr(now) + #9+ 'Today has no Error.');
            Application.Terminate;
            Exit;
          end;
        end
        else begin
          writelog(DateTimeToStr(now) + #9+ 'No Errorlogfile.');
          Application.Terminate;
          exit;
        end;  end;  idsmtp1.Host := inifile.ReadString('mailserver', 'host', '');
      idsmtp1.Username := inifile.ReadString('mailserver', 'username', '');
      idsmtp1.Password := inifile.ReadString('mailserver', 'password', '');
      idsmtp1.AuthenticationType := atLogin;
      idmessage1.From.Text := inifile.ReadString('mailhead', 'from', '');
      idmessage1.Recipients.EMailAddresses := inifile.ReadString('mailhead', 'Recipients', '');
      idmessage1.Subject := inifile.ReadString('mailhead', 'subject', '');
      idmessage1.Body.text := 'See the attachment.     --' + datetimetostr(now());
      if fileexists(inifile.ReadString('mailhead', 'Attachment', '')) then
        TIdAttachment.Create(idMessage1.MessageParts, inifile.ReadString('mailhead', 'Attachment', ''));
      if fileexists(inifile.ReadString('mailhead', 'Attachment1', '')) then
        TIdAttachment.Create(idMessage1.MessageParts, inifile.ReadString('mailhead', 'Attachment1', ''));
      if fileexists(inifile.ReadString('mailhead', 'Attachment2', '')) then
        TIdAttachment.Create(idMessage1.MessageParts, inifile.ReadString('mailhead', 'Attachment2', ''));
      if fileexists(inifile.ReadString('mailhead', 'Attachment3', '')) then
        TIdAttachment.Create(idMessage1.MessageParts, inifile.ReadString('mailhead', 'Attachment3', ''));
      try
        idsmtp1.Connect;
      except
        begin
    //      showmessage('connect error');
          writelog(DateTimeToStr(now) +  #9+'Can not connect host' + idsmtp1.Host);
          Application.Terminate;
          exit;
        end;
      end;
    //if SMTP1.Connected() then
    //  begin
      try
        idsmtp1.Send(idmessage1);
      except
      //  showmessage('send fail');
        writelog(DateTimeToStr(now) + #9+ 'Send failed.');
        Application.Terminate;
        Exit;
      end;
      writelog(DateTimeToStr(now) + #9+'Send OK.');
      Application.Terminate;end;procedure TForm1.Timer1Timer(Sender: TObject);
    begin
      Close;
    end;end.
      

  3.   

    http://topic.csdn.net/t/20021010/17/1085447.html