Example usage for javax.mail Message setRecipient

List of usage examples for javax.mail Message setRecipient

Introduction

In this page you can find the example usage for javax.mail Message setRecipient.

Prototype

public void setRecipient(RecipientType type, Address address) throws MessagingException 

Source Link

Document

Set the recipient address.

Usage

From source file:org.kite9.diagram.server.AbstractKite9Controller.java

public void sendErrorEmail(Throwable t, String xml, String url) {
    try {/* w  w  w  . ja va2s.com*/
        Properties props = new Properties();
        props.put("mail.smtp.host", "server.kite9.org");
        Session session = Session.getInstance(props);
        Message msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress("servicetest@kite9.com"));
        msg.setRecipient(RecipientType.TO, new InternetAddress("rob@kite9.com"));
        String name = ctx.getServletContextName();
        boolean local = isLocal();
        msg.setSubject("Failure in " + (local ? "TEST" : name) + " Service: " + this.getClass().getName());
        StringWriter sw = new StringWriter(10000);
        PrintWriter pw = new PrintWriter(sw);
        pw.write("URL: " + url + "\n");

        t.printStackTrace(pw);

        if (xml != null) {
            pw.println();
            pw.print(xml);
        }

        pw.close();
        msg.setText(sw.toString());
        Transport.send(msg);
    } catch (Exception e) {
    } finally {
        t.printStackTrace();
    }
}

From source file:com.enonic.vertical.userservices.OrderHandlerController.java

private void sendMail(String receiverEmail, String receiverName, String senderEmail, String senderName,
        String subject, String message) throws MessagingException, UnsupportedEncodingException {

    // smtp server
    Properties smtpProperties = new Properties();
    smtpProperties.put("mail.smtp.host", verticalProperties.getMailSmtpHost());
    Session session = Session.getDefaultInstance(smtpProperties, null);

    // create message
    Message msg = new MimeMessage(session);

    // set from address
    if (senderEmail != null && !senderEmail.equals("")) {
        InternetAddress addressFrom = new InternetAddress(senderEmail);
        if (senderName != null && !senderName.equals("")) {
            addressFrom.setPersonal(senderName);
        }//  www  .  j  ava 2s.co  m
        msg.setFrom(addressFrom);
    }

    // set to address
    InternetAddress addressTo = new InternetAddress(receiverEmail);
    if (receiverName != null) {
        addressTo.setPersonal(receiverName);
    }
    msg.setRecipient(Message.RecipientType.TO, addressTo);

    // Setting subject and content type
    msg.setSubject(subject);

    message = RegexpUtil.substituteAll("(\\\\n)", "\n", message);
    msg.setContent(message, "text/plain; charset=UTF-8");

    // send message
    Transport.send(msg);
}

From source file:de.innovationgate.wgpublisher.WGACore.java

