codice:
/******************************************************/
/** CLASSE PER LA GESTIONE DELLE MAIL - VERSIONE 7.0 **/
/******************************************************/
import com.sun.mail.smtp.SMTPMessage;
import java.util.*;
import javax.activation.*;
import javax.mail.*;
import javax.mail.Message.RecipientType;
import javax.mail.internet.*;
public class MailHelper{
private String smtpServer = "smtp.miosmtp.it";
/**
* METODO COSTRUTTORE DI DEFAULT
*
*/
public MailHelper(){
}
/**
* METODO COSTRUTTORE PER ASSEGNAZIONE SMPT
*
* @param smtpServer
* smtpServer da assegnare alla classe
* @return
*/
public MailHelper(String smtpServer){
this.smtpServer = smtpServer;
}
/**
* METODO PER INOLTRO MAIL DI TESTO
*
* @param from
* mittente della mail
* @param to
* destinatari della mail
* @param subject
* oggetto della mail
* @param body
* testo semplice non formattato
* @return
* @throws AddressException
* @throws MessagingException
*/
public boolean sendMail(String from, String[] to, String [] toBCC, 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));
setTo(to, msg, Message.RecipientType.TO);
setTo(toBCC, msg, Message.RecipientType.BCC);
// -- 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;
}
/**
* METODO PER INOLTRO MAIL IN FORMATO HTML A PIù DESTINATARI
*
* @param from
* mittente della mail
* @param to
* destinatari della mail
* @param subject
* oggetto della mail
* @param body
* testo formattato HTML
* @return
* @throws MessagingException
*/
public boolean sendHtmlMail(String from, String[] to, String[] toBCC, String subject, String body) throws MessagingException{
boolean returnValue = false;
Properties props2 = System.getProperties();
props2.put("mail.smtp.host", smtpServer);
Session session = Session.getDefaultInstance(props2, null);
SMTPMessage msg = new SMTPMessage(session);
msg.setHeader("X-Mailer", "LOTONtechEmail");
msg.setSentDate(new Date());
msg.setFrom(new InternetAddress(from));
msg.setSubmitter(from);
msg.setSubject(subject);
setTo(to, msg, Message.RecipientType.TO);
setTo(toBCC, msg, Message.RecipientType.BCC);
String charset = "utf-8";
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(body, "text/html; charset=" + charset);
MimeMultipart multipart = new MimeMultipart("related");
multipart.addBodyPart(messageBodyPart);
msg.setContent(multipart);
Transport.send(msg);
returnValue = true;
return returnValue;
}
/**
* METODO PER INOLTRO MAIL SMTP CON PIù ALLEGATI
*
* @param from
* mittente della mail
* @param to
* destinatari della mail
* @param subject
* oggetto della mail
* @param body
* testo semplice non formattato
* @param allegati
* array di stringhe path allegati
* @return
* @throws MessagingException
*/
public boolean sendSMTPMail(String from, String[] to, String[] toBCC, String subject, String body, String [] allegati) throws MessagingException{
boolean returnValue = false;
String fromName = from;
Properties props = System.getProperties();
props.put("mail.smtp.host", smtpServer);
Session session = Session.getDefaultInstance(props, null);
SMTPMessage msg = new SMTPMessage(session);
msg.setHeader("X-Mailer", "LOTONtechEmail");
msg.setSentDate(new Date());
msg.setFrom(new InternetAddress(fromName));
msg.setSubmitter(fromName);
msg.setSubject(subject);
setTo(to, msg, Message.RecipientType.TO);
setTo(toBCC, msg, Message.RecipientType.BCC);
String charset = "utf-8";
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(body, "text/html; charset=" + charset);
MimeMultipart multipart = new MimeMultipart("related");
multipart.addBodyPart(messageBodyPart);
// ciclo per ogli allegato
for(int i = 0;i<allegati.length;i++){
if (!allegati[i].equals("")){
messageBodyPart = new MimeBodyPart();
DataSource fds = new FileDataSource(allegati[i]);
messageBodyPart.setFileName(fds.getName());
messageBodyPart.setDataHandler(new DataHandler(fds));
messageBodyPart.setHeader("Content-ID", "<memememe>");
multipart.addBodyPart(messageBodyPart);
}
} // fine ciclo
msg.setContent(multipart);
Transport.send(msg);
returnValue = true;
return returnValue;
}
/**
* METODO PRIVATO PER CONTROLLI E ASSEGNAZIONI CAMPI DESTINATARIO
*
* @param to
* destinatari della mail
* @param msg
* messaggio a cui aggiungere i destinatari
* @param rt
* tipo di destinatario
* @throws MessagingException
*/
private void setTo(String[] to, Message msg, RecipientType rt) throws MessagingException{
if(to != null){
boolean addTo = false;
InternetAddress[] addressTo = new InternetAddress[to.length];
for (int i = 0; i < to.length; i++)
{
if(to[i]!=null && !"".equals(to[i])){
addressTo[i] = new InternetAddress(to[i]);
addTo = true;
}
}
if(addTo){
msg.setRecipients(rt, addressTo);
}
}
}
}