public void sendMailWithAttachment(String from,List<String> toList, String subject ,String body,String filePath) throws MessagingException{
Properties props = new Properties();
props.put("mail.smtp.host", "exchange.company.com");//
props.put("mail.smtp.user", "sender user name");
props.put("mail.smtp.password", "sender password"); //mail.smtp.ssl.enable
props.put("mail.smtp.ssl.enable", "false");
Session session = Session.getDefaultInstance(props, null);
session.setDebug(true);
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
InternetAddress ia=null;
List<InternetAddress> addresees=new ArrayList<InternetAddress>();
for(String to :toList ){
ia=new InternetAddress(to);
addresees.add(ia);
}
InternetAddress[] array = new InternetAddress[addresees.size()];
array=addresees.toArray(array);
//msg.setRecipients(Message.RecipientType.TO, new InternetAddress[]{new InternetAddress("melsonbati@daralshifa.com")});
msg.setRecipients(Message.RecipientType.TO,array);
msg.setSubject(subject);
msg.setText(body);
MimeBodyPart attachmentBodyPart = new MimeBodyPart();
File file=new File(filePath);
DataSource source = new FileDataSource(file);
attachmentBodyPart.setDataHandler(new DataHandler(source));
attachmentBodyPart.setFileName(file.getName());
Multipart multipart = new MimeMultipart();
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(body);
messageBodyPart.setContent(body, "text/html");
multipart.addBodyPart(messageBodyPart);
multipart.addBodyPart(attachmentBodyPart);
msg.setContent(multipart);
Transport.send(msg);
}
Properties props = new Properties();
props.put("mail.smtp.host", "exchange.company.com");//
props.put("mail.smtp.user", "sender user name");
props.put("mail.smtp.password", "sender password"); //mail.smtp.ssl.enable
props.put("mail.smtp.ssl.enable", "false");
Session session = Session.getDefaultInstance(props, null);
session.setDebug(true);
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
InternetAddress ia=null;
List<InternetAddress> addresees=new ArrayList<InternetAddress>();
for(String to :toList ){
ia=new InternetAddress(to);
addresees.add(ia);
}
InternetAddress[] array = new InternetAddress[addresees.size()];
array=addresees.toArray(array);
//msg.setRecipients(Message.RecipientType.TO, new InternetAddress[]{new InternetAddress("melsonbati@daralshifa.com")});
msg.setRecipients(Message.RecipientType.TO,array);
msg.setSubject(subject);
msg.setText(body);
MimeBodyPart attachmentBodyPart = new MimeBodyPart();
File file=new File(filePath);
DataSource source = new FileDataSource(file);
attachmentBodyPart.setDataHandler(new DataHandler(source));
attachmentBodyPart.setFileName(file.getName());
Multipart multipart = new MimeMultipart();
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(body);
messageBodyPart.setContent(body, "text/html");
multipart.addBodyPart(messageBodyPart);
multipart.addBodyPart(attachmentBodyPart);
msg.setContent(multipart);
Transport.send(msg);
}
Comments
Post a Comment