Example usage for javax.mail.internet MimeMessage MimeMessage

List of usage examples for javax.mail.internet MimeMessage MimeMessage

Introduction

In this page you can find the example usage for javax.mail.internet MimeMessage MimeMessage.

Prototype

public MimeMessage(MimeMessage source) throws MessagingException 

Source Link

Document

Constructs a new MimeMessage with content initialized from the source MimeMessage.

Usage

From source file:easyproject.utils.SendMail.java

@Override
public void run() {

    Properties props = new Properties();

    props.put("mail.debug", "true");
    props.put("mail.smtp.auth", true);
    props.put("mail.smtp.starttls.enable", true);
    props.put("mail.smtp.host", servidorSMTP);
    props.put("mail.smtp.port", puerto);

    Session session = Session.getInstance(props, null);

    try {//from   w  ww.j  a  va 2s .  co m
        MimeMessage message1 = new MimeMessage(session);

        message1.addRecipient(Message.RecipientType.TO, new InternetAddress(destino));
        message1.setSubject(asunto);
        message1.setSentDate(new Date());
        message1.setContent(mensaje, "text/html; charset=utf-8");

        Transport tr = session.getTransport("smtp");
        tr.connect(servidorSMTP, usuario, password);
        message1.saveChanges();
        tr.sendMessage(message1, message1.getAllRecipients());
        tr.close();

    } catch (MessagingException e) {
        e.printStackTrace();
    }

}

From source file:org.cloudfoundry.identity.uaa.login.test.FakeJavaMailSender.java

@Override
public MimeMessage createMimeMessage() {
    return new MimeMessage(session);
}

From source file:net.sourceforge.vulcan.mailer.MessageAssembler.java

public MimeMessage constructMessage(String subscribers, ConfigDto config, ProjectStatusDto status, String html)
        throws MessagingException, AddressException {

    final MimeMessage message = new MimeMessage(mailSession);

    message.setSentDate(new Date());
    message.setFrom(new InternetAddress(config.getSenderAddress()));

    if (isNotBlank(config.getReplyToAddress())) {
        message.setReplyTo(InternetAddress.parse(config.getReplyToAddress()));
    }//from w  w  w . j  a  va2 s.  c  o m
    message.addRecipients(Message.RecipientType.TO, InternetAddress.parse(subscribers));

    message.setSubject(status.getName() + ": " + status.getStatus());

    final Multipart multipart = new MimeMultipart();

    html = html.replaceAll("\\r", "");

    addMultipartBody(multipart, "text/html; charset=UTF-8", html);

    message.setContent(multipart);

    return message;
}

From source file:org.nekorp.workflow.desktop.servicio.imp.ServicioAlertaVerificacionEmailImp.java

@Override
public void enviarAlerta(List<AlertaVerificacion> alertas) {
    try {//w  w  w . j av a  2 s  .c om
        for (AlertaVerificacion x : alertas) {
            Message message = new MimeMessage(template.buildSession());
            message.setFrom(new InternetAddress(template.getMailSender()));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(template.getMailRecipient()));
            message.setSubject("Alerta de Verificacin");
            ST contenido = new ST(contenidoRaw);
            contenido.add("placas", x.getPlacas());
            contenido.add("periodo", x.getPeriodo());
            message.setText(contenido.render());
            Transport.send(message);
        }
    } catch (MessagingException e) {
        throw new RuntimeException(e);
    }
}

From source file:net.kamhon.ieagle.function.email.SendMail.java

