+ -
当前位置:首页 → 问答吧 → 如何用DELPHI2010发送带附件的邮件?

如何用DELPHI2010发送带附件的邮件?

时间:2011-11-22

来源:互联网

在网上和论坛上都搜了,没有发现相关的内容,请大家赐教!
如何用DELPHI2010发送带附件的邮件

作者: jxbw197408   发布时间: 2011-11-22

该回复于2011-11-22 15:56:50被管理员删除

  • 对我有用[0]
  • 丢个板砖[0]
  • 引用
  • 举报
  • 管理
  • TOP
#2楼 得分:0回复于:2011-11-22 17:18:18
看到有回复一次,咋没看到内容呢?

作者: scu96124678   发布时间: 2011-11-22

首先在uses列表中添加: IdAttachmentFile单元。
我写了一个简单的过程,代码如下:

//参数Attachment是要作为附件的文件。例如:f:\text.txt
procedure FastMail(Attachment: string);
var
  FSMTP: TIdSMTP;
  FMailMessage: TIdMessage;
begin
  FSMTP := TIdSMTP.Create(nil);
  FMailMessage := TIdMessage.Create(nil);
  //为正常显示汉字设置字符集
  FMailMessage.CharSet := 'gb2312';
  //服务器参数
  FSMTP.Host := '这里填写你的邮件服务器';
  FSMTP.Username := '你的用户名';
  FSMTP.Password := '你的密码';

  //尝试连接服务器
  try
  FSMTP.Connect;
  except
  Exit;
  end;

  FMailMessage.Subject := 'Mail with Attachment';
  FMailMessage.Body.Add('A demo to show how to send a mail with attachments');
  FMailMessage.From.Address := '发信人地址';
  FMailMessage.Recipients.EMailAddresses := '收信人地址';
  //附件处理,可以连续添加多个附件
  if FileExists(Attachment) then
  TIdAttachmentFile.Create(FMailMessage.MessageParts, FAttachment);
  //设置邮件优先级为最高
  FMailMessage.Priority := mpHighest;

  try
  FSMTP.Send(FMailMessage);
  finally
  FSMTP.Disconnect;
  FSMTP.Free;
  FMailMessage.Free;
  end;
end;

这段代码在Delphi 2010 和 XE2中皆可通过。希望对楼主有帮助。

作者: jxbw197408   发布时间: 2011-11-22