+procedure ParseIPv6Address(S: string; var Address: string; var Port: word);
+{ IPv6 addresses can be supplied in the following formats:
+ [<IPv6 address>]:<port> e.g. [::1]:25
+ or
+ <hostname>:<port> e.g. mail.example.com:25 }
+var SPort: string; c: integer;
+begin
+ if S[1] = '[' then begin
+ { Guess format is "[<IPv6 address>]:<port>". }
+ c:= pos(']', S);
+ if c > 1 then begin
+ Address:= Copy(S, 2, c - 2);
+ if c = Length(S) then begin
+ { There is no port to extract. }
+ Port:= STANDARD_SMTP_PORT;
+ end
+ else begin
+ { The closing bracket should be followed by a colon. }
+ if S[c+1] = ':' then
+ { Extract port number. }
+ Port:= StrToIntDef(Copy(S, c+2, Length(S) - (c+1)), 0)
+ else
+ { Invalid format. }
+ Port:= 0;
+ end;
+ end
+ else begin
+ { Format is incorrect, return invalid data. }
+ Address:= '';
+ Port:= 0;
+ end;
+ end
+ else begin
+ { Guess format is "<hostname>:<port>". }
+ SplitParameters(S, Address, SPort, ':');
+ if (pos(':', Address) = 0) and (pos(':', SPort) = 0) then begin
+ { Format seems correct. }
+ if SPort = '' then Port:= STANDARD_SMTP_PORT
+ else Port:= StrToIntDef(SPort, 0);
+ end
+ else begin
+ { Format is incorrect, return invalid data. }
+ Address:= '';
+ Port:= 0;
+ end;
+ end;
+end;
+