开发者

Is there an accepted syntax for SMTP server? (and how to parse it)

I've seen th开发者_开发问答ings like:

user:password@smtpserver:port

In the past, but I'm not sure if the some library parsed that to build a properties to create a session or is there some sort of accepted format.


While there is a SMTP URL Scheme, I have never seen anyone use it. In practice, most applications provide four separate fields for host, port, user name and password. But if you really need to put those four components into a single string, the example you provided is probably the best-known format for something like this.


Using an URI for specifying a network resource, such as an SMTP server is probably the cloeset thing to a "accepted" format you'd see, an SMTP URI, would be something like smtp://user:host@example.com:port or perhaps just smtp://example.com . You'd use a generic URI parsing library to extract the various components.

There's also an old RFC draft for SMTP URLs


I think that would work.

I would like to add as answer, how I'm using java.net.URI class get information from that URI.

class Demo { 
  public static void main( String ... args ) throws Exception  { 
    System.out.println( inspect( new URI("smtp://user:port@host:25")));
  }
  // invoke all the getXyz methods on object and construct 
  // a string with the result. 
  private static String inspect( Object o ) throws Exception  { 
    StringBuilder builder = new StringBuilder();
    for( Method m : o.getClass().getMethods() ) { 
      String name = m.getName();
      if( name.startsWith("get")) { 
        builder.append( name )
        .append(" = " )
        .append( m.invoke( o ) )
        .append( "\n" );
      }
    }
    return builder.toString();
  }
}

Output

getAuthority = user:port@host:25
getFragment = null
getPath =
getQuery = null
getScheme = smtp
getHost = host
getPort = 25
getUserInfo = user:port
getRawAuthority = user:port@host:25
getRawFragment = null
getRawPath =
getRawQuery = null
getRawSchemeSpecificPart = //user:port@host:25
getRawUserInfo = user:port
getSchemeSpecificPart = //user:port@host:25
getClass = class java.net.URI


Was configuring node app and had a hard time getting it.

smtp://username:password@smtphost:port

The funny part was my password was having an '@' so it was presuming the hostname has started. The username also had an '@' server but it was good as it was separated by the colon ":"

Hope this helps. Cheers

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