Java Email InternetAddress create

Description

Java Email InternetAddress create

import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class Main {

   public static void main(String[] argv) {
      try {/*w w w. j av a 2  s.co  m*/
         String from = "from address";
         String to = "to address";
         String smtp = "address";
         String message = "message";
         // Set the host smtp address
         Properties props = new Properties();
         props.put("mail.smtp.host", smtp);

         // create some properties and get the default Session
         Session session = Session.getDefaultInstance(props, null);
         session.setDebug(false);

         // create a message
         Message msg = new MimeMessage(session);

         // set the from and to address
         InternetAddress addressFrom = new InternetAddress(from);
         msg.setFrom(addressFrom);
         InternetAddress[] address = new InternetAddress[1];
         address[0] = new InternetAddress(to);
         msg.setRecipients(Message.RecipientType.TO, address);
         msg.setSubject("*** Applet Email ***");
         // Append Footer
         msg.setContent(message, "text/plain");
         // Uncomment for production
         Transport.send(msg);

      } catch (javax.mail.MessagingException ex) {
         ex.printStackTrace();

      }
   }
}



PreviousNext

Related