Example usage for javax.mail Transport send

List of usage examples for javax.mail Transport send

Introduction

In this page you can find the example usage for javax.mail Transport send.

Prototype

public static void send(Message msg) throws MessagingException 

Source Link

Document

Send a message.

Usage

From source file:frameworkcontentspeed.Utils.SendEmailAtachament.java

public static void sendGRUP(String from, String password, String bcc, String sub, String msg, String filename)
        throws Exception {
    //Get properties object    
    Properties props = new Properties();
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.socketFactory.port", "465");
    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.port", "465");
    //get Session   
    Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(from, password);
        }//from  w  w w  .j  a  v a  2 s . c om
    });
    //compose message    
    try {
        MimeMessage message = new MimeMessage(session);
        //message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
        message.addRecipient(Message.RecipientType.BCC, new InternetAddress(bcc));

        message.setSubject(sub);
        message.setText(msg);

        BodyPart messageBodyPart = new MimeBodyPart();

        messageBodyPart.setText("Raport teste automat");

        Multipart multipart = new MimeMultipart();

        multipart.addBodyPart(messageBodyPart);

        messageBodyPart = new MimeBodyPart();

        DataSource source = new FileDataSource(filename);

        messageBodyPart.setDataHandler(new DataHandler(source));

        messageBodyPart.setFileName(filename);

        multipart.addBodyPart(messageBodyPart);

        //message.setContent(multipart);
        StringWriter writer = new StringWriter();
        IOUtils.copy(new FileInputStream(new File(filename)), writer);

        message.setContent(writer.toString(), "text/html");

        //send message  
        Transport.send(message);
        System.out.println("message sent successfully");
    } catch (MessagingException e) {
        throw new RuntimeException(e);
    }

}

From source file:org.infoglue.common.util.mail.MailService.java

/**
 *
 * @param from the sender of the email./*  w  w w . jav  a  2s  .  co m*/
 * @param to the recipient of the email.
 * @param subject the subject of the email.
 * @param content the body of the email.
 * @throws SystemException if the email couldn't be sent due to some mail server exception.
 */
public void send(String from, String to, String subject, String content) throws SystemException {
    final Message message = createMessage(from, to, subject, content);

    try {
        Transport.send(message);
    } catch (MessagingException e) {
        throw new SystemException("Unable to send message.", e);
    }
}

From source file:com.spartasystems.holdmail.util.TestMailClient.java

public void sendEmail(String fromEmail, String toEmail, String subject, String textBody, String htmlBody) {
    try {//from  w w w.j  ava  2 s .  co m
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(fromEmail));
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmail));
        message.setSubject(subject);

        // Set the message
        createMultiMimePart(message, textBody, htmlBody);

        Transport.send(message);
    } catch (MessagingException e) {
        throw new HoldMailException("Failed to send email : " + e.getMessage(), e);
    }
}

From source file:Utilities.SendEmailUsingGMailSMTP.java

public void enviarCorreo(String correo, String clave) {
    String to = correo;//ww w .j  a  v  a  2  s .c o  m

    final String username = "angelicabarrientosvera@gmail.com";//change accordingly
    final String password = "90445359D10s";//change accordingly

    // Assuming you are sending email through relay.jangosmtp.net
    String host = "smtp.gmail.com";

    Properties props = new Properties();
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.host", host);
    props.put("mail.smtp.port", "587");
    props.put("mail.smtp.ssl.trust", "smtp.gmail.com");

    // Get the Session object.
    Session session = Session.getInstance(props, new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username, password);
        }
    });

    try {
        // Create a default MimeMessage object.
        Message message = new MimeMessage(session);

        // Set From: header field of the header.
        message.setFrom(new InternetAddress(to));

        // Set To: header field of the header.
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));

        // Set Subject: header field
        message.setSubject("Testing Subject");

        // Now set the actual message
        message.setText("\n" + "Use esta clave para poder restablecer la contrasea" + "\n"
                + "Tu cdigo para restaurar su cuenta es:" + clave);

        // Send message
        Transport.send(message);

        System.out.println("Sent message successfully....");
        //return true;

    } catch (MessagingException e) {
        throw new RuntimeException(e);

    }

}

From source file:org.tizzit.util.mail.MailHelper.java

/**
 * Sends an email in text-format in iso-8859-1-encoding
 * /*from  ww w.jav  a  2 s  . co  m*/
 * @param subject the subject of the new mail
 * @param message the content of the mail
 * @param from the sender-address
 * @param to the receiver-address(es)
 * @param cc the address of the receiver of a copy of this mail
 * @param bcc the address of the receiver of a blind-copy of this mail
 */
public static void sendMail(String subject, String message, String from, String[] to, String cc, String bcc) {
    try {
        MimeMessage msg = new MimeMessage(mailSession);
        msg.setFrom(new InternetAddress(from));
        if (cc != null && !cc.equals(""))
            msg.setRecipients(Message.RecipientType.CC, cc);
        if (bcc != null && !bcc.equals(""))
            msg.setRecipients(Message.RecipientType.BCC, bcc);
        for (int i = to.length - 1; i >= 0; i--) {
            msg.addRecipients(Message.RecipientType.TO, to[i]);
        }
        msg.setSubject(subject, "iso-8859-1");
        msg.setSentDate(new Date());
        msg.setText(message, "iso-8859-1");
        Transport.send(msg);
    } catch (Exception e) {
        log.error("Error sending mail: " + e.getLocalizedMessage());
    }
}

From source file:util.Support.java

/**
 * Send email by SSL//from  w ww.ja v  a  2 s.  co  m
 *
 * @param toEmail Email of user
 * @param idActive id active authentication
 */
public static void sendMail(String toEmail, String idActive) {
    Properties props = new Properties();
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.socketFactory.port", "465");
    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.port", "465");

    Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(util.Constants.FROM_EMAIL, util.Constants.PASSWORD_EMAIL);
        }
    });

    try {

        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(util.Constants.FROM_EMAIL));
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmail));
        message.setSubject("Authentication registration your account.");
        if (!idActive.isEmpty()) {
            message.setText("Dear Mail Crawler," + "\n Click to link to complete the registered , please!"
                    + "\n http://localhost:8084/JudiBlog/active?id=" + idActive + "");
        } else {
            message.setText("OK, thank you!" + "\n You have registed successfully!");
        }
        Transport.send(message);

    } catch (MessagingException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.mycompany.login.mb.EmailBean.java

public void envia() throws AddressException, MessagingException {
    Session session = Session.getInstance(this.propriedades, this.authentication);
    MimeMessage message = new MimeMessage(session);
    message.setFrom(new InternetAddress("anderson.freitas@lifemed.com.br"));
    message.setRecipients(Message.RecipientType.TO, "anderson.freitas@lifemed.com.br");
    message.setSentDate(new Date());
    message.setSubject("Teste envio jsf");
    message.setContent("Sua solicitao foi aprovada: OS n" + this.os, "text/plain");

    Transport.send(message);

}

From source file:se.vgregion.mobile.services.SmtpErrorReportService.java

@Override
public void report(ErrorReport report) {
    Properties props = new Properties();
    props.put("mail.smtp.host", host);
    props.put("mail.smtp.port", port);

    Session mailSession = Session.getDefaultInstance(props);
    Message simpleMessage = new MimeMessage(mailSession);

    try {//  ww  w  . j  av a2 s . c o  m
        InternetAddress fromAddress = new InternetAddress(from);
        InternetAddress toAddress = new InternetAddress(to);

        simpleMessage.setFrom(fromAddress);
        simpleMessage.setRecipient(RecipientType.TO, toAddress);
        simpleMessage.setSubject(subject);

        String text = String.format(body, report.getPrinter().getName(), report.getDescription(),
                report.getReporter());
        simpleMessage.setText(text);

        Transport.send(simpleMessage);
    } catch (MessagingException e) {
        throw new RuntimeException("Failed sending error report via mail", e);
    }

}

From source file:com.app.mail.DefaultMailSender.java

@Override
public void sendCancellationMessage(String emailAddress) {
    Session session = _authenticateOutboundEmailAddress();

    try {/* w  w  w.j a  v  a  2s.co  m*/
        Message emailMessage = _populateMessage(emailAddress, "Cancellation Successful",
                "cancellation_email.vm", session);

        Transport.send(emailMessage);
    } catch (Exception e) {
        _log.error("Unable to send cancellation message", e);
    }
}

From source file:au.org.ala.biocache.service.EmailService.java

/**
 * Sends an email with the supplied details. 
 * // ww  w .j ava  2s . c om
 * @param recipient
 * @param subject
 * @param content
 * @param sender
 */
public void sendEmail(String recipient, String subject, String content, String sender) {

    logger.debug("Send email to : " + recipient);
    logger.debug("Body: " + content);
    Session session = Session.getDefaultInstance(properties);

    try {

        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(sender));
        message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient));
        message.setSubject(subject);
        message.setContent(content, "text/html");
        Transport.send(message);
    } catch (Exception e) {
        logger.error("Unable to send email to " + recipient + ".\n" + content, e);
    }
}