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
|
public void sendMail(File file)
{
Properties props = System.getProperties();
String from = "name@mydomain.com";
props.put("mail.smtp.host", "myhost");
props.put("mail.smtp.port", "25");
Session session = Session.getInstance(props, null);
try{
MimeMessage message = new MimeMessage(session);
message.addHeader("MIME-Version", "1.0");
message.addHeader("Content-Transfer-Encoding", "BASE64");
message.addHeader("Content-Type", "multipart/mixed");
message.setFrom(new InternetAddress(from));
message.setSentDate(new Date());
String[] to = {"xx@yahoo.fr","yy@gmail.com"};
InternetAddress[] toAddress = new InternetAddress[to.length];
for (int i = 0; i < to.length; i++)
toAddress[i] = new InternetAddress(to[i]);
message.setRecipients(Message.RecipientType.TO, toAddress);
message.setSubject("send mail");
MimeMultipart multipart = new MimeMultipart();
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(bean.getBody()+"<br>","text/html");
multipart.addBodyPart(messageBodyPart);
MimeBodyPart messageBodyPart2 = new MimeBodyPart();
FileDataSource fileDataSource = new FileDataSource(file) {
@Override
public String getContentType() {
return "application/octet-stream";
}
};
messageBodyPart2.setDataHandler(new DataHandler(fileDataSource));
messageBodyPart2.setFileName(file.getName());
multipart.addBodyPart(messageBodyPart2);
message.setContent(multipart);
Transport.send(message);
System.out.println("sending message");
}
catch (MessagingException mex)
{
mex.printStackTrace();
Exception ex = null;
if ((ex = mex.getNextException()) != null) {
ex.printStackTrace();
}
}
} |
Partager