Hi!
You could implement this code snippet, which is sending mail through google smtp.
private class JFXMail implements Callable<Boolean> {
private String smtpserver;
private String from;
private String to;
private String subj;
private String text;
public JFXMail(String smtpserver, String from, String to, String subj, String text){
this.smtpserver = smtpserver;
this.from = from;
this.to = to;
this.subj = subj;
this.text = text;
}
private Session getSession() {
Authenticator authenticator = new Authenticator();
Properties properties = new Properties();
properties.setProperty("mail.smtp.submitter", authenticator.getPasswordAuthentication().getUserName());
properties.setProperty("mail.smtp.auth", "true");
properties.setProperty("mail.smtp.starttls.enable","true");
properties.setProperty("mail.smtp.socketFactory.port", "465");
properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
properties.setProperty("mail.smtp.socketFactory.fallback", "false");
properties.setProperty("mail.smtp.host", smtpserver);
properties.setProperty("mail.smtp.port", "465");
return Session.getInstance(properties, authenticator);
}
private class Authenticator extends javax.mail.Authenticator {
private PasswordAuthentication authentication;
public Authenticator() {
String username = "[email protected]";
String password = "xypassword";
authentication = new PasswordAuthentication(username, password);
}
protected PasswordAuthentication getPasswordAuthentication() {
return authentication;
}
}
public Boolean call() throws Exception {
Boolean retVal = false;
try {
Message message = new MimeMessage(getSession());
message.addRecipient(RecipientType.TO, new InternetAddress(to));
message.addFrom(new InternetAddress[] { new InternetAddress(from) });
message.setSubject(subj);
message.setContent(text, "text/plain");
Transport.send(message);
retVal = true;
} catch (Exception e) {
retVal = false;
throw new JFException(e);
}
return retVal;
}
}
Call JFXMail from your strategy like this:
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFXMail mail = new JFXMail(smtpserver, from, to, subj, mess);
context.executeTask(mail);
}
});
regards,
Kirilla