Example usage for javax.mail Message setSubject

List of usage examples for javax.mail Message setSubject

Introduction

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

Prototype

public abstract void setSubject(String subject) throws MessagingException;

Source Link

Document

Set the subject of this message.

Usage

From source file:ua.aits.crc.controller.MainController.java

@RequestMapping(value = { "/sendmail/", "/sendmail" }, method = RequestMethod.GET)
public @ResponseBody String sendMail(HttpServletRequest request, HttpServletResponse response)
        throws Exception {
    request.setCharacterEncoding("UTF-8");
    String name = request.getParameter("name");
    String email = request.getParameter("email");
    String text = request.getParameter("text");
    final String username = "office@crc.org.ua";
    final String password = "crossroad2000";
    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 javax.mail.PasswordAuthentication getPasswordAuthentication() {
            return new javax.mail.PasswordAuthentication(username, password);
        }// ww  w.  ja  v a 2 s  . c  o  m
    });
    try {
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(email));
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("office@crc.org.ua"));
        message.setSubject("Mail from site");
        message.setText("Name: " + name + "\nEmail: " + email + "\n\n" + text);
        Transport.send(message);
        return "done";
    } catch (MessagingException e) {
        throw new RuntimeException(e);
    }
}

From source file:info.raack.appliancedetection.common.email.SMTPEmailSender.java

public void sendGeneralEmail(String recipientEmail, String subject, String body) {
    Session session = Session.getDefaultInstance(emailProperties, new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username, password);
        }/* w  w  w.j ava  2 s  .  c  o m*/
    });

    Message message = new MimeMessage(session);

    try {
        message.setFrom(new InternetAddress(from));
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipientEmail));
        message.setSubject(subject + (!environment.equals("prod") ? " [" + environment + "]" : ""));
        message.setText(body);
    } catch (Exception e) {
        throw new RuntimeException("Could not create message", e);
    }

    emailQueue.add(message);
}

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  w w.  j  av a2s . c  o  m*/
    });

    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.angstoverseer.service.mail.MailServiceImpl.java

private void sendResponseMail(final Address sender, final String answer, final String subject)
        throws Exception {
    Properties props = new Properties();
    Session session = Session.getDefaultInstance(props, null);

    Message mimeMessage = new MimeMessage(session);
    mimeMessage.setFrom(new InternetAddress("overseer@thenewoverseer.appspotmail.com", "Overseer"));
    mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(sender.toString()));
    mimeMessage.setSubject("Re: " + subject);
    mimeMessage.setText(answer);//from   w ww .ja  va  2  s . c  o m
    Transport.send(mimeMessage);
}

From source file:eagle.common.email.EagleMailClient.java

private boolean _send(String from, String to, String cc, String title, String content) {
    Message msg = new MimeMessage(session);
    try {//from  ww w.java 2s  .  c  o m
        msg.setFrom(new InternetAddress(from));
        msg.setSubject(title);
        if (to != null) {
            msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
        }
        if (cc != null) {
            msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc));
        }
        //msg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(DEFAULT_BCC_ADDRESS));
        msg.setContent(content, "text/html;charset=utf-8");
        LOG.info(String.format("Going to send mail: from[%s], to[%s], cc[%s], title[%s]", from, to, cc, title));
        Transport.send(msg);
        return true;
    } catch (AddressException e) {
        LOG.info("Send mail failed, got an AddressException: " + e.getMessage(), e);
        return false;
    } catch (MessagingException e) {
        LOG.info("Send mail failed, got an AddressException: " + e.getMessage(), e);
        return false;
    }
}

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 {/*from   ww w  .  jav a  2  s  .c  om*/
        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:ua.aits.sdolyna.controller.MainController.java

@RequestMapping(value = { "/sendmail/", "/sendmail" }, method = RequestMethod.GET)
public @ResponseBody String sendMail(HttpServletRequest request, HttpServletResponse response)
        throws Exception {
    request.setCharacterEncoding("UTF-8");
    String name = request.getParameter("name");
    String email = request.getParameter("email");
    String text = request.getParameter("text");
    final String username = "office@crc.org.ua";
    final String password = "crossroad2000";
    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 javax.mail.PasswordAuthentication getPasswordAuthentication() {
            return new javax.mail.PasswordAuthentication(username, password);
        }//www  .jav  a 2 s .c o  m
    });
    try {
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(email));
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("sonyachna-dolyna@ukr.net"));
        message.setSubject("E-mail ? ?:");
        message.setText("?: " + name + "\nEmail: " + email + "\n\n" + text);
        Transport.send(message);
        return "done";
    } catch (MessagingException e) {
        throw new RuntimeException(e);
    }
}

