Here you can find the source of executeSMTPSend(String fromEmailAddress, List
public static void executeSMTPSend(String fromEmailAddress, List<String> toEmailAddresses, String subject, String body, Transport transport, File... fileAttachments) throws MessagingException, UnsupportedEncodingException, IOException
//package com.java2s; //License from project: Creative Commons License import java.io.File; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.Date; import java.util.List; import javax.mail.Address; import javax.mail.MessagingException; import javax.mail.Multipart; 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.MimeMessage.RecipientType; import javax.mail.internet.MimeMultipart; public class Main { private static Session session = null; public static void executeSMTPSend(String fromEmailAddress, List<String> toEmailAddresses, String subject, String body, Transport transport, File... fileAttachments) throws MessagingException, UnsupportedEncodingException, IOException { MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(fromEmailAddress)); message.setRecipients(RecipientType.TO, buildAddresses(toEmailAddresses.toArray(new String[toEmailAddresses.size()]))); message.setSubject(subject);/*from w ww. j av a 2 s. c o m*/ MimeBodyPart textPart = new MimeBodyPart(); textPart.setText(body); Multipart multiPart = new MimeMultipart(); multiPart.addBodyPart(textPart); if (fileAttachments != null) { for (File attachment : fileAttachments) { MimeBodyPart attachmentPart = new MimeBodyPart(); attachmentPart.attachFile(attachment.getCanonicalFile()); multiPart.addBodyPart(attachmentPart); } } message.setContent(multiPart); message.setSentDate(new Date()); transport.sendMessage(message, message.getAllRecipients()); } public static Address[] buildAddresses(String addresses[]) throws AddressException, UnsupportedEncodingException { Address[] newAddresses = new Address[addresses.length]; for (int t = 0; t < addresses.length; t++) { newAddresses[t] = new InternetAddress(addresses[t]); } return newAddresses; } }