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:de.micromata.genome.gwiki.model.GWikiStandardEmailProvider.java

public void sendEmailImpl(Map<String, String> ctx) throws MessagingException {
    Map<String, String> headers = new HashMap<String, String>();
    if (ctx.containsKey(TO) == true) {
        headers.put(TO, ctx.get(TO));// ww  w  .j a  v  a 2 s .co m
    }
    if (ctx.containsKey(FROM) == true) {
        headers.put(FROM, ctx.get(FROM));
    }
    if (ctx.containsKey(CC) == true) {
        headers.put(CC, ctx.get(CC));
    }
    if (ctx.containsKey(BCC) == true) {
        headers.put(BCC, ctx.get(BCC));
    }
    if (ctx.containsKey(SUBJECT) == true) {
        headers.put(SUBJECT, ctx.get(SUBJECT));
    }
    Session emailSession = getEmailSession();
    if (emailSession == null) {
        GWikiLog.warn("Cannot send email, because no emailSession is configured");
        return;
    }
    MimeMessage message = new MimeMessage(emailSession);
    String encoding = DEFAULT_MAIL_ENCODING;
    GWikiMailHelper mh = new GWikiMailHelper(message, false, encoding);
    setHeaders(mh, headers);
    mh.setBody(ctx.get(TEXT));
    Transport.send(message);
}

From source file:com.fstx.stdlib.common.messages.MailSenderImpl.java

/**
 * @see com.ess.messages.MailSender#send()
 *//*w  w  w  .  j  av a2  s.  c  o m*/
public boolean send() {

    try {
        // 2005-11-27 RSC always requires authentication.
        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");

        props.put("mail.smtp.host", host.getAddress());
        /*
         * 2005-11-27 RSC
         * Since webmail.us is starting to make other ports available
         * as Comcast blocks port 25.
         */
        props.put("mail.smtp.port", host.getPort());

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

        MimeMessage messageOut = new MimeMessage(s);

        InternetAddress fromOut = new InternetAddress(from.getAddress());

        //reid 2004-12-20
        fromOut.setPersonal(from.getName());

        messageOut.setFrom(fromOut);

        InternetAddress toOut = new InternetAddress(this.to.getAddress());

        //reid 2004-12-20
        toOut.setPersonal(to.getName());

        messageOut.addRecipient(javax.mail.Message.RecipientType.TO, toOut);

        messageOut.setSubject(message.getSubject());

        messageOut.setText(message.getMessage());

        if (host.useAuthentication()) {

            Transport transport = s.getTransport("smtp");
            transport.connect(host.getAddress(), host.getUsername(), host.getPassword());
            transport.sendMessage(messageOut, messageOut.getAllRecipients());
            transport.close();
        } else {

            Transport.send(messageOut);
        }

    } catch (Exception e) {
        log.info("\n\nMailSenderIMPL3: " + host.getAddress());
        e.printStackTrace();
        throw new RuntimeException(e);
    }

    return true;
}

From source file:org.eclipse.che.mail.MailSender.java

public void sendMail(EmailBean emailBean) throws SendMailException {
    File tempDir = null;/*from  w  ww. ja v a2s .  co m*/
    try {
        MimeMessage message = new MimeMessage(mailSessionProvider.get());
        Multipart contentPart = new MimeMultipart();

        MimeBodyPart bodyPart = new MimeBodyPart();
        bodyPart.setText(emailBean.getBody(), "UTF-8", getSubType(emailBean.getMimeType()));
        contentPart.addBodyPart(bodyPart);

        if (emailBean.getAttachments() != null) {
            tempDir = Files.createTempDir();
            for (Attachment attachment : emailBean.getAttachments()) {
                // Create attachment file in temporary directory
                byte[] attachmentContent = Base64.getDecoder().decode(attachment.getContent());
                File attachmentFile = new File(tempDir, attachment.getFileName());
                Files.write(attachmentContent, attachmentFile);

                // Attach the attachment file to email
                MimeBodyPart attachmentPart = new MimeBodyPart();
                attachmentPart.attachFile(attachmentFile);
                attachmentPart.setContentID("<" + attachment.getContentId() + ">");
                contentPart.addBodyPart(attachmentPart);
            }
        }

        message.setContent(contentPart);
        message.setSubject(emailBean.getSubject(), "UTF-8");
        message.setFrom(new InternetAddress(emailBean.getFrom(), true));
        message.setSentDate(new Date());
        message.addRecipients(Message.RecipientType.TO, InternetAddress.parse(emailBean.getTo()));

        if (emailBean.getReplyTo() != null) {
            message.setReplyTo(InternetAddress.parse(emailBean.getReplyTo()));
        }
        LOG.info("Sending from {} to {} with subject {}", emailBean.getFrom(), emailBean.getTo(),
                emailBean.getSubject());

        Transport.send(message);
        LOG.debug("Mail sent");
    } catch (Exception e) {
        LOG.error(e.getLocalizedMessage());
        throw new SendMailException(e.getLocalizedMessage(), e);
    } finally {
        if (tempDir != null) {
            try {
                FileUtils.deleteDirectory(tempDir);
            } catch (IOException exception) {
                LOG.error(exception.getMessage());
            }
        }
    }
}

