javaMail的Transport.send方法没有像transport.connect一样设置用户,密码怎么能发送的

还有Session.getDefaultInstance(props, “”)的第二个参数设置用户和transport.connect设置用户一样吗
2026年09月26日 00:02
有1个网友回答
网友(1):

有些邮件服务器不需要验证用户名密码,一般需要验证的是这样:

MyAuthenticator authenticator = null;    
      Properties pro = mailInfo.getProperties();   
      if (mailInfo.isValidate()) {    
      // 如果需要身份认证,则创建一个密码验证器    
        authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());    
      }   
      // 根据邮件会话属性和密码验证器构造一个发送邮件的session    
      Session sendMailSession = Session.getDefaultInstance(pro,authenticator);