send Email Format Plain - Java Network

Java examples for Network:EMail

Description

send Email Format Plain

Demo Code


import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
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;
import org.apache.log4j.Logger;

public class Main{
    private final static Logger logger = Logger
            .getLogger(EmailSmtpUtil.class.getName());
    private final static String MAIL_PORT = "";
    private final static String MAIL_SERVER = "";
    private final static String MAIL_FROM = "";
    private final static String MAIL_STARTTLS = "";
    private final static String MAIL_AUTH = "";
    private final static String MAIL_USER = "";
    private final static String MAIL_PWD = "";
    public static void sendEmailFormatPlain(String to, String bodyPlain,
            String subject) {//from  w w  w .j a  v a2 s. c o m

        logger.info("sendEmailFormatPlain");
        logger.info("Message send in progress...");

        Properties properties = System.getProperties();
        properties.setProperty("mail.smtp.host", MAIL_SERVER);
        properties.setProperty("mail.smtp.port", MAIL_PORT);
        Session session = Session.getDefaultInstance(properties);
        if (MAIL_AUTH.equals("true")) {
            properties.setProperty("mail.smtp.auth", MAIL_AUTH);
            properties.setProperty("mail.smtp.starttls.enable",
                    MAIL_STARTTLS);
            session = Session.getInstance(properties,
                    new javax.mail.Authenticator() {
                        protected PasswordAuthentication getPasswordAuthentication() {
                            return new PasswordAuthentication(MAIL_USER,
                                    MAIL_PWD);
                        }
                    });
        }

        try {
            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress(MAIL_FROM));
            message.addRecipient(Message.RecipientType.TO,
                    new InternetAddress(to));
            message.setSubject(subject);
            message.setContent(bodyPlain, "text/plain");

            Transport.send(message);
            logger.info("... MESSAGE SENDED with SUCCESS.");
        } catch (MessagingException mex) {
            mex.printStackTrace();
            logger.info("... MESSAGE SENDED with FAIL!!!!!!!!!");
        }

    }
}

Related Tutorials