public void send(String to, String cc, String bcc, String from, String subject, String text, String emailType) {

    // Session session = Session.getInstance(props, null);
    Session session = Session.getInstance(props, new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(emailServerSetting.getUsername(),
                    emailServerSetting.getPassword());
        }// w w w  .  j a  v  a 2 s  .co  m
    });

    session.setDebug(emailServerSetting.isDebug());

    try {
        Message msg = new MimeMessage(session);
        if (StringUtils.isNotBlank(from)) {
            msg.setFrom(new InternetAddress(from));
        } else {
            msg.setFrom();
        }

        msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));
        if (StringUtils.isNotBlank(cc)) {
            msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc, false));
        }
        if (StringUtils.isNotBlank(bcc)) {
            msg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(bcc, false));
        }

        msg.setSubject(subject);
        if (Emailq.TYPE_HTML.equalsIgnoreCase(emailType)) {
            msg.setContent(text, "text/html");
        } else {
            msg.setText(text);
        }
        msg.setSentDate(new java.util.Date());
        Transport.send(msg);
    } catch (MessagingException mex) {
        mex.printStackTrace();
        Exception ex = null;
        if ((ex = mex.getNextException()) != null) {
            ex.printStackTrace();
        }
        throw new SystemErrorException(mex);
    }
}

From source file:mx.unam.pixel.controller.MailController.java

