在 SMTP 协议和 JavaMail api 中保存会话

问题描述

我开发了一个代表用户发送电子邮件的 Java 应用程序。

为了避免保存用户密码,我想存储邮件会话。 但是,我看到在 SMTP 协议的 Java 的 Mail api 中,实现对发送的每条消息进行身份验证!

它只是一个实现还是在SMTP协议中声明如此?

我的代码

我正在使用 JavaMail API 启动 SMTP 会话并发送电子邮件,如下所示:

//Set up headers
String from,to...

//Set up authentication
  Properties props = new Properties();  
   props.put("mail.smtp.host",host);  
   props.put("mail.smtp.auth","true");  

  Session session = Session.getDefaultInstance(props,new javax.mail.Authenticator() {  
      protected PasswordAuthentication getpasswordAuthentication() {  
    return new PasswordAuthentication(user,password);  
      }  
    });  

//Set up message
MimeMessage message = new MimeMessage(session); 
...

//Send message
  Transport.send(message);  

在 Transport.send(message) (Inside JavaMail api)中,有如下实现:

try{
  this.Connect()
  this.SendMessage(message)
  this.CloseConnection()
}

我可以在没有transport.CloseConnection()的情况下使用transport.SendMessage(message)的实现吗? 因此我将保存会话并继续使用它。

SMTP 会支持吗?会议将保留多长时间?

解决方法

好吧,它似乎工作正常。 而不是使用

3.6.0

使用:

Transport.send(message);