Java exec executeSMTPSend(String fromEmailAddress, List toEmailAddresses, String subject, String body, Transport transport, File... fileAttachments)

Here you can find the source of executeSMTPSend(String fromEmailAddress, List toEmailAddresses, String subject, String body, Transport transport, File... fileAttachments)

Description

execute SMTP Send

License

Creative Commons License

Declaration

public static void executeSMTPSend(String fromEmailAddress, List<String> toEmailAddresses, String subject,
            String body, Transport transport, File... fileAttachments)
            throws MessagingException, UnsupportedEncodingException, IOException 

Method Source Code


//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;
    }
}

Related

  1. executeShellCommand(String command)
  2. executeShellCommand(String command)
  3. executeShellCommand(String command, File dir)
  4. executeShellCommand(String commandName)
  5. executeShellScript(final String shellScript, final File tempDirectory)
  6. execVmCmdAsync(String vmNameSpace, String vmKey, String vmUser, String vmIp, String vmCmd, String controllerOutPutFile)
  7. execWindowsCommand(String cmd)
  8. execWithOutput(File dumpFile, String... args)
  9. execWithResult(String cmd)