1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
|
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.SendFailedException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public static boolean envoyerMailWithAttachements(String from, String to[], String toCC[], String objet, String text, String[] nomFichiers) throws ApplicationException
{
boolean result = false;
boolean retry = false;
String serveur_mail = "DOMBST01";
do
{
try
{
Properties prop = System.getProperties();
prop.put("mail.smtp.host", serveur_mail);
// Cf connexion au serveur de mail
Session session = Session.getDefaultInstance(prop, null);
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
if (to != null)
{
InternetAddress[] adressesTo = new InternetAddress[to.length];
for (int i = 0; i < to.length; i++)
{
adressesTo[i] = new InternetAddress(to[i]);
}
message.setRecipients(Message.RecipientType.TO, adressesTo);
}
if (toCC != null)
{
InternetAddress[] adressesToCC = new InternetAddress[toCC.length];
for (int i = 0; i < toCC.length; i++)
{
adressesToCC[i] = new InternetAddress(toCC[i]);
}
message.setRecipients(Message.RecipientType.CC, adressesToCC);
}
message.setSubject(objet);
message.setHeader("X-Mailer", "MAILER_VERSION");
message.setSentDate(new Date());
Multipart multipart = new MimeMultipart();
// Corps du mail
MimeBodyPart messageBodyPart = new MimeBodyPart();
//messageBodyPart.setText(text);
messageBodyPart.setText(text, "UTF-8");
multipart.addBodyPart(messageBodyPart);
if (nomFichiers != null)
{
//Piece jointes
for (int i = 0; i < nomFichiers.length; i++)
{
MimeBodyPart piecejointeBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(nomFichiers[i]);
piecejointeBodyPart.setDataHandler(new DataHandler(source));
piecejointeBodyPart.setFileName(nomFichiers[i]);
multipart.addBodyPart(piecejointeBodyPart);
}
}
// Ajout des éléments au mail
message.setContent(multipart);
Transport.send(message);
result = true;
}
catch (SendFailedException e)
{
if (!retry)
{
retry = true;
serveur_mail = "DOMBST02";
}
else
{
retry = false;
throw new ApplicationException("HAND-MH01", "error.beanmail1", e);
}
}
catch (AddressException e)
{
throw new ApplicationException("HAND-MH02", "error.beanmail0", e);
}
catch (MessagingException e)
{
throw new ApplicationException("HAND-MH03", "error.beanmail0", e);
}
}
while (retry);
return result;
} |
Partager