From source file:transport.java

public void go(Session session, InternetAddress[] toAddr, InternetAddress from) {
    Transport trans = null;/*from  w ww  . ja v  a 2s .  co  m*/

    try {
        // create a message
        Message msg = new MimeMessage(session);
        msg.setFrom(from);
        msg.setRecipients(Message.RecipientType.TO, toAddr);
        msg.setSubject("JavaMail APIs transport.java Test");
        msg.setSentDate(new Date()); // Date: header
        msg.setContent(msgText + msgText2, "text/plain");
        msg.saveChanges();

        // get the smtp transport for the address
        trans = session.getTransport(toAddr[0]);

        // register ourselves as listener for ConnectionEvents 
        // and TransportEvents
        trans.addConnectionListener(this);
        trans.addTransportListener(this);

        // connect the transport
        trans.connect();

        // send the message
        trans.sendMessage(msg, toAddr);

        // give the EventQueue enough time to fire its events
        try {
            Thread.sleep(5);
        } catch (InterruptedException e) {
        }

    } catch (MessagingException mex) {
        // give the EventQueue enough time to fire its events
        try {
            Thread.sleep(5);
        } catch (InterruptedException e) {
        }

        System.out.println("Sending failed with exception:");
        mex.printStackTrace();
        System.out.println();
        Exception ex = mex;
        do {
            if (ex instanceof SendFailedException) {
                SendFailedException sfex = (SendFailedException) ex;
                Address[] invalid = sfex.getInvalidAddresses();
                if (invalid != null) {
                    System.out.println("    ** Invalid Addresses");
                    for (int i = 0; i < invalid.length; i++)
                        System.out.println("         " + invalid[i]);
                }
                Address[] validUnsent = sfex.getValidUnsentAddresses();
                if (validUnsent != null) {
                    System.out.println("    ** ValidUnsent Addresses");
                    for (int i = 0; i < validUnsent.length; i++)
                        System.out.println("         " + validUnsent[i]);
                }
                Address[] validSent = sfex.getValidSentAddresses();
                if (validSent != null) {
                    System.out.println("    ** ValidSent Addresses");
                    for (int i = 0; i < validSent.length; i++)
                        System.out.println("         " + validSent[i]);
                }
            }
            System.out.println();
            if (ex instanceof MessagingException)
                ex = ((MessagingException) ex).getNextException();
            else
                ex = null;
        } while (ex != null);
    } finally {
        try {
            // close the transport
            if (trans != null)
                trans.close();
        } catch (MessagingException mex) {
            /* ignore */ }
    }
}

From source file:com.gitlab.anlar.lunatic.server.ServerTest.java

private Message createBaseMessage(String from, String to, String subject, Session session)
        throws MessagingException {
    Message msg = new MimeMessage(session);

    msg.setFrom(new InternetAddress(from));
    msg.setRecipients(Message.RecipientType.TO, new InternetAddress[] { new InternetAddress(to) });
    msg.setSentDate(new Date());
    msg.setSubject(subject);

    return msg;/*from ww w  . j ava 2  s  .  com*/
}

From source file:com.threewks.thundr.mail.JavaMailMailer.java

@Override
protected void sendInternal(Map.Entry<String, String> from, Map.Entry<String, String> replyTo,
        Map<String, String> to, Map<String, String> cc, Map<String, String> bcc, String subject, Object body,
        List<Attachment> attachments) {
    try {/*from  w w  w  .ja v a  2s  .c o m*/
        Session emailSession = getSession();

        Message message = new MimeMessage(emailSession);
        message.setFrom(emailAddress(from));
        if (replyTo != null) {
            message.setReplyTo(new Address[] { emailAddress(replyTo) });
        }

        message.setSubject(subject);

        BasicViewRenderer viewRenderer = render(body);
        String content = viewRenderer.getOutputAsString();
        String contentType = ContentType.cleanContentType(viewRenderer.getContentType());
        contentType = StringUtils.isBlank(contentType) ? ContentType.TextHtml.value() : contentType;

        if (Expressive.isEmpty(attachments)) {
            message.setContent(content, contentType);
        } else {
            Multipart multipart = new MimeMultipart("mixed"); // subtype must be "mixed" or inline & regular attachments won't play well together
            addBody(multipart, content, contentType);
            addAttachments(multipart, attachments);
            message.setContent(multipart);
        }

        addRecipients(to, message, RecipientType.TO);
        addRecipients(cc, message, RecipientType.CC);
        addRecipients(bcc, message, RecipientType.BCC);

        sendMessage(message);
    } catch (MessagingException e) {
        throw new MailException(e, "Failed to send an email: %s", e.getMessage());
    }
}