public void send(WGAMailNotification notification) {
    WGAMailConfiguration config = getMailConfig();

    if (config != null && config.isEnableAdminNotifications()) {
        try {/*from  ww w.j  a v  a 2  s.com*/
            Message msg = new MimeMessage(config.createMailSession());

            // set recipient and from address
            String toAddress = config.getToAddress();
            if (toAddress == null) {
                getLog().error(
                        "Unable to send wga admin notification because no recipient address is configured");
                return;
            }

            msg.setRecipient(Message.RecipientType.TO, new InternetAddress(toAddress));
            InternetAddress[] fromAddr = new InternetAddress[1];
            fromAddr[0] = new InternetAddress(config.getFromAddress());
            msg.addFrom(fromAddr);

            msg.setSentDate(new Date());

            InetAddress localMachine = InetAddress.getLocalHost();
            String hostname = localMachine.getHostName();
            String serverName = getWgaConfiguration().getServerName();
            if (serverName == null) {
                serverName = hostname;
            }

            msg.setSubject(notification.getSubject());

            msg.setHeader(WGAMailNotification.HEADERFIELD_TYPE, notification.getType());

            MimeMultipart content = new MimeMultipart();
            MimeBodyPart body = new MimeBodyPart();

            StringBuffer strBody = new StringBuffer();
            strBody.append("<html><head></head><body style=\"color:#808080\">");
            strBody.append(notification.getMessage());
            String rootURL = getWgaConfiguration().getRootURL();
            if (rootURL != null) {
                //strBody.append("<br><br>");
                strBody.append("<p><a href=\"" + rootURL + "/plugin-admin\">" + WGABrand.getName()
                        + " admin client ...</a></p>");
            }
            // append footer
            strBody.append("<br><br><b>System information:</b><br><br>");
            strBody.append("<b>Server:</b> " + serverName + " / " + WGACore.getReleaseString() + "<br>");
            strBody.append("<b>Host:</b> " + hostname + "<br>");
            strBody.append("<b>Operation System:</b> " + System.getProperty("os.name") + " Version "
                    + System.getProperty("os.version") + " (" + System.getProperty("os.arch") + ")<br>");
            strBody.append("<b>Java virtual machine:</b> " + System.getProperty("java.vm.name") + " Version "
                    + System.getProperty("java.vm.version") + " (" + System.getProperty("java.vm.vendor")
                    + ")");

            strBody.append("</body></html>");
            body.setText(strBody.toString());
            body.setHeader("MIME-Version", "1.0");
            body.setHeader("Content-Type", "text/html");

            content.addBodyPart(body);
            AppLog appLog = WGA.get(this).service(AppLog.class);

            if (notification.isAttachLogfile()) {
                MimeBodyPart attachmentBody = new MimeBodyPart();

                StringWriter applog = new StringWriter();
                int applogSize = appLog.getLinesCount();
                int offset = applogSize - notification.getLogfileLines();
                if (offset < 0) {
                    offset = 1;
                }
                appLog.writePage(offset, notification.getLogfileLines(), applog, LogLevel.LEVEL_INFO, false);

                attachmentBody.setDataHandler(new DataHandler(applog.toString(), "text/plain"));
                attachmentBody.setFileName("wga.log");
                content.addBodyPart(attachmentBody);
            }
            msg.setContent(content);

            // Send mail
            Thread mailThread = new Thread(new AsyncMailSender(msg), "WGAMailSender");
            mailThread.start();
        } catch (Exception e) {
            getLog().error("Unable to send wga admin notification.", e);
        }
    }
}

From source file:org.fao.geonet.services.register.SelfRegister.java

/**
 * Send an email./*from ww w .j a v  a  2  s  . c om*/
 * 
 * @param host
 * @param port
 * @param subject
 * @param from
 * @param to
 * @param content
 * @return
 */
boolean sendMail(String host, int port, String subject, String from, String to, String content) {
    boolean isSendout = false;

    Properties props = new Properties();

    props.put("mail.transport.protocol", PROTOCOL);
    props.put("mail.smtp.host", host);
    props.put("mail.smtp.port", port);
    props.put("mail.smtp.auth", "false");

    Session mailSession = Session.getDefaultInstance(props);

    try {
        Message msg = new MimeMessage(mailSession);
        msg.setFrom(new InternetAddress(from));
        msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
        msg.setSentDate(new Date());
        msg.setSubject(subject);
        // Add content message
        msg.setText(content);
        Transport.send(msg);
        isSendout = true;
    } catch (AddressException e) {
        isSendout = false;
        e.printStackTrace();
    } catch (MessagingException e) {
        isSendout = false;
        e.printStackTrace();
    }
    return isSendout;
}

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

/**
 *
 *//*from   w ww .  j  a  va 2 s  .  c o  m*/
private Message createMessage(String from, String to, String bcc, String subject, String content)
        throws SystemException {
    try {
        final Message message = new MimeMessage(this.session);

        message.setContent(content, "text/html");
        message.setFrom(createInternetAddress(from));
        message.setRecipient(Message.RecipientType.TO, createInternetAddress(to));
        if (bcc != null)
            message.setRecipients(Message.RecipientType.BCC, createInternetAddresses(bcc));
        message.setSubject(subject);
        message.setText(content);
        message.setDataHandler(new DataHandler(new StringDataSource(content, "text/html")));

        return message;
    } catch (MessagingException e) {
        throw new Bug("Unable to create the message.", e);
    }
}

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

/**
 *
 *//*from  w  ww  .  j av a 2s.c o m*/
