我开发了一个代表用户发送电子邮件的Java应用程序。
为了避免保存用户密码,我希望存储邮件会话。但是,我看到在Java的SMTP协议的Mail中,实现对发送的每一条消息进行身份验证!
它只是一个implementation,还是在SMTP协议中声明是这样?
我的代码:
我正在使用SMTP API启动JavaMail会话并发送电子邮件,如下所示:
//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(消息)(在JavaMail api中)中,有以下实现:
try{
this.Connect()
this.SendMessage(message)
this.CloseConnection()
}我可以在没有transport.SendMessage()的情况下使用transport.CloseConnection(消息)的实现吗?因此,我将保存会话并继续使用它。
SMTP会支持它吗?会议将保留多少时间?
发布于 2021-02-20 11:32:40
好像挺好的。而不是使用
Transport.send(message); 使用:
Transport transport = message.getTransport(address);
try{
transport.connect();
}catch (Exception e){
//already connected
}
transport.sendMessage(message);https://stackoverflow.com/questions/66239672
复制相似问题