【问题标题】:Save Session in SMTP protocol and JavaMail api在 SMTP 协议和 JavaMail api 中保存会话
【发布时间】:2021-05-20 04:58:55
【问题描述】:

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

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

它只是一个实现还是在SMTP协议中声明是这样的?

我的代码:

我正在使用 JavaMail API 来启动一个 SMTP Session 并发送一封电子邮件,如下:

//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) 中(在 JavaMail api 中),有以下实现:

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

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

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

【问题讨论】:

    标签: java smtp gmail-api smtpclient smtp-auth


    【解决方案1】:

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

    Transport.send(message);  
    

    用途:

    Transport transport = message.getTransport(address);
    try{
       transport.connect();
    }catch (Exception e){
       //already connected
    }
    transport.sendMessage(message);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多