1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185:
| unit SMTPSendMail;
interface
uses Classes, SysUtils, IdMessage, IdSMTP, IdURI, IdTCPClient, IdDNSResolver;
procedure SendMail(const SMTPServer, EMailAddress, From, Subject, MailText : string); overload; procedure SendMail(const SMTPServer, EMailAddress, From, Subject, MailText, Attachment : string; Username : string=''; Password : string=''); overload; procedure ParseEmail(EmailAddress : string; var Username, Domain : string); function ValidateEmailAddress(const EMailAddress : string; const DNSServer : string='') : boolean;
implementation
function CharPos(str : string; c : char) : integer; var i, iLen : integer; begin result := 0; iLen := Length(str); for i := 1 to iLen do if str[i]=c then begin result := i; break; end; end;
procedure ParseEmail(EmailAddress : string; var Username, Domain : string); begin Username := Copy(EmailAddress, 1, CharPos(EmailAddress, '@')-1); Domain := Copy(EmailAddress, CharPos(EmailAddress, '@')+1,9999999); end;
procedure SendMail(const SMTPServer, EMailAddress, From, Subject, MailText, Attachment : String; Username : string=''; Password : string=''); var SMTP: TIdSMTP; Msg: TIdMessage; begin SMTP := TIdSMTP.Create(nil); try SMTP.Username := Username; SMTP.Password := Password;
Msg := TIdMessage.Create(SMTP); Msg.AttachmentEncoding := 'MIME'; Msg.ContentType := 'text/plain'; msg.Encoding := meMIME; msg.NoEncode := False; Msg.NoDecode := False;
Msg.IsEncoded := True; Msg.Subject := Subject; with Msg.Recipients.Add do begin Address := EMailAddress; Name := EMailAddress; end; Msg.From.Address := From; Msg.From.Name := Copy(From,1,pos('@',From)-1); with Msg.ReplyTo.Add do begin Address := From; Name := Copy(From,1,pos('@',From)-1); end;
Msg.Body.Text := MailText; if Attachment <> '' then TIdAttachment.Create(Msg.MessageParts, Attachment);
SMTP.Host := SMTPServer; SMTP.Connect; try; SMTP.Send(Msg); finally SMTP.Disconnect; end; finally SMTP.Free; end; end;
procedure SendMail(const SMTPServer, EMailAddress, From, Subject, MailText : string); begin SendMail(SMTPServer, EMailAddress, From, Subject, MailText, '','',''); end;
function ValidateEmailAddress(const EMailAddress : string; const DNSServer : string='') : boolean; var iPos, iResult : integer; dns : TIdDNSResolver; sResult, sUser, sDomain : string; i : integer; bFound : boolean; begin Result := False; bFound := False; iPos := CharPos(EMailAddress,'@'); if iPos > 0 then begin iPos := CharPos(EMailAddress,'.'); if iPos > 0 then begin if DNSServer = '' then begin Result := True; exit; end; ParseEmail(EMailAddress, sUser, sDomain); dns := TIdDNSResolver.Create(nil); try dns.AllowRecursiveQueries := True; dns.Host := DNSServer; dns.QueryRecords := [qtMX]; dns.ReceiveTimeout := 1000; dns.Resolve(sDomain); for i := 0 to dns.QueryResult.Count-1 do begin if dns.QueryResult[i].RecType = qtMX then begin sDomain := TMXRecord(dns.QueryResult[i]).ExchangeServer; bFound := True; break; end; end;
if bFound then begin with TIdTCPClient.Create(nil) do try Port := 25; Host := sDomain; Connect; WriteLn('HELO '+sDomain); sResult := ReadLn; WriteLn('MAIL FROM:<'+EmailAddress+'>'); sResult := ReadLn; WriteLn('RCPT TO:<'+EmailAddress+'>'); sResult := ReadLn; iPos := CharPos(sResult,' '); if iPos > 0 then sResult := Copy(sResult,1,iPos-1); iPos := CharPos(sResult,'-'); if iPos > 0 then sResult := Copy(sResult,1,iPos-1); iResult := StrToIntDef(sResult,0); Result := iResult = 250; finally free; end; end; finally dns.Free; end; end; end; end;
end. |