Java Email Transport send email

Description

Java Email Transport send email


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 boolean sendMail(String email, String fname, String lname, String feedback) {
      boolean result = false;
      try {/* ww  w  .  j  a  va  2  s . c  o  m*/
         // Send email here
         // servlet configuration initialization parameters

         String contextSmtp = "smtpAddress";
         // Set the host smtp address
         Properties props = new Properties();
         // String smtpServer = obtainProperties("SMTP_SERVER");
         props.put("mail.smtp.host", contextSmtp);

         // 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(email);
         msg.setFrom(addressFrom);
         InternetAddress[] address = new InternetAddress[1];
         address[0] = new InternetAddress(email);
         msg.setRecipients(Message.RecipientType.TO, address);
         msg.setSubject("***Customer Feedback ***");
         // Append Footer
         msg.setContent(feedback, "text/plain");
         // Uncomment for production
         Transport.send(msg);

         result = true;
      } catch (javax.mail.MessagingException ex) {
         ex.printStackTrace();
         result = false;
      }
      return result;
   }
}



PreviousNext

Related