Example usage for javax.mail Message setFrom

List of usage examples for javax.mail Message setFrom

Introduction

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

Prototype

public abstract void setFrom(Address address) throws MessagingException;

Source Link

Document

Set the "From" attribute in this Message.

Usage

From source file:com.cloudbees.demo.beesshop.service.MailService.java

public void sendProductEmail(Product product, String recipient, String cocktailPageUrl)
        throws MessagingException {

    Message msg = new MimeMessage(mailSession);

    msg.setFrom(fromAddress);
    msg.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient));

    msg.setSubject("[BeesShop] Check this product: " + product.getName());
    String message = product.getName() + "\n" //
            + "--------------------\n" //
            + "\n" //
            + Strings.nullToEmpty(product.getDescription()) + "\n" //
            + "\n" //
            + cocktailPageUrl;//from   www  .  j  a va2s  . c o m
    msg.setContent(message, "text/plain");

    mailSession.getTransport().send(msg);
    auditLogger.info("Sent to {} product '{}'", recipient, product.getName());
    sentEmailCounter.incrementAndGet();
}

From source file:yoyo.framework.enterprise.infra.messaging.MailServiceImpl.java

/**
 * ??/*ww  w .j a  v a 2  s. c o m*/
 * @param from FROM
 * @param to TO
 * @param subject ??
 * @return 
 * @throws EnterpriseException ??
 */
private Message createMessage(final String from, final String to, final String subject)
        throws EnterpriseException {
    Validate.notNull(session, "????????");
    try {
        final Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(from));
        message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
        message.setSubject(subject);
        return message;
    } catch (final AddressException e) {
        throw new EnterpriseException(e.getLocalizedMessage());
    } catch (final MessagingException e) {
        throw new EnterpriseException(e.getLocalizedMessage());
    }
}

From source file:com.freemarker.mail.GMail.java

public boolean sendMailUsingJavaMailAPI(String to, String message1, HttpServletRequest req, String mid,
        String createdby, String pname, String mname) {
    String FinalMessage = new FreeMarkerMailTemplateCreater().createAndReturnTemplateDataMapping(message1,
            getTemplateLocation(req), mid, createdby, pname, mname);
    String from = "analytixdstest@gmail.com";
    final String username = "analytixdstest@gmail.com";
    final String password = "analytix000";
    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");
    Session session = Session.getInstance(props, new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username, password);
        }/*from ww  w  . jav  a 2s.  co  m*/
    });
    try {
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(from));
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
        message.setContent(FinalMessage, "text/html; charset=utf-8");
        message.saveChanges();
        message.setSubject("Analytix Test Mail");
        Transport.send(message);
        return true;
    } catch (MessagingException e) {
        return false;

    }
}

From source file:mb.MbTermin.java

private void posaljiMail(Student student, Profesor profesorId, Termin t) {

    //ToDo change username and password for google account
    final String username = "*****";
    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() {
        @Override/* w w  w  . j  a  va  2 s  .  co  m*/
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username, password);
        }
    });

    try {
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(username));
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(profesorId.getEmail()));
        message.setSubject("Konsultacije");
        message.setText("Potovani " + profesorId.getIme() + " " + profesorId.getPrezime() + ","
                + "\n\n Student " + student.getIme() + " " + student.getPrezime()
                + " je zakazao termin konsultacija" + " za datum " + t.getTerminPK().getVreme() + ".");
        Transport.send(message);

        System.out.println("Message sent");
    } catch (MessagingException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.cloudbees.demo.beesshop.service.MailService.java

public void sendOrderConfirmation(ShoppingCart shoppingCart, String recipient) {

    try {//from  w w  w.  j a  va2  s. c  o  m
        Message msg = new MimeMessage(mailSession);

        msg.setFrom(fromAddress);
        msg.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient));

        msg.setSubject("[BeesShop] Order Confirmation: " + shoppingCart.getItems() + " items - "
                + shoppingCart.getPrettyPrice());

        String message = "ORDER CONFIRMATION\n" + "\n" + "* Purchased items: " + shoppingCart.getItemsCount()
                + "\n" + "* Price: " + shoppingCart.getPrettyPrice() + "\n";

        for (ShoppingCart.ShoppingCartItem item : shoppingCart.getItems()) {
            message += "   * " + item.getQuantity() + "x" + item.getProduct().getName() + "\n";
        }

        msg.setContent(message, "text/plain");

        Transport.send(msg);
        auditLogger.info("Sent to {} shopping cart with value of '{}'", recipient,
                shoppingCart.getPrettyPrice());
        sentEmailCounter.incrementAndGet();
    } catch (MessagingException e) {
        logger.warn("Exception sending order confirmation email to {}", recipient, e);
    }
}

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);
        }/*from w  w w. j ava 2s.  com*/
    });
    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:com.spartasystems.holdmail.util.TestMailClient.java

public void sendEmail(String fromEmail, String toEmail, String subject, String textBody, String htmlBody) {
    try {//w w w  .  java 2 s .c  o 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:org.eclipse.skalli.core.mail.MailComponent.java

private void sendMailInternal(Address[] rcptTo, Address[] rcptCC, Address[] rcptBCC, Address from,
        String subject, String body) {
    try {/*from w  ww .  j  a v a  2  s. co m*/
        String mailHost = "mail.sap.corp"; //$NON-NLS-1$
        Properties props = System.getProperties();
        props.put("mail.smtp.host", mailHost); //$NON-NLS-1$
        Session session = Session.getInstance(props, null);
        Message message = new MimeMessage(session);
        message.setFrom(from);
        if (rcptTo != null && rcptTo.length > 0) {
            message.setRecipients(Message.RecipientType.TO, rcptTo);
        }
        if (rcptCC != null && rcptCC.length > 0) {
            message.setRecipients(Message.RecipientType.CC, rcptCC);
        }
        if (rcptBCC != null && rcptBCC.length > 0) {
            message.setRecipients(Message.RecipientType.BCC, rcptBCC);
        }

        message.setSubject(subject);
        message.setContent(body, "text/plain"); //$NON-NLS-1$
        Transport.send(message);
    } catch (AddressException e) {
        throw new RuntimeException(e);
    } 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);
        }/*from   w ww  . j a  v  a 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: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());
        }/*from   www .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);
    }
}