邮件发送(50分)

  • 主题发起人 主题发起人 飞来石
  • 开始时间 开始时间

飞来石

Unregistered / Unconfirmed
GUEST, unregistred user!
如何使用SMTP组件发送邮件到新浪的邮箱e-mail地址是:mymailbox@sina.com
Host如何设置?
 
pop3.sina.com.cn
 
先创建一个空的Delphi应用程序
放5个TMemos, 8 个TEdits, 1个 TListBox, 3 个TButtons, a TCheckBox, a TNMSMTP, a TRadioGroup, and a TOpenDialog 在主窗体

Edit1: Host 属性
Edit2: 你的邮箱名字
Edit3: PostMessage.Date 属性
Edit4: PostMessage.FromAddress 属性
Edit5: PostMessage.FromName 属性
Edit6: PostMessage.LocalProgram 属性
Edit7: PostMessage.ReplyTo属性
Edit8: PostMessage.Subject 属性。邮件的主题
Memo1: PostMessage.ToAddress 属性

Memo2: PostMessage.ToBlindCarbonCopy 同时发送的信箱
Memo3: PostMessage.ToCarbonCopy 密送信箱
Memo4: PostMessage.Body 内容
Memo5: 显示状态
ListBox1: PostMessage.Attachments 附件
Button1: 连接、不连接 按钮
Button2: 发送按钮
Button3: 清空按钮
CheckBox1: 设置参数的值
OpenDialog1: 添加附件
RadioGroup1: 指定文件的发送方式

添加2 items 在 RadioGroup1的 Items 属性: MIME 和 UUEncode



Insert the following code into Button1's OnClick event:

procedure TForm1.Button1Click(Sender: TObject);
begin
if NMSMTP1.Connected then
NMSMTP1.Disconnect
else
begin
NMSMTP1.Host := Edit1.Text;
NMSMTP1.UserID := Edit2.Text;
NMSMTP1.Connect;
end;
end;
当Button1被单击时,如果NMSMTP1已经连上了邮件服务器,则把他断开,否则,连上Edit1指定的邮件服务器





procedure TForm1.Button2Click(Sender: TObject);
begin
if NMSMTP1.Connected then
begin
NMSMTP1.ClearParams := CheckBox1.Checked;

NMSMTP1.SubType := mtPlain;
case RadioGroup1.ItemIndex of
0: NMSMTP1.EncodeType := uuMime;
1: NMSMTP1.EncodeType := uuCode;
end;
NMSMTP1.PostMessage.FromAddress := Edit4.Text;
NMSMTP1.PostMessage.FromName := Edit5.Text;
NMSMTP1.PostMessage.ToAddress.Text := Memo1.Text;
NMSMTP1.PostMessage.ToCarbonCopy.Text := Memo3.Text;
NMSMTP1.PostMessage.ToBlindCarbonCopy.Text := Memo2.Text;
NMSMTP1.PostMessage.Body.Text := Memo4.Text;

NMSMTP1.PostMessage.Attachments.Text := ListBox1.Items.Text;
NMSMTP1.PostMessage.Subject := Edit8.Text;
NMSMTP1.PostMessage.LocalProgram := Edit6.Text;
NMSMTP1.PostMessage.Date := Edit3.Text;
NMSMTP1.PostMessage.ReplyTo := Edit7.Text;
NMSMTP1.SendMail;
end
else
ShowMessage('You need to connect before you can send your message');
end;



procedure TForm1.Button3Click(Sender: TObject);
begin
NMSMTP1.ClearParameters;
Edit3.Clear;
Edit4.Clear;
Edit5.Clear;
Edit6.Clear;
Edit7.Clear;
Edit8.Clear;
Memo1.Clear;
Memo2.Clear;
Memo3.Clear;
Memo4.Clear;
Memo5.Clear;
ListBox1.Clear;
end;



procedure TForm1.ListBox1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
if Key = VK_INSERT then
if OpenDialog1.Execute then
ListBox1.Items.Add(OpenDialog1.FileName);
if Key = VK_DELETE then
ListBox1.Items.Delete(ListBox1.ItemIndex);
end;

I

procedure TForm1.NMSMTP1AttachmentNotFound(Filename: String);
begin
Memo5.Lines.Add('File attachment '+FileName+' not found');
end;



procedure TForm1.NMSMTP1AuthenticationFailed(var Handled: Boolean);
var
S: String;
begin
S := NMSMTP1.UserID;
if InputQuery('Authentication Failed', 'Invalid User ID. New User ID: ', S) then
begin
NMSMTP1.UserID := S;
Handled := TRUE;
end;
end;


procedure TForm1.NMSMTP1Connect(Sender: TObject);
begin
Memo5.Lines.Add('Connected');
end;






procedure TForm1.NMSMTP1SendStart(Sender: TObject);
begin
Memo5.Lines.Add('Sending Message');
end;


procedure TForm1.NMSMTP1EncodeStart(Filename: String);
begin
Memo5.Lines.Add('Encoding '+FileName);
end;



procedure TForm1.NMSMTP1EncodeEnd(Filename: String);

begin
Memo5.Lines.Add(FileName+' encoded');
end;



procedure TForm1.NMSMTP1Failure(Sender: TObject);
begin
Memo5.Lines.Add('Message delivery failure');
end;


procedure TForm1.NMSMTP1Success(Sender: TObject);
begin
Memo5.Lines.Add('Message sent successfully');
end;




procedure TForm1.NMSMTP1HeaderIncomplete(var handled: Boolean; hiType: Integer);

var
S: String;
begin
case hiType of
hiFromAddress:
if InputQuery('Missing From Address', 'Enter From Address: ', S) then
begin
NMSMTP1.PostMessage.FromAddress := S;
Handled := TRUE;
end;

hiToAddress:
if InputQuery('Missing To Address', 'Enter To Address: ', S) then
begin
NMSMTP1.PostMessage.ToAddress.Text := S;
Handled := TRUE;
end;

end;
end;


procedure TForm1.NMSMTP1RecipientNotFound(Recipient: String);
begin
Memo5.Lines.Add('Recipient '+Recipient+' not found');
end;

 
你的发件地址是什么?比如,发件人是21cn的,就用21cn的smtp
 
不必用21cn的smtp用Delphi的TNMSTP就可以
 
用INDY控件的SMTP就行了,不过要作一点修改

这个是因为,indy发邮件时候,会加上一个indy标志,然后163居然把这种标志当作垃圾邮件
所以,就发送不出去了,而etang就不这么检查,所以可以。
解决办法,把标志去掉了,要修改控件原码
找到IDMessage.pas,再其中搜索Values['X-Library'] ,把它所在的行注释掉,就可以了
然后保存,重新编译控件包,或者重新安装控件,要么把这个单元加入工程也可以

 
http://www.ccw.com.cn/htm/app/down/1204Source.zip
 
现在好多网站的发件服务要求登录了,NMSMTP不行了
 
菜鸟王子:我用你的方法,显示状态为Connected,
我就添了一部分内容,有
NMSMTP1.PostMessage.FromAddress,
Edit8: PostMessage.Subject
Memo1: PostMessage.ToAddress 属性
Memo4: PostMessage.Body 内容
提示:Message sent successfully
但是我在目标邮箱里什么都收不到,为什么,能给我回答吗
 
接受答案了.
 
后退
顶部