public void enviaMensaje(String mensaje) {
    try {//from ww  w  .  j  a  va2s  .c  o  m
        Properties props = new Properties();

        // Nombre del host de correo, es smtp.gmail.com
        props.setProperty("mail.smtp.host", "smtp.gmail.com");

        // TLS si est disponible
        props.setProperty("mail.smtp.starttls.enable", "true");

        // Puerto de gmail para envio de correos
        props.setProperty("mail.smtp.port", "587");

        // Nombre del usuario
        props.setProperty("mail.smtp.user", "ejemplo@gmail.com");

        // Si requiere o no usuario y password para conectarse.
        props.setProperty("mail.smtp.auth", "true");

        // Preparamos la sesion
        Session session = Session.getDefaultInstance(props);
        //session.setDebug(true);

        //Construimos el mensage
        MimeMessage message = new MimeMessage(session);
        InternetAddress cuenta = new InternetAddress("enrique.wps@gmail.com");
        message.setFrom(); //Correo electronico que manda el mensaja
        message.addRecipient(Message.RecipientType.TO, new InternetAddress("vampa@ciencias.unam.mx"));
        message.setSubject("Test MUFFIN");
        message.setText("Test Muffin Cuerpo");
        // Abrimos la comunicacion 
        Transport t = session.getTransport("smtp");

        t.connect("dasds@gmail.com", "asdsadas"); // Ususario y contrasea
        t.sendMessage(message, message.getAllRecipients());
        // Cierre
        t.close();

    } catch (AddressException ex) {
        System.err.println("er");
    } catch (MessagingException ex) {
        Logger.getLogger(MailController.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:org.nekorp.workflow.desktop.servicio.imp.ServicioAlertaEmailImp.java

@Override
public void enviarAlerta(List<AlertaServicio> alertas) {
    try {/*from   ww w.j ava2s  .  c  om*/
        for (AlertaServicio x : alertas) {
            Message message = new MimeMessage(template.buildSession());
            message.setFrom(new InternetAddress(template.getMailSender()));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(template.getMailRecipient()));
            message.setSubject("Alerta");
            ST contenido = new ST(contenidoRaw);
            contenido.add("cliente", x.getNombreCliente());
            contenido.add("tipo", x.getTipoAuto());
            contenido.add("marca", x.getMarcaAuto());
            contenido.add("placas", x.getPlacasAuto());
            contenido.add("kilometraje", x.getKilometrajeAuto());
            contenido.add("servicio", x.getDescripcionServicio());
            contenido.add("kilometrajeServicio", x.getKilometrajeServicio());
            message.setText(contenido.render());
            Transport.send(message);
        }
    } catch (MessagingException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.trivago.mail.pigeon.mail.MailFacade.java

public void sendMail(MailTransport mailTransport) {
    log.debug("Mail delivery started");
    File propertyfile = ((PropertiesConfiguration) Settings.create().getConfiguration()).getFile();
    Properties config = new Properties();

    try {/*  w w w.  j  a v  a  2 s.co m*/
        config.load(new FileReader(propertyfile));
    } catch (IOException e) {
        log.error(e);
    }

    Session session = Session.getDefaultInstance(config);

    log.debug("Received session");

    MimeMessage message = new MimeMessage(session);

    String to = mailTransport.getTo();
    String from = mailTransport.getFrom();
    String replyTo = mailTransport.getReplyTo();
    String subject = mailTransport.getSubject();
    String html = mailTransport.getHtml();
    String text = mailTransport.getText();

    try {
        Address fromAdr = new InternetAddress(from);
        Address toAdr = new InternetAddress(to);
        Address rplyAdr = new InternetAddress(replyTo);

        message.setSubject(subject);
        message.setFrom(fromAdr);
        message.setRecipient(Message.RecipientType.TO, toAdr);
        message.setReplyTo(new Address[] { rplyAdr });
        message.setSender(fromAdr);
        message.addHeader("Return-path", replyTo);
        message.addHeader("X-TRV-MID", mailTransport.getmId());
        message.addHeader("X-TRV-UID", mailTransport.getuId());

        // Content
        MimeBodyPart messageTextPart = new MimeBodyPart();
        messageTextPart.setText(text);
        messageTextPart.setContent(html, "text/html");

        Multipart multipart = new MimeMultipart();
        multipart.addBodyPart(messageTextPart);

        // Put parts in message
        message.setContent(multipart);

        log.debug("Dispatching message");
        Transport.send(message);
        log.debug("Mail delivery ended");
    } catch (MessagingException e) {
        log.error(e);
    }

}

From source file:EmailBean.java

public void sendMessage() throws Exception {

    Properties properties = System.getProperties();

    //populate the 'Properties' object with the mail
    //server address, so that the default 'Session'
    //instance can use it.
    properties.put("mail.smtp.host", smtpHost);

    Session session = Session.getDefaultInstance(properties);

    Message mailMsg = new MimeMessage(session);//a new email message

    InternetAddress[] addresses = null;// w  w w.jav  a  2  s  .  com

    try {

        if (to != null) {

            //throws 'AddressException' if the 'to' email address
            //violates RFC822 syntax
            addresses = InternetAddress.parse(to, false);

            mailMsg.setRecipients(Message.RecipientType.TO, addresses);

        } else {

            throw new MessagingException("The mail message requires a 'To' address.");

        }

        if (from != null) {

            mailMsg.setFrom(new InternetAddress(from));

        } else {

            throw new MessagingException("The mail message requires a valid 'From' address.");

        }

        if (subject != null)
            mailMsg.setSubject(subject);

        if (content != null)
            mailMsg.setText(content);

        //Finally, send the mail message; throws a 'SendFailedException'
        //if any of the message's recipients have an invalid address
        Transport.send(mailMsg);

    } catch (Exception exc) {

        throw exc;

    }

}

From source file:com.gcrm.util.mail.MailService.java

public void sendSimpleMail(String toAddress) throws Exception {
    List<EmailSetting> emailSettings = baseService.getAllObjects(EmailSetting.class.getSimpleName());
    EmailSetting emailSetting = null;/*from  w  w w.  j  av  a2s  .c om*/
    if (emailSettings != null && emailSettings.size() > 0) {
        emailSetting = emailSettings.get(0);
    } else {
        return;
    }
    Session mailSession = this.createSmtpSession(emailSetting);
    if (mailSession != null) {
        Transport transport = mailSession.getTransport();
        MimeMessage msg = new MimeMessage(mailSession);
        MimeMessageHelper helper = new MimeMessageHelper(msg, true, "utf-8");
        helper.setFrom(emailSetting.getFrom_address());
        helper.setTo(toAddress);
        helper.setSubject("Test Mail From " + emailSetting.getFrom_name());
        helper.setText("This is test mail from " + emailSetting.getFrom_name(), true);
        transport.connect();
        transport.sendMessage(msg, msg.getRecipients(Message.RecipientType.TO));
    }
}