L
linxiao8302
Unregistered / Unconfirmed
GUEST, unregistred user!
本人详细贴出了相关代码,希望各位鼎力相助!!!
uses
IdComponent,IdTCPConnection, IdTCPClient, IdMessageClient, IdSMTP,IdBaseComponent,IdMessage,IdExplicitTLSClientServerBase,
IdSMTPBase, IdAttachmentFile,IdText;//引用的与Indy10有关的单元
type //省去了窗体的定义部分
TSmtpThread = class(TThread) //定义的线程,用于发邮件
private
FHost: String;
FUserName: String;
FPassword:String;
// FPriority:TThreadPriority;
protected
procedure Execute; override;
public
constructor Create(Host:String;UserName:String;Password:String);
destructor Destroy;override;
function URLGet(s:String):String;
function CIDGet(url:String):String;
function UrlToCid(s:String;s1:String;s2:String):String;
function InlineParse(s:String):String;
end;
var
ComposeForm: TComposeForm; //窗体
not_relatedAttachmentList:TStrings;//用于记录附件信息
relatedAttachmentList:TStrings; //用于记录嵌式附件信息
//以下是具体执行部分
procedure TComposeForm.FormCreate(Sender: TObject);
begin
not_relatedAttachmentList:=Tstringlist.Create;
relatedAttachmentList:=TStringList.Create;
end;
procedure TComposeForm.ComposeAttachmentExecute(Sender: TObject);
begin
if OpenDialog1.Execute then
not_relatedAttachmentList.Add(OpenDialog1.FileName);//添加附件时加入文件名
end;
//there we define some method in SmtpThread to send the message
//writen in HTMLEdit1 and some transfrom ensure the success of sent of message.
constructor TSmtpThread.Create(Host:String;UserName:String;Password:String);
begin
inherited Create(False);
Priority :=tpNormal;
FreeOnTerminate := True;
FHost:=Host;
FUserName:=UserName;
FPassword:=Password;
end;
destructor TSmtpThread.Destroy;
begin
inherited Destroy;
end;
procedure TSmtpThread.Execute;
var
Smtp:TIdSMTP;
Msg:TIdMessage;
tempstr1,tempstr2:string;
i:integer;
begin
tempstr1:=ComposeForm.HTMLEdit1.InnerHTML;//一个HTMLEdit控件,
// 此语句产生 html格式的字符串
//各位也可用下面语句替换帮忙测试
//tempstr1:='<html><body><p>This message has an inline
// image<img src="c:/temp/image1.gif" /></p></body></html>'
tempstr2:=InlineParse(tempstr1);//执行内嵌式附件信的转化
Msg:=TIdMessage.Create(nil);//动态创建
//以下部分完成格式的匹配
//*************************************************
if (relatedAttachmentList.Count>0) and (not_relatedAttachmentList.Count>0) then
begin
with TIdText.Create(Msg.MessageParts, nil) do begin
ContentType := 'multipart/alternative';
ParentPart :=-1;
end;
with TIdText.Create(Msg.MessageParts, nil) do begin
Body.Text :=tempstr2;
ContentType := 'text/html';
ParentPart := 0;
end;
for i:=0 to relatedAttachmentList.Count-1 do
with TIdAttachmentFile.Create(Msg.MessageParts, relatedAttachmentList.Strings) do begin
ContentID := CIDGet(relatedAttachmentList.Strings);
ContentType := 'image/*';
ContentDisposition := 'inline';
ParentPart := 0;
end;
for i:=0 to not_relatedAttachmentList.Count-1 do
with TIdAttachmentFile.Create(Msg.MessageParts,not_relatedAttachmentList.Strings) do begin
ContentID := CIDGet(not_relatedAttachmentList.Strings);
ContentType := 'whatever';
ParentPart :=-1;
end;
Msg.ContentType:='multipart/mixed';
end;
if (relatedAttachmentList.Count>0) and (not_relatedAttachmentList.Count<=0) then
begin
with TIdText.Create(Msg.MessageParts, nil) do begin
Body.Text :=tempstr2;
ContentType := 'text/html';
ParentPart := -1;
end;
for i:=0 to relatedAttachmentList.Count-1 do
with TIdAttachmentFile.Create(Msg.MessageParts, relatedAttachmentList.Strings) do begin
ContentID := CIDGet(relatedAttachmentList.Strings);
ContentType := 'image/*';
ContentDisposition := 'inline';
ParentPart := -1;
end;
Msg.ContentType:='multipart/related; type="text/html"';
end;
if (relatedAttachmentList.Count<=0) and (not_relatedAttachmentList.Count>0) then
begin
with TIdText.Create(Msg.MessageParts, nil) do begin
Body.Text :=tempstr2;
ContentType := 'text/html';
ParentPart := -1;
end;
for i:=0 to not_relatedAttachmentList.Count-1 do
with TIdAttachmentFile.Create(Msg.MessageParts,not_relatedAttachmentList.Strings) do begin
ContentID := CIDGet(not_relatedAttachmentList.Strings);
ContentType := 'whatever';
ParentPart :=-1;
end;
Msg.ContentType:='multipart/mixed';
end;
if (relatedAttachmentList.Count<=0) and (not_relatedAttachmentList.Count<=0) then
begin
with TIdText.Create(Msg.MessageParts, nil) do begin
Body.Text :=tempstr2;
ContentType := 'text/html';
ParentPart := -1;
end;
Msg.ContentType:='text/html';
end;
//**************************************************
with Msg do
begin
Clear;
From.Address:='linxiao8302@163.com';//直接输入,方便测试
//大家可以直接往我的这些邮箱中发,也方便我比较分析
ReplyTo.EMailAddresses:='scandinavian0330@yahoo.com';
CCList.EMailAddresses:='scandinavian0330@yahoo.com';
Subject:='ThanksForYourHelp';
Priority := TIdMessagePriority(mpHighest);
end;
Smtp:=TIdSMTP.Create(nil);
with Smtp do
begin
Host:=FHost;
Port:= 25;
Username:=FUserName;
Password:=FPassword;
AuthType := atDefault;
Connect;
try
Send(Msg);
showmessage('success');//测试时加的
finally
Disconnect;
end;
end;
Msg.Free;
Smtp.Free;
end;
function TSmtpThread.URLGet(s:String):String;//取得html中插入的图片等
//信息的物理地址,不知各位是怎么做的
var
p:integer;
begin
result:='';
p:=Pos('src="cid',s);
if p>0 then exit;
p:=Pos('src="',s);
if p>0 then begin
s:=Copy(s,p+5,Length(s)-p-10);
p:=Pos('"',s);
result:=copy(s,1,p-1);
end;
end;
function TSmtpThread.CIDGet(url:String):String;//直接将文件名作为CID
begin //写成函数是方便以后改成其他处理方式
result:=ExtractFileName(url);
end;
function TSmtpThread.UrlToCid(s:String;s1:String;s2:String):String;
var //转化HTML中的物理地址为CID
p:Integer;
begin
p:=pos(s1,s);
Delete(s,p,Length(s1));
Insert('cid:'+s2,s,p);
result:=s;
end;
function TSmtpThread.InlineParse(s:string):String;
var //对全文进行CID替换
htmlText:String;
cid,url:String;
begin
htmlText:=s;
url:=URLGet(htmlText);
while url<>'' do begin
relatedAttachmentList.Add(url);
cid:=CIDGet(url);
htmlText:=UrlToCid(htmlText,url,cid);
url:=URLGet(htmlText);
end;
result:=htmlText;
end;
procedure TComposeForm.SendMailClick(Sender: TObject);//发信
begin //各位用自己邮箱帮忙测试哟,不甚感激
TSmtpThread.Create('smtp.163.com','linxiao8302','******');
end;
uses
IdComponent,IdTCPConnection, IdTCPClient, IdMessageClient, IdSMTP,IdBaseComponent,IdMessage,IdExplicitTLSClientServerBase,
IdSMTPBase, IdAttachmentFile,IdText;//引用的与Indy10有关的单元
type //省去了窗体的定义部分
TSmtpThread = class(TThread) //定义的线程,用于发邮件
private
FHost: String;
FUserName: String;
FPassword:String;
// FPriority:TThreadPriority;
protected
procedure Execute; override;
public
constructor Create(Host:String;UserName:String;Password:String);
destructor Destroy;override;
function URLGet(s:String):String;
function CIDGet(url:String):String;
function UrlToCid(s:String;s1:String;s2:String):String;
function InlineParse(s:String):String;
end;
var
ComposeForm: TComposeForm; //窗体
not_relatedAttachmentList:TStrings;//用于记录附件信息
relatedAttachmentList:TStrings; //用于记录嵌式附件信息
//以下是具体执行部分
procedure TComposeForm.FormCreate(Sender: TObject);
begin
not_relatedAttachmentList:=Tstringlist.Create;
relatedAttachmentList:=TStringList.Create;
end;
procedure TComposeForm.ComposeAttachmentExecute(Sender: TObject);
begin
if OpenDialog1.Execute then
not_relatedAttachmentList.Add(OpenDialog1.FileName);//添加附件时加入文件名
end;
//there we define some method in SmtpThread to send the message
//writen in HTMLEdit1 and some transfrom ensure the success of sent of message.
constructor TSmtpThread.Create(Host:String;UserName:String;Password:String);
begin
inherited Create(False);
Priority :=tpNormal;
FreeOnTerminate := True;
FHost:=Host;
FUserName:=UserName;
FPassword:=Password;
end;
destructor TSmtpThread.Destroy;
begin
inherited Destroy;
end;
procedure TSmtpThread.Execute;
var
Smtp:TIdSMTP;
Msg:TIdMessage;
tempstr1,tempstr2:string;
i:integer;
begin
tempstr1:=ComposeForm.HTMLEdit1.InnerHTML;//一个HTMLEdit控件,
// 此语句产生 html格式的字符串
//各位也可用下面语句替换帮忙测试
//tempstr1:='<html><body><p>This message has an inline
// image<img src="c:/temp/image1.gif" /></p></body></html>'
tempstr2:=InlineParse(tempstr1);//执行内嵌式附件信的转化
Msg:=TIdMessage.Create(nil);//动态创建
//以下部分完成格式的匹配
//*************************************************
if (relatedAttachmentList.Count>0) and (not_relatedAttachmentList.Count>0) then
begin
with TIdText.Create(Msg.MessageParts, nil) do begin
ContentType := 'multipart/alternative';
ParentPart :=-1;
end;
with TIdText.Create(Msg.MessageParts, nil) do begin
Body.Text :=tempstr2;
ContentType := 'text/html';
ParentPart := 0;
end;
for i:=0 to relatedAttachmentList.Count-1 do
with TIdAttachmentFile.Create(Msg.MessageParts, relatedAttachmentList.Strings) do begin
ContentID := CIDGet(relatedAttachmentList.Strings);
ContentType := 'image/*';
ContentDisposition := 'inline';
ParentPart := 0;
end;
for i:=0 to not_relatedAttachmentList.Count-1 do
with TIdAttachmentFile.Create(Msg.MessageParts,not_relatedAttachmentList.Strings) do begin
ContentID := CIDGet(not_relatedAttachmentList.Strings);
ContentType := 'whatever';
ParentPart :=-1;
end;
Msg.ContentType:='multipart/mixed';
end;
if (relatedAttachmentList.Count>0) and (not_relatedAttachmentList.Count<=0) then
begin
with TIdText.Create(Msg.MessageParts, nil) do begin
Body.Text :=tempstr2;
ContentType := 'text/html';
ParentPart := -1;
end;
for i:=0 to relatedAttachmentList.Count-1 do
with TIdAttachmentFile.Create(Msg.MessageParts, relatedAttachmentList.Strings) do begin
ContentID := CIDGet(relatedAttachmentList.Strings);
ContentType := 'image/*';
ContentDisposition := 'inline';
ParentPart := -1;
end;
Msg.ContentType:='multipart/related; type="text/html"';
end;
if (relatedAttachmentList.Count<=0) and (not_relatedAttachmentList.Count>0) then
begin
with TIdText.Create(Msg.MessageParts, nil) do begin
Body.Text :=tempstr2;
ContentType := 'text/html';
ParentPart := -1;
end;
for i:=0 to not_relatedAttachmentList.Count-1 do
with TIdAttachmentFile.Create(Msg.MessageParts,not_relatedAttachmentList.Strings) do begin
ContentID := CIDGet(not_relatedAttachmentList.Strings);
ContentType := 'whatever';
ParentPart :=-1;
end;
Msg.ContentType:='multipart/mixed';
end;
if (relatedAttachmentList.Count<=0) and (not_relatedAttachmentList.Count<=0) then
begin
with TIdText.Create(Msg.MessageParts, nil) do begin
Body.Text :=tempstr2;
ContentType := 'text/html';
ParentPart := -1;
end;
Msg.ContentType:='text/html';
end;
//**************************************************
with Msg do
begin
Clear;
From.Address:='linxiao8302@163.com';//直接输入,方便测试
//大家可以直接往我的这些邮箱中发,也方便我比较分析
ReplyTo.EMailAddresses:='scandinavian0330@yahoo.com';
CCList.EMailAddresses:='scandinavian0330@yahoo.com';
Subject:='ThanksForYourHelp';
Priority := TIdMessagePriority(mpHighest);
end;
Smtp:=TIdSMTP.Create(nil);
with Smtp do
begin
Host:=FHost;
Port:= 25;
Username:=FUserName;
Password:=FPassword;
AuthType := atDefault;
Connect;
try
Send(Msg);
showmessage('success');//测试时加的
finally
Disconnect;
end;
end;
Msg.Free;
Smtp.Free;
end;
function TSmtpThread.URLGet(s:String):String;//取得html中插入的图片等
//信息的物理地址,不知各位是怎么做的
var
p:integer;
begin
result:='';
p:=Pos('src="cid',s);
if p>0 then exit;
p:=Pos('src="',s);
if p>0 then begin
s:=Copy(s,p+5,Length(s)-p-10);
p:=Pos('"',s);
result:=copy(s,1,p-1);
end;
end;
function TSmtpThread.CIDGet(url:String):String;//直接将文件名作为CID
begin //写成函数是方便以后改成其他处理方式
result:=ExtractFileName(url);
end;
function TSmtpThread.UrlToCid(s:String;s1:String;s2:String):String;
var //转化HTML中的物理地址为CID
p:Integer;
begin
p:=pos(s1,s);
Delete(s,p,Length(s1));
Insert('cid:'+s2,s,p);
result:=s;
end;
function TSmtpThread.InlineParse(s:string):String;
var //对全文进行CID替换
htmlText:String;
cid,url:String;
begin
htmlText:=s;
url:=URLGet(htmlText);
while url<>'' do begin
relatedAttachmentList.Add(url);
cid:=CIDGet(url);
htmlText:=UrlToCid(htmlText,url,cid);
url:=URLGet(htmlText);
end;
result:=htmlText;
end;
procedure TComposeForm.SendMailClick(Sender: TObject);//发信
begin //各位用自己邮箱帮忙测试哟,不甚感激
TSmtpThread.Create('smtp.163.com','linxiao8302','******');
end;