怎样用delphi编写一个在线发送邮件的程序(100分)

  • 主题发起人 主题发起人 lucky.gong
  • 开始时间 开始时间
L

lucky.gong

Unregistered / Unconfirmed
GUEST, unregistred user!
各位高手,我现在写了一个小程序,算是ERP的二次开发吧.在面板上有几个文本框edit,用来填写一些数据,提交后写入到ERP的数据库中,在某一表中就增加了一条记录.同时,我想把这条记录的信息通过邮件的方式发给指定的某些人(收件人固定),我们公司使用exchange服务器,客户端用outlook接收.ERP数据库和exchang不在同一服务器上.请问大家有没有办法.最好有实际例子和代码?
不好意思,我分不多,只能给100分,希望各位朋友帮忙.
 
如果你使用的idSmtp,设置AuthenticationType:=atLogin;然后设置userid,和password就可以了
下边是 Indy Demo 里边的:
{-----------------------------------------------------------------------------
Demo Name: SMTPRelay
Author: Allen O'Neill - Springboard Technologies Ltd - http://www.springboardtechnologies.com
Copyright: Indy Pit Crew
Purpose:
History:
Date: 27/10/2002 00:27:55
Checked with Indy version: 9.0 - Allen O'Neill - Springboard Technologies Ltd - http://www.springboardtechnologies.com
-----------------------------------------------------------------------------
Notes:

Demonstrates sending an email without the use of a local SMTP server
This works by extracting the domain part form the recipient email address,
then doing an MX lookup against a DNS server for that domain part,
and finally connecting directly to the SMTP server that the MX record
point to, to deliver the message.

}


unit fMain;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
IdTCPConnection, IdTCPClient, IdMessageClient, IdSMTP, IdComponent,
IdUDPBase, IdUDPClient, IdDNSResolver, IdBaseComponent, IdMessage,
StdCtrls, ExtCtrls, ComCtrls, IdAntiFreezeBase, IdAntiFreeze;

type
TfrmMain = class(TForm)
IdMessage: TIdMessage;
IdDNSResolver: TIdDNSResolver;
IdSMTP: TIdSMTP;
Label1: TLabel;
sbMain: TStatusBar;
Label2: TLabel;
edtDNS: TEdit;
Label3: TLabel;
Label4: TLabel;
edtSender: TEdit;
Label5: TLabel;
edtRecipient: TEdit;
Label6: TLabel;
edtSubject: TEdit;
Label7: TLabel;
mmoMessageText: TMemo;
btnSendMail: TButton;
btnExit: TButton;
IdAntiFreeze: TIdAntiFreeze;
Label8: TLabel;
edtTimeOut: TEdit;
Label9: TLabel;
Label10: TLabel;
procedure btnExitClick(Sender: TObject);
procedure btnSendMailClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
fMailServers : TStringList;
Function PadZero(s:String):String;
Function GetMailServers:Boolean;
Function ValidData : Boolean;
Procedure SendMail; OverLoad;
Function SendMail(aHost : String):Boolean; OverLoad;
Procedure LockControls;
procedure UnlockControls;
Procedure Msg(aMessage:String);
end;

var
frmMain: TfrmMain;

implementation

{$R *.DFM}


procedure TfrmMain.btnExitClick(Sender: TObject);
begin
application.terminate;
end;

procedure TfrmMain.btnSendMailClick(Sender: TObject);
begin
Msg('');
LockControls;
if ValidData then SendMail;
UnlockControls;
Msg('');
end;

function TfrmMain.GetMailServers: Boolean;
var
i,x : integer;
LDomainPart : String;
LMXRecord : TMXRecord;
begin
if not assigned(fmailServers) then fMailServers := TStringList.Create;
fmailServers.clear;

Result := true;
with IdDNSResolver do
begin
QueryResult.Clear;
QueryRecords := [qtMX];
Msg('Setting up DNS query parameters');
Host := edtDNS.text;
ReceiveTimeout := StrToInt(edtTimeOut.text);
// Extract the domain part from recipient email address
LDomainPart := copy(edtRecipient.text,pos('@',edtRecipient.text)+1,length(edtRecipient.text)); // the domain name to resolve

try
Msg('Resolving DNS');
Resolve(LDomainPart);

if QueryResult.Count > 0 then
begin
for i := 0 to QueryResult.Count - 1 do
begin
LMXRecord := TMXRecord(QueryResult.Items);
fMailServers.Append(PadZero(IntToStr(LMXRecord.Preference)) + '=' + LMXRecord.ExchangeServer);
end;

// sort in order of priority and then remove extra data
fMailServers.Sorted := false;
for i := 0 to fMailServers.count - 1 do
begin
x := pos('=',fMailServers.Strings);
if x > 0 then fMailServers.Strings :=
copy(fMailServers.Strings,x+1,length(fMailServers.Strings));
end;
fMailServers.Sorted := true;
fMailServers.Duplicates := dupIgnore;
Result := true;
end
else
begin
Msg('No response from DNS server');
MessageDlg('There is no response from the DNS server !', mtInformation, [mbOK], 0);
Result := false;
end;
except
on E : Exception do
begin
Msg('Error resolving domain');
MessageDlg('Error resolving domain: ' + e.message, mtInformation, [mbOK], 0);
Result := false;
end;
end;

end;
end;

