Switch to full style
General Java code examples
Post a reply

Send Email Recipient CC,BCC MimeMultipart, Attachments

Thu Apr 25, 2013 11:00 pm

java code
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
}

}




Post a reply
  Related Posts  to : Send Email Recipient CC,BCC MimeMultipart, Attachments
 Send an email in asp.net     -  
 Send email in asp     -  
 Send email link example.     -  
 send email from ASP using CDOSYS     -  
 Send Email from a PHP Script Example     -  
 send confirmation link to email     -  
 i want to send conformation email with user details on the     -  
 background attachments     -  
 validate email address in asp     -  

Topic Tags

Java Email