credo esistano vari modi te ne posto 1, anche se non è stilisticamente perfetto.. se la mail non viene inviata va in exception, se va tutto bene restituisce true...
codice:
public boolean sendMail(String from, String to, String subject, String body) throws AddressException, MessagingException{
boolean returnValue = false;
Properties props = System.getProperties();
props.put("mail.smtp.host", smtpServer);
Session session = Session.getDefaultInstance(props, null);
// -- Create a new message --
Message msg = new MimeMessage(session);
// -- Set the FROM and TO fields --
msg.setFrom(new InternetAddress(from));
msg.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to, false));
// -- Set the subject and body text --
msg.setSubject(subject);
String mimeEncoding = MimeUtility.mimeCharset("<Java Char encoding name>");
//msg.setContent(body, "text/html");
String charset = "utf-8";
msg.setContent(body, "text/plain; charset=" + charset);
// -- Set some other header information --
msg.setHeader("X-Mailer", "LOTONtechEmail");
msg.setSentDate(new Date());
// -- Send the message --
Transport.send(msg);
returnValue = true;
return returnValue;
}