| 
| 
 | Вопрос # 1 330/ вопрос открыт / | 
 |  Здравствуйте!Есть ли предопределенные обработчики исключительных ситуаций при отправки письма и где их можно увидеть?
 Например при отправки письма на несуществующий адрес
 у меня в программе генерится просто Exception а мне нужно что бы я мог распознать что ошибка именно из за не верного адреса. Спасибо.
 
|  |   Вопрос задал: Сидаровский Георгий (статус: Посетитель)Вопрос отправлен: 11 февраля 2008, 13:26
 Состояние вопроса: открыт, ответов: 1.
 |  Ответ #1. Отвечает эксперт: Feniks Здравствуйте, Сидаровский Георгий!А вы уверены, что это у вас именно из-за не существующего адреса ?
 К сожалению, вы не указали какими именно компонентами пользуетесь, какая версия Делфи...
 Поэтому, смею предложить вам вот этот пример в Приложении. Есть, правда, еще примеры, например отправка через MAPI, если нужны, пишите в личку.
 Приложение:Переключить в обычный режим unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,StdCtrls, ExtCtrls, ComCtrls, Psock, NMsmtp;    typeTForm1 = class(TForm)Memo: TRichEdit;Panel1: TPanel;SMTP: TNMSMTP;Panel2: TPanel;FromAddress: TEdit;predefined: TLabel;FromName: TEdit;Subject: TEdit;LocalProgram: TEdit;ReplyTo: TEdit;islog: TCheckBox;Host: TEdit;Port: TEdit;userid: TEdit;Button1: TButton;procedure Button1Click(Sender: TObject);privateProcedure CleanContext;procedure PerformConnection;procedure AddMessage(msg:string; color:integer);procedure log(inpt :string);Procedure SetSMTP;publicfunction SendEmail(_to, cc, bcc, Subject, body, attachment:string; HTMLFormat:boolean):boolean;end; var Form1: TForm1; implementation{$R *.DFM} Procedure TForm1.SetSMTP;beginSMTP.Host:=Host.Text;SMTP.Port:=strtoint(Port.text);SMTP.UserID:=userid.text;end; Function GetEmailDateTime:string;var tz:_time_Zone_information;   s:string;beginGetTimeZoneInformation(tz);if (tz.Bias*100 div 60)<1000 thens:=format(' -0%d',[tz.Bias*100 div 60])elses:=format(' -%d',[tz.Bias*100 div 60]);result:=formatdatetime('ddd, dd mmm yyyy hh:nn:ss',now)+s;end; Procedure TForm1.CleanContext;{set default values, some of them comes from "Setup" form}beginSMTP.PostMessage.FromAddress:=FromAddress.text;SMTP.PostMessage.FromName:=FromName.text;SMTP.PostMessage.ToAddress.Clear;SMTP.PostMessage.ToCarbonCopy.clear;SMTP.PostMessage.ToBlindCarbonCopy.clear;SMTP.PostMessage.Body.clear;SMTP.PostMessage.Attachments.clear;SMTP.PostMessage.Subject:=Subject.text;SMTP.PostMessage.LocalProgram:=LocalProgram.text;(*Mon, 27 Nov 2000 12:37:46 -0700*)SMTP.PostMessage.Date:=GetEmailDateTime;SMTP.PostMessage.ReplyTo:=ReplyTo.Text;end; procedure TForm1.log(inpt :string);var outf:textfile;begin {writing in the log file}if not islog.checked then exit;assignfile(outf, changefileext(paramstr(0), '.log'));if fileexists(changefileext(paramstr(0), '.log')) thenappend(outf)elserewrite(outf);writeln(outf, datetimetostr(now)+'|'+inpt);closefile(outf);end; procedure TForm1.AddMessage(msg:string; color:integer);begin {showing in the memo field progress...}while memo.lines.Count>2000 do memo.lines.Delete(0);memo.sellength:=0;memo.selstart:=length(memo.text);memo.selattributes.Color:=Color;memo.seltext:=#13#10+DateTimeTostr(now)+' '+msg;memo.perform($00B7,0,0);Application.ProcessMessages;if color<>clRed then log(DateTimeTostr(now)+' '+msg) else log('Error: '+DateTimeTostr(now)+'
'+msg);end; procedure TForm1.PerformConnection;beginwhile (not SMTP.connected) dobegin   SetSMTP;   AddMessage('Connecting to SMTP',clBlue);   application.processmessages;   try     SMTP.Connect;     AddMessage('No Errors',clBlue);   except     on e:exception do AddMessage('Error conection: '+e.message,clBlue);   end;end;end; Function TForm1.SendEmail(_to, cc, bcc, Subject, body,  attachment:string;
HTMLFormat:boolean):boolean;beginPerformConnection;result:=true;CleanContext;tryif (attachment<>'') and (not Fileexists(attachment)) then   begin     AddMessage('Attachment is not ready yet ('+ attachment+') ', clNavy);     sleep(300);     result:=false;     exit;   end;SMTP.PostMessage.ToAddress.text:=StringReplace(_to, ';',#13#10, [rfReplaceAll, rfIgnoreCase]);if cc<>'' then SMTP.PostMessage.ToCarbonCopy.text:=StringReplace(cc, ';',#13#10,
[rfReplaceAll, rfIgnoreCase]);if bcc<>'' then SMTP.PostMessage.ToBlindCarbonCopy.text:=StringReplace(bcc, ';',#13#10,
[rfReplaceAll, rfIgnoreCase]);if Subject<>'' then SMTP.PostMessage.Subject:=Subject;if HTMLFormat then SMTP.SubType:=mtPlain else SMTP.SubType:=mtHtml;SMTP.PostMessage.Body.Text:=Body;if attachment<>'' then SMTP.PostMessage.Attachments.add(attachment);AddMessage('Sending to '+ _to, clGreen);SMTP.SendMail;AddMessage('Complete.'+#13#10, clGreen);excepton e:sysutils.exception do   begin     AddMessage(e.message, clRed);     result:=false;   end;end;end; procedure TForm1.Button1Click(Sender: TObject);beginSendEmail('somewhere@somedomain.ru', '', '', 'test', 'body',  '', False);end; end.
|  | Ответ отправил: Feniks (статус: Бакалавр)Время отправки: 11 февраля 2008, 19:16
 
 |  
 Мини-форум вопросаМини-форум пуст. Чтобы оставлять сообщения в мини-форумах, Вы должны авторизироваться на сайте. |