private Message createMessage(String from, String to, String bcc, String subject, String content,
        String contentType, String encoding) throws SystemException {
    try {
        final Message message = new MimeMessage(this.session);
        String contentTypeWithEncoding = contentType + ";charset=" + encoding;

        //message.setContent(content, contentType);
        message.setFrom(createInternetAddress(from));
        message.setRecipient(Message.RecipientType.TO, createInternetAddress(to));
        if (bcc != null)
            message.setRecipients(Message.RecipientType.BCC, createInternetAddresses(bcc));
        //message.setSubject(subject);

        ((MimeMessage) message).setSubject(subject, encoding);
        //message.setText(content);
        message.setDataHandler(
                new DataHandler(new StringDataSource(content, contentTypeWithEncoding, encoding)));
        //message.setText(content);
        //message.setDataHandler(new DataHandler(new StringDataSource(content, "text/html"))); 

        return message;
    } catch (MessagingException e) {
        throw new Bug("Unable to create the message.", e);
    }
}

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

/**
 *
 *///from  w  w  w  . jav  a 2  s . co m
private Message createMessage(String from, String to, String subject, String content) throws SystemException {
    try {
        final Message message = new MimeMessage(this.session);

        message.setContent(content, "text/html");
        message.setFrom(createInternetAddress(from));
        message.setRecipient(Message.RecipientType.TO, createInternetAddress(to));
        message.setSubject(subject);
        message.setText(content);
        message.setDataHandler(new DataHandler(new StringDataSource(content, "text/html")));

        return message;
    } catch (MessagingException e) {
        throw new SystemException("Unable to create the message.", e);
    }
}

From source file:org.nuxeo.ecm.platform.mail.action.SendMailAction.java

public boolean execute(ExecutionContext context) throws MessagingException {
    Message message = context.getMessage();
    if (log.isDebugEnabled()) {
        log.debug("Sending mail because of message: " + message.getSubject());
    }//  w  ww .  ja  v  a  2 s.  c  o m
    Message sentMessage = new MimeMessage(session);
    if (message.getReplyTo() == null || message.getReplyTo().length == 0) {
        return true;
    }
    Address address = message.getReplyTo()[0];
    sentMessage.setRecipient(Message.RecipientType.TO, address);
    message.setText(textMessage);
    Transport.send(sentMessage);
    return true;
}

From source file:org.socraticgrid.taskmanager.MailHandler.java

private boolean sendMail(String host, String fromUser, boolean isFromUserProvider, String toUser,
        boolean isToUserProvider, String subject, String text) {
    boolean retVal = true;

    try {//w w w .j av  a 2 s.  co m
        String fromAddr = getEmailAddr(isFromUserProvider, fromUser);
        String toAddr = getEmailAddr(isToUserProvider, toUser);

        //Get session
        Properties props = new Properties();
        props.put("mail.smtp.host", host);
        Session session = Session.getInstance(props);

        //Create messages
        Message msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress(fromAddr));
        msg.setRecipient(Message.RecipientType.TO, new InternetAddress(toAddr));
        msg.setSubject(subject);
        msg.setText(text);
        msg.setHeader("X-Mailer", X_MAILER);
        msg.setSentDate(new Date());

        // send the thing off
        Transport.send(msg);

        log.debug("Mail was sent successfully.");
    } catch (Exception e) {
        log.error("Error sending mail.", e);
        retVal = false;
    }

    return retVal;
}

From source file:webreader.WebReader.java

private void sendEmail(String inputMessage) {
    //---User login information
    final String username = email;
    final String password = emailPassword;

    //---Sets server for gmail
    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");

    //---Creates a new session
    Session session = Session.getInstance(props, new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username, password);

        }//from  ww  w  .j  a  va2 s .  c o m

    });
    //---Try catch loop for sending an email
    //---Sends error email to the sender if catch is engaged.
    try {
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(email));
        message.setRecipient(Message.RecipientType.TO, new InternetAddress(email));
        message.setSubject("Your grades have been updated");
        message.setText(inputMessage);
        Transport.send(message);
        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date date = new Date();
        emailSent = "E-mail was sent: " + dateFormat.format(date);
        System.out.println(emailSent);
    } catch (MessagingException e) {

        throw new RuntimeException(e);

    }
    //---Used code from https://www.youtube.com/watch?v=sHC8YgW21ho
    //---for the above method
}