From source file:Sender.java

/** Do the work: send the mail to the SMTP server. */
public void doSend() {

    // We need to pass info to the mail server as a Properties, since
    // JavaMail (wisely) allows room for LOTS of properties...
    Properties props = new Properties();

    // Your LAN must define the local SMTP server as "mailhost"
    // for this simple-minded version to be able to send mail...
    props.put("mail.smtp.host", "mailhost");

    // Create the Session object
    session = Session.getDefaultInstance(props, null);
    session.setDebug(true); // Verbose!

    try {/*from w  ww  . ja v  a2  s  . co  m*/
        // create a message
        mesg = new MimeMessage(session);

        // From Address - this should come from a Properties...
        mesg.setFrom(new InternetAddress("nobody@host.domain"));

        // TO Address
        InternetAddress toAddress = new InternetAddress(message_recip);
        mesg.addRecipient(Message.RecipientType.TO, toAddress);

        // CC Address
        InternetAddress ccAddress = new InternetAddress(message_cc);
        mesg.addRecipient(Message.RecipientType.CC, ccAddress);

        // The Subject
        mesg.setSubject(message_subject);

        // Now the message body.
        mesg.setText(message_body);
        // XXX I18N: use setText(msgText.getText(), charset)

        // Finally, send the message!
        Transport.send(mesg);

    } catch (MessagingException ex) {
        while ((ex = (MessagingException) ex.getNextException()) != null) {
            ex.printStackTrace();
        }
    }
}

From source file:nu.mine.kino.jenkins.plugins.projectmanagement.utils.PMUtils.java

public static void sendMail(String[] addresses, String subject, String message)
        throws UnsupportedEncodingException, MessagingException {

    MimeMessage mimeMessage = new MimeMessage(Mailer.descriptor().createSession());
    String adminAddress = JenkinsLocationConfiguration.get().getAdminAddress();

    InternetAddress[] to = new InternetAddress[addresses.length];
    for (int i = 0; i < addresses.length; i++) {
        to[i] = new InternetAddress(addresses[i], true);
    }//w  ww .  ja  va 2 s. c  o m
    mimeMessage.setSender(new InternetAddress(adminAddress));
    mimeMessage.setRecipients(Message.RecipientType.TO, to);
    mimeMessage.setSubject(subject, "ISO-2022-JP");
    mimeMessage.setText(message, "ISO-2022-JP");
    Transport.send(mimeMessage);
}

From source file:com.ayu.filter.RegularService.java

@Async
public void sendSSLMail(String text, String toMail) {

    final String username = "clouddefenceids";
    final String password = "";

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

    Session session = Session.getInstance(props, new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username, password);
        }// w ww. ja v a  2s. com
    });

    try {

        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(username));
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toMail));
        message.setSubject("Forgot Password");
        message.setText(text);

        Transport.send(message);

        System.out.println("Done");

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

From source file:com.fsrin.menumine.common.message.MailSenderImpl.java

/**
 * @see com.ess.messages.MailSender#send()
 *//* w  w  w.  ja  v  a  2 s .c  o m*/
