Java Email MimeBodyPart create

Description

Java Email MimeBodyPart create

import java.io.IOException;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class Main {

   public static void main(String[] args) {

      String host = "smtp.somewhere.com";
      String username = "username";
      String password = "password";
      String from = "someone@somewhere.com";
      String to = "anotherone@somewhere.com";

      Properties properties = new Properties();
      properties.put("mail.smtp.host", host);
      properties.put("mail.smtp.auth", "true");

      Session session = Session.getDefaultInstance(properties, new MessageAuthenticator(username, password));

      MimeMessage message = new MimeMessage(session);
      try {/*from   ww w  . j a  va  2 s  .  com*/
         message.setFrom(new InternetAddress(from));
         message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
         message.setSubject("Subject");

         // Create Mime Content
         MimeBodyPart messageBodyPart = new MimeBodyPart();
         messageBodyPart.setContent("This is a test message", "text/plain");

         MimeBodyPart fileBodyPart = new MimeBodyPart();
         fileBodyPart.attachFile("attach.txt");

         Multipart multipart = new MimeMultipart();
         multipart.addBodyPart(messageBodyPart);
         multipart.addBodyPart(fileBodyPart);

         message.setContent(multipart);
         Transport.send(message);
      } catch (MessagingException | IOException e) {
         e.printStackTrace();
      }
   }

}

class MessageAuthenticator extends Authenticator {
   PasswordAuthentication authentication = null;

   public MessageAuthenticator(String username, String password) {
      authentication = new PasswordAuthentication(username, password);
   }

   @Override
   protected PasswordAuthentication getPasswordAuthentication() {
      return authentication;
   }
}



PreviousNext

Related