Thu Apr 25, 2013 11:00 pm
public void sendMail(Mail mail) {
Message msg = new MimeMessage(session);
try {
if (mail.getSender() != null) {
msg.setFrom(toAddress(mail.getSender()));
}
} catch (MessagingException messagingException) {
// Do something
}
try {
msg.setRecipients(Message.RecipientType.TO, toAddresses(mail
.getToRecipient()));
} catch (MessagingException messagingException) {
// Do something
}
try {
msg.setRecipients(Message.RecipientType.CC, toAddresses(mail
.getCcRecipient()));
} catch (MessagingException messagingException) {
// Do something
}
try {
msg.setRecipients(Message.RecipientType.BCC, toAddresses(mail
.getBccRecipient()));
} catch (MessagingException messagingException) {
// Do something
}
Multipart messageParts = new MimeMultipart();
MimeBodyPart textPart = new MimeBodyPart();
try {
msg.setSubject(mail.getSubject());
} catch (MessagingException messagingException) {
// Do something
}
try {
textPart.setText(mail.getMessage(), "UTF8");
} catch (MessagingException messagingException) {
// Do somthing
}
try {
messageParts.addBodyPart(textPart);
} catch (MessagingException messagingException) {
// Do something
}
if (mail.getAttachments() != null) {
for (int i = 0; i < mail.getAttachments().length; i++) {
File file = mail.getAttachments()[i];
if (!file.exists()) {
// Do Something
}
DataSource dataSource = new FileDataSource(file);
MimeBodyPart attachmentPart = new MimeBodyPart();
try {
attachmentPart.setDataHandler(new DataHandler(dataSource));
} catch (MessagingException messagingException) {
// Do Something
}
try {
attachmentPart.setFileName(file.getName());
} catch (MessagingException messagingException) {
// Do Something
}
try {
messageParts.addBodyPart(attachmentPart);
} catch (MessagingException messagingException) {
// Do Something
}
}
}
try {
msg.setContent(messageParts);
} catch (MessagingException messagingException) {
// Do Something
}
try {
Transport.send(msg);
} catch (MessagingException messagingException) {
// Do Something
}
}
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.