我做了一个多线程的发送email实现,但是在sendmail一步时出现错误,以下是部分代码,base64编码是我从网上找的,已编译测试过,没有问题。
1.创建了一个线程类TSendThread = class(TThread)
type
TSendThread = class(TThread)
procedure NMSMTPConnect();
public NMSMTP1: TNMSMTP;
constructor Create(strTo: TStringList; strSubject: string; strBody: TStringList);
procedure Execute; override;
function EncodeString(Decoded:string):String;
function EncodeBASE64(Encoded: TMemoryStream {TMailText};
Decoded: TMemoryStream): Integer; //编码函数
procedure sendLetter();
end;
const
mailuser:string = 'wjdragon76';
mailpass:string = '*******';implementationconstructor TSendThread.Create(strTo: TStringList; strSubject: string; strBody: TStringList);
begin
NMSMTP1 := TNMSMTP.Create(nil);
NMSMTP1.Host := 'smtp.sina.com.cn';
NMSMTP1.Port := 25;
NMSMTP1.UserID := 'wjdragon76';
NMSMTP1.PostMessage.ToAddress := strTo;
NMSMTP1.PostMessage.Subject := strSubject;
NMSMTP1.PostMessage.Body := strBody; inherited Create(False);
FreeOnTerminate := True;
end;procedure TSendThread.Execute;
begin
sendletter;
end;procedure TSendThread.NMSMTPConnect;
var
strUserName,strPassword:string;begin {对用户名和密码进行Base64编码}
strUserName:=EncodeString(mailuser);
strPassword:=EncodeString(mailpass);
{进行认证,输入编码后的用户名、密码}
nmsmtp1.Transaction('auth login');
nmsmtp1.Transaction(strUserName);
nmsmtp1.Transaction(strPassword);
end;procedure TSendThread.sendLetter;begin
if NMSMTP1.Connected then
begin
NMSMTP1.Disconnect;
end;
NMSMTP1.Connect;
while not NMSMTP1.Connected do
NMSMTP1.Connect; NMSMTPConnect;
NMSMTP1.SendMail;
end;2.一个窗体
type
TForm1 = class(TForm)
Button1: TButton;
procedure SendClick(Sender: TObject);
private
{ Private declarations } public
{ Public declarations }
toMail : TStringList;
sSubject: String;
sContent: TStringList; end;var
Form1: TForm1;implementation{$R *.dfm}{ TForm1 }
procedure TForm1.SendClick(Sender: TObject);
var
sendthread:TSendThread;begin
toMail := TStringList.Create;
toMail.Add('[email protected]'); sSubject := 'ok'; sContent := TStringList.Create;
sContent.Add('this is my mail');
sendthread := TSendThread.Create(toMail,sSubject,sContent);
end;
在运行到sendletter中的最后一步也就是sendmail时出现错误:
project sendmail.exe raised exception class Exception with message 'Incomplete Header'.Process stopped .Used Step or Run to continue
我弄不清楚问题出在何处?请大家赐教,谢谢
1.创建了一个线程类TSendThread = class(TThread)
type
TSendThread = class(TThread)
procedure NMSMTPConnect();
public NMSMTP1: TNMSMTP;
constructor Create(strTo: TStringList; strSubject: string; strBody: TStringList);
procedure Execute; override;
function EncodeString(Decoded:string):String;
function EncodeBASE64(Encoded: TMemoryStream {TMailText};
Decoded: TMemoryStream): Integer; //编码函数
procedure sendLetter();
end;
const
mailuser:string = 'wjdragon76';
mailpass:string = '*******';implementationconstructor TSendThread.Create(strTo: TStringList; strSubject: string; strBody: TStringList);
begin
NMSMTP1 := TNMSMTP.Create(nil);
NMSMTP1.Host := 'smtp.sina.com.cn';
NMSMTP1.Port := 25;
NMSMTP1.UserID := 'wjdragon76';
NMSMTP1.PostMessage.ToAddress := strTo;
NMSMTP1.PostMessage.Subject := strSubject;
NMSMTP1.PostMessage.Body := strBody; inherited Create(False);
FreeOnTerminate := True;
end;procedure TSendThread.Execute;
begin
sendletter;
end;procedure TSendThread.NMSMTPConnect;
var
strUserName,strPassword:string;begin {对用户名和密码进行Base64编码}
strUserName:=EncodeString(mailuser);
strPassword:=EncodeString(mailpass);
{进行认证,输入编码后的用户名、密码}
nmsmtp1.Transaction('auth login');
nmsmtp1.Transaction(strUserName);
nmsmtp1.Transaction(strPassword);
end;procedure TSendThread.sendLetter;begin
if NMSMTP1.Connected then
begin
NMSMTP1.Disconnect;
end;
NMSMTP1.Connect;
while not NMSMTP1.Connected do
NMSMTP1.Connect; NMSMTPConnect;
NMSMTP1.SendMail;
end;2.一个窗体
type
TForm1 = class(TForm)
Button1: TButton;
procedure SendClick(Sender: TObject);
private
{ Private declarations } public
{ Public declarations }
toMail : TStringList;
sSubject: String;
sContent: TStringList; end;var
Form1: TForm1;implementation{$R *.dfm}{ TForm1 }
procedure TForm1.SendClick(Sender: TObject);
var
sendthread:TSendThread;begin
toMail := TStringList.Create;
toMail.Add('[email protected]'); sSubject := 'ok'; sContent := TStringList.Create;
sContent.Add('this is my mail');
sendthread := TSendThread.Create(toMail,sSubject,sContent);
end;
在运行到sendletter中的最后一步也就是sendmail时出现错误:
project sendmail.exe raised exception class Exception with message 'Incomplete Header'.Process stopped .Used Step or Run to continue
我弄不清楚问题出在何处?请大家赐教,谢谢
解决方案 »
- 用openDialog选择EXE后提取其ICO然后显示在ListView里
- Combobox的问题
- 怎样设置EXCEL文件中的格式?比如说某一列的都保留一位小数?谢谢
- 如何用DELPHI实现隧道监控类的软件
- delphi游戏开发人员讨论QQ群:7523207
- Delphi错误,请大家帮忙解决
- 关于idhttp中代理服务器的用法
- DataBase Desktop中的Alias新增与MS Access的连接失败。急请教。
- Kylix到底出了没有?如果出了到哪里能找到?
- 为什么笔记本装不上DELPHI5.0???
- 请问这是什么意思 Invalid object name 't_print'
- 程序运行后怎样得到程序所在的路径?
而Delphi5的TNMSMTP控件没有验证信息,建议用Delphi7的TSMTP
转载
首先 在indy clients 页上 摘取一个TidSMTP控件(就是那个蓝颜色的柜子啦),然后 在indy misc 页上
摘取一个TIdMessage控件(象一个信笺的东东)。呵呵,我们给他们取名为SMTP和IdMsgSend 。 然后开始自己的如下代码 procedure Tform1.SendAlarmEmail;
begin
if SMTPAuthority then //SMTPAuthority 是 表示这个SMTP服务器是否需要认证的boolean
SMTP.AuthenticationType := atLogin
else SMTP.AuthenticationType := atNone;
SMTP.UserID := [email protected];//帐户
SMTP.Password := **********;//密码
{General setup}
SMTP.Host := SMTP.263.net;
SMTP.Port := 25;
try
SMTP.Connect;
except
Showmessage(‘连接SMTP服务器失败!‘);
Exit;
end;
try
with IdMsgSend do
begin
body.Clear;
Body.Add(‘hello world‘);//内容
From.Text := ‘[email protected]‘;
Recipients.EMailAddresses :=‘[email protected]‘//收件人
Subject:=‘老友,哈喽‘//主题
end;
SMTP.Send(IdMsgSend);
finally
SMTP.Disconnect;
end;
end;