保存会话在SMTP协议和JavaMail api



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

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

它只是一个实现吗还是在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)(在JavaMail api中)中,有以下实现:

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

我可以使用transport.SendMessage(message)的实现而不使用transport.CloseConnection()吗?因此我将保存会话并继续使用它。

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

看起来还行。而不是使用

Transport.send(message);  

使用:

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

最新更新