lundi 9 mars 2015

Sending emails with JAVAMAIL without authenticating every time



Hello I'm working on an admin dashboard with spring MVC, and it's the first time I use javamail.


My question is: Is there any way I can authenticate to the session at first and then use this instance to send more emails without authentication? (while the session is still available).


With the method I'm using I need to save the sender's email and password as "clear text" to authenticate every time.


So anyone please have any suggestions on how to manage session and how to store email and password to authenticate when I need to send/receive emails?


Thank you!


So here is the code I'm using to send the email:



@Override
public boolean sendEmail(EmailTMP e) throws UnsupportedEncodingException {
String host = "smtp.gmail.com";
final String password=e.getSender().getInboxPassword();
final String from = e.getSender().getInboxEmail();
String name=e.getSender().getNom();
String toAddress = e.getReceiver();
String subject=e.getSubject();
String textContent=e.getContent();

Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");

<!-- I need to replace the code below to automatically authenticate and get the current session-->
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(from, password);
}
});

try {

Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(toAddress));
message.setSubject(subject);


message.setText(textContent);
InternetAddress fromAddress=new InternetAddress(from, name);
message.setFrom(fromAddress);
Transport.send(message);

return true;

} catch (MessagingException c) {
return false;
}

}



Aucun commentaire:

Enregistrer un commentaire