// Used in DNS preferance sorting
procedure TfrmMain.LockControls;
var i : integer;
begin
edtDNS.enabled := false;
edtSender.enabled := false;
edtRecipient.enabled := false;
edtSubject.enabled := false;
mmoMessageText.enabled := false;
btnExit.enabled := false;
btnSendMail.enabled := false;
end;

procedure TfrmMain.UnlockControls;
begin
edtDNS.enabled := true;
edtSender.enabled := true;
edtRecipient.enabled := true;
edtSubject.enabled := true;
mmoMessageText.enabled := true;
btnExit.enabled := true;
btnSendMail.enabled := true;
end;


function TfrmMain.PadZero(s: String): String;
begin
if length(s) < 2 then
s := '0' + s;
Result := s;
end;

procedure TfrmMain.SendMail;
var
i : integer;
begin
if GetMailServers then
begin
with IdMessage do
begin
Msg('Assigning mail message properties');
From.Text := edtSender.text;
Sender.Text := edtSender.text;
Recipients.EMailAddresses := edtRecipient.text;
Subject := edtSubject.text;
Body := mmoMessageText.Lines;
end;

for i := 0 to fMailServers.count -1 do
begin
Msg('Attempting to send mail');
if SendMail(fMailServers.Strings) then
begin
MessageDlg('Mail successfully sent and available for pickup by recipient !', mtInformation, [mbOK], 0);
Exit;
end;
end;
// if we are here then something went wrong .. ie there were no available servers to accept our mail!
MessageDlg('Could not send mail to remote server - please try again later.', mtInformation, [mbOK], 0);
end;
if assigned(fMailServers) then FreeAndNil(fMailServers);
end;

function TfrmMain.SendMail(aHost: String): Boolean;
begin
Result := false;
with IdSMTP do
begin
Caption := 'Trying to sendmail via: ' + aHost;
Msg('Trying to sendmail via: ' + aHost);
Host := aHost;
try
Msg('Attempting connect');
Connect;
Msg('Successful connect ... sending message');
Send(IdMessage);
Msg('Attempting disconnect');
Disconnect;
msg('Successful disconnect');
Result := true;
except on E : Exception do
begin
if connected then try disconnect; except end;
Msg('Error sending message');
result := false;
ShowMessage(E.Message);
end;
end;
end;
Caption := '';
end;


function TfrmMain.ValidData: Boolean;
var ErrString:string;
begin
Result := True;
ErrString := '';

if trim(edtDNS.text) = '' then ErrString := ErrString + #13 + #187 + 'DNS server not filled in';
if trim(edtSender.text) = '' then ErrString := ErrString + #13 + #187 + 'Sender email not filled in';
if trim(edtRecipient.text) = '' then ErrString := ErrString + #13 + #187 + 'Recipient not filled in';

if ErrString <> '' then
begin
MessageDlg('Cannot proceed due to the following errors:'+#13+#10+ ErrString, mtInformation, [mbOK], 0);
Result := False;
end;
end;

procedure TfrmMain.Msg(aMessage: String);
begin
sbMain.SimpleText := aMessage;
application.ProcessMessages;
end;

end.
 
请问abin30,这些代码是组件里面的内容吗?直接调用该组件是否可以
 
abin30
这好像是一个完整程序呀
 
直接可以使用,上边的代码是IndyDemo里边的,可以下一个看看。
 
看这个:http://www.delphibbs.com/delphibbs/dispq.asp?lid=3359545
 
我看了一些书上INDY例子,发现都写得很简单,都是用smtp.163.com作为HOST.如果是公司内部有邮件服务器呢exch01,那HOST应该怎么设
 
是不是分少了,就没有更多人帮忙?我用公司自己的邮件服务器,HOST写成smtp.exch01,行不通呀
 
这个无所谓了,只要能找到你的邮件服务器久可以,可以是服务器的IP或者域名或者主机名。
 
提示出错.socket error #10053(#11001等),请问这是什么原因啊.
 
abin30

我看到一个这样实例,觉得代码简单,就想用,但是就是不行.主体程序就是下面.麻烦你帮我看,我在&quot;IdSMTP1.Host := edt_SMTP.Text&quot;中HOST填了smtp.exch01,发送就出错.
procedure TForm1.Button2Click(Sender: TObject);
begin

IdMessage1.From.Address := edt_From.Text;

IdMessage1.From.Name := edt_UserName.Text;

IdMessage1.Recipients.EMailAddresses := edt_To.Text;

IdMessage1.Subject := edt_Subject.Text;

IdMessage1.Body.Add(Memo1.Text);

TidAttachment.Create(IdMessage1.MessageParts, edt_File.Text);


IdSMTP1.Host := edt_SMTP.Text;

IdSMTP1.Username := edt_UserName.Text;

IdSMTP1.Password := edt_Psw.Text;

IdSMTP1.AuthenticationType := atLogin;

IdSMTP1.Connect;

IdSMTP1.Send(IdMessage1);

end;

procedure TForm1.IdSMTP1WorkEnd(Sender: TObject; AWorkMode: TWorkMode);
begin
ShowMessage('finished&pound;&iexcl;');
// close
IdSMTP1.Disconnect;
end;
 
socket error #10053 soft ware caused connection abort 请问这是什么原因啊
 
接受答案了.
 
后退
顶部