用Delphi發郵件

2021-07-12 01:31:54 字數 4151 閱讀 8777

用delphi發郵件

控制項idsmtp1: tidsmtp;

idmessage1: tidmessage;

**idsmtp1.host:='smtp.163.com';

idsmtp1.port:=25;

idsmtp1.username:='發信人名稱;

idsmtp1.password:='發信人郵箱密碼';

idsmtp1.connect();

idmessage1.body.clear;

idmessage1.body.add(信的內容);

idmessage1.from.text:='從哪發的,應該可以郵箱欺騙';

idmessage1.recipients.emailaddresses:='發到哪個郵箱';

idmessage1.subject:='郵件題目';

idsmtp1.authenticationtype := atlogin;

idsmtp1.authenticate;

idsmtp1.send(idmessage1);

idsmtp1.disconnect;

end;

重點來了,有朋友試驗這個**說不好用,剛開始我測試也不行,在csdn上用了幾百分也沒弄明白是怎麼回事,

後來乙個偶然的機會發現,用新註冊的郵箱用這個**是發不了了,用用過一段時間的郵箱就可以發,新註冊使用者只能通過web方式發郵件,

估計與在論壇註冊的新使用者在某個設定時間內發不了帖子是乙個道理。

或者: 

我寫了乙個發郵件的函式,包你滿意

unit unit1;

inte***ce

uses

windows, messages, sysutils, variants, classes,

graphics, controls, forms, dialogs, stdctrls, idcomponent,

idtcpconnection, idtcpclient, idmessageclient, idsmtp, idbasecomponent,

idmessage;

type

tform1 = class(tform)

button1: tbutton;

procedure button1click(sender: tobject);

private

public

end;

varform1: tform1;

implementation

type

tloginemailserver=record

smtphost:string;

smtpport:integer;

username:string;

password:string;

smtpauthtype:integer;

end;

function sendemail(posmtpserver:tloginemailserver;pobody:tstrings;psfromemial,

pstoemail,pssubject:string;pscontenttype:string;

cctoemail:string;poattachmentpath:tstrings):integer;

var 

loidmsgsend: tidmessage;

losmtp: tidsmtp;

i:integer; 

begin

result:=3;

loidmsgsend:=nil;

losmtp:=nil; 

try 

loidmsgsend:=tidmessage.create(nil);

losmtp:=tidsmtp.create(nil);

with loidmsgsend do

begin 

contenttype:=pscontenttype; 

from.text := psfromemial; 

replyto.emailaddresses := psfromemial; 

recipients.emailaddresses := pstoemail; 

cclist.emailaddresses:=cctoemail; 

subject := pssubject; 

priority := mphigh;

receiptrecipient.text := '';

body.assign(pobody); 

if assigned(poattachmentpath) then 

begin 

for i := 0 to poattachmentpath.count-1 do    

begin

tidattachment.create(loidmsgsend.messageparts,poattachmentpath.strings[i]);

end; 

end; 

end; 

with losmtp do                    

begin 

host :=posmtpserver.smtphost; 

port := posmtpserver.smtpport; 

if posmtpserver.smtpauthtype=1 then 

authenticationtype:=atlogin 

else 

authenticationtype:=atnone; 

username := posmtpserver.username; 

password := posmtpserver.password; 

try 

connect;    

send(loidmsgsend);       

except 

result:=2; 

exit; 

end; 

result:=0;

end;

finally

loidmsgsend.free;

losmtp.free; 

end;

end;

procedure tform1.button1click(sender: tobject);

begin

sendemail(.........);

end;

end.

procedure tform1.button1click(sender: tobject);

begin

tryidsmtp1.authenticationtype:=atlogin; //設定登陸型別

idsmtp1.username:=edit1.text; //設定登陸帳號

idsmtp1.password:=edit2.text; //設定登陸密碼

idsmtp1.host:=edit3.text; //設定smtp位址

idsmtp1.port:=strtoint(edit4.text); //設定埠   必須轉化為整型

idsmtp1.connect;   //開始連線伺服器

except

showmessage('連線失敗,請重試!');

exit; //連線失敗 的話 退出該執行過程 

end; 

idmessage1.body.clear;   //先清空上次傳送的內容 

idmessage1.subject:=edit5.text;   //設定郵件傳送的標題 

idmessage1.body.assign(memo1.lines);   //設定郵件傳送的主體 

idmessage1.from.address:=edit6.text; //設定郵件的發件人   也就是說該郵件來自什麼地方 

idmessage1.recipients.emailaddresses:=edit7.text;   //收件人的位址 

try 

idsmtp1.send(idmessage1); 

showmessage('郵件傳送成功!');

except 

showmessage('郵件傳送失敗!');

end; 

end;   

--咳,又忘了出處。。老早以前儲存的。。。抱歉抱歉

用CDONTS NewMail發郵件

last updated by recon on 05 14 2001 on error resume next 利用cdonts元件在win2k上傳送郵件 dim objmail 普通郵件 無主體 set objmail server.createobject cdonts.newmail obj...

用python發郵件

python內建對smtp支援,可傳送純文字,html以及帶附件的郵件以下是傳送純文字郵件的 把email模組和smtplib模組引入進來,使用mimetext構造了郵件內容 from email import encoders from email.mime.text import mimetex...

linux用shell發郵件

方法一 簡單郵件傳送 echo hello world mail s subject t yanggang ithomer.com,yanggang 2050 163.com a from 463103470 qq.com 效果截圖 方法二 文字格式傳送郵件 python view plain co...