public boolean send() {

    try {

        Properties props = new Properties();

        props.put("mail.smtp.host", host.getAddress());

        if (host.useAuthentication()) {

            props.put("mail.smtp.auth", "true");
        }

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

        s.setDebug(true);

        //            PasswordAuthentication pa = new PasswordAuthentication(host
        //                    .getUsername(), host.getPassword());
        //
        //            URLName url = new URLName(host.getAddress());
        //
        //            s.setPasswordAuthentication(url, pa);

        MimeMessage messageOut = new MimeMessage(s);

        InternetAddress fromOut = new InternetAddress(from.getAddress());

        //reid 2004-12-20
        fromOut.setPersonal(from.getName());

        messageOut.setFrom(fromOut);

        InternetAddress toOut = new InternetAddress(this.to.getAddress());

        //reid 2004-12-20
        toOut.setPersonal(to.getName());

        messageOut.addRecipient(javax.mail.Message.RecipientType.TO, toOut);

        messageOut.setSubject(message.getSubject());

        messageOut.setText(message.getMessage());

        if (host.useAuthentication()) {

            Transport transport = s.getTransport("smtp");
            transport.connect(host.getAddress(), host.getUsername(), host.getPassword());
            transport.sendMessage(messageOut, messageOut.getAllRecipients());
            transport.close();
        } else {

            Transport.send(messageOut);
        }

    } catch (Exception e) {
        log.info("\n\nMailSenderIMPL3: " + host.getAddress());

        e.printStackTrace();
        throw new RuntimeException(e);

    }

    return true;
}

From source file:ru.org.linux.user.LostPasswordController.java

private void sendEmail(User user, String email, Timestamp resetDate) throws MessagingException {
    Properties props = new Properties();
    props.put("mail.smtp.host", "localhost");
    Session mailSession = Session.getDefaultInstance(props, null);

    MimeMessage msg = new MimeMessage(mailSession);
    msg.setFrom(new InternetAddress("no-reply@linux.org.ru"));

    String resetCode = UserService.getResetCode(configuration.getSecret(), user.getNick(), email, resetDate);

    msg.addRecipient(RecipientType.TO, new InternetAddress(email));
    msg.setSubject("Your password @linux.org.ru");
    msg.setSentDate(new Date());
    msg.setText("?!\n\n"
            + "? ??  ?   ?? http://www.linux.org.ru/reset-password\n\n"
            + "  " + user.getNick() + ",  ?: " + resetCode + "\n\n"
            + "!");

    Transport.send(msg);
}

From source file:io.kodokojo.service.SmtpEmailSender.java

@Override
public void send(List<String> to, List<String> cc, List<String> ci, String subject, String content,
        boolean htmlContent) {
    if (CollectionUtils.isEmpty(to)) {
        throw new IllegalArgumentException("to must be defined.");
    }//  ww  w  .j av a2  s.com
    if (isBlank(content)) {
        throw new IllegalArgumentException("content must be defined.");
    }
    Session session = Session.getDefaultInstance(properties, new Authenticator() {
        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username, password);
        }
    });
    Message message = new MimeMessage(session);
    try {
        message.setFrom(from);
        message.setSubject(subject);
        InternetAddress[] toInternetAddress = convertToInternetAddress(to);
        message.setRecipients(Message.RecipientType.TO, toInternetAddress);
        if (CollectionUtils.isNotEmpty(cc)) {
            InternetAddress[] ccInternetAddress = convertToInternetAddress(cc);
            message.setRecipients(Message.RecipientType.CC, ccInternetAddress);
        }
        if (CollectionUtils.isNotEmpty(ci)) {
            InternetAddress[] ciInternetAddress = convertToInternetAddress(ci);
            message.setRecipients(Message.RecipientType.BCC, ciInternetAddress);
        }
        if (htmlContent) {
            message.setContent(content, "text/html");
        } else {
            message.setText(content);
        }
        message.setHeader("X-Mailer", "Kodo Kojo mailer");
        message.setSentDate(new Date());
        Transport.send(message);

    } catch (MessagingException e) {
        LOGGER.error("Unable to send email to {} with subject '{}'", StringUtils.join(to, ","), subject, e);
    }
}

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

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

    try {//from  w w  w.  j  av a  2s.c  o m
        Message emailMessage = _populateMessage(emailAddress, "Resubscribe Successful", "resubscribe_email.vm",
                session);

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