List of usage examples for javax.mail Message addRecipient
public void addRecipient(RecipientType type, Address address) throws MessagingException
From source file:com.app.mail.DefaultMailSender.java
private static Message _populateContactMessage(String emailAddress, String messageBody, Session session) throws Exception { Message message = new MimeMessage(session); message.setFrom(new InternetAddress(emailAddress)); message.addRecipient(Message.RecipientType.TO, new InternetAddress(PropertiesValues.OUTBOUND_EMAIL_ADDRESS)); message.setSubject("You Have A New Message From " + emailAddress); message.setText(messageBody);//from w w w.j a v a 2 s. c o m return message; }
From source file:org.vulpe.commons.util.VulpeEmailUtil.java
/** * Send mail to recipients by Web Service. * * @param recipients/*ww w . j a va2s . c o m*/ * * @param subject * * @param body * * @param mailerService * * @throws VulpeSystemException * exception */ public static void sendMailByService(final String[] recipients, final String subject, final String body, final String mailerService) { try { final ResourceBundle bundle = ResourceBundle.getBundle("mail"); String mailFrom = ""; if (bundle.containsKey("mail.from")) { mailFrom = bundle.getString("mail.from"); } final InitialContext initialContext = new InitialContext(); final Session session = (Session) initialContext.lookup(mailerService); final Message message = new MimeMessage(session); message.setFrom(new InternetAddress(mailFrom)); for (String recipient : recipients) { message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient)); } // msg.setRecipient(Message.RecipientType.TO, new // InternetAddress(to)); message.setSubject(subject); message.setText(body); Transport.send(message); } catch (Exception e) { LOG.error(e.getMessage()); } }
From source file:dk.netarkivet.common.utils.EMailUtils.java
/** * Send an email, possibly forgiving errors. * * @param to The recipient of the email. Separate multiple recipients with * commas. Supports only adresses of the type 'john@doe.dk', not * 'John Doe <john@doe.dk>' * @param from The sender of the email.//w w w . ja v a 2 s . c o m * @param subject The subject of the email. * @param body The body of the email. * @param forgive On true, will send the email even on invalid email * addresses, if at least one recipient can be set, on false, will * throw exceptions on any invalid email address. * * * @throws ArgumentNotValid If either parameter is null, if to, from or * subject is the empty string, or no recipient * can be set. If "forgive" is false, also on * any invalid to or from address. * @throws IOFailure If the message cannot be sent for some reason. */ public static void sendEmail(String to, String from, String subject, String body, boolean forgive) { ArgumentNotValid.checkNotNullOrEmpty(to, "String to"); ArgumentNotValid.checkNotNullOrEmpty(from, "String from"); ArgumentNotValid.checkNotNullOrEmpty(subject, "String subject"); ArgumentNotValid.checkNotNull(body, "String body"); Properties props = new Properties(); props.put(MAIL_FROM_PROPERTY_KEY, from); props.put(MAIL_HOST_PROPERTY_KEY, Settings.get(CommonSettings.MAIL_SERVER)); Session session = Session.getDefaultInstance(props); Message msg = new MimeMessage(session); // to might contain more than one e-mail address for (String toAddressS : to.split(",")) { try { InternetAddress toAddress = new InternetAddress(toAddressS.trim()); msg.addRecipient(Message.RecipientType.TO, toAddress); } catch (AddressException e) { if (forgive) { log.warn("To address '" + toAddressS + "' is not a valid email " + "address", e); } else { throw new ArgumentNotValid("To address '" + toAddressS + "' is not a valid email " + "address", e); } } catch (MessagingException e) { if (forgive) { log.warn("To address '" + toAddressS + "' could not be set in email", e); } else { throw new ArgumentNotValid("To address '" + toAddressS + "' could not be set in email", e); } } } try { if (msg.getAllRecipients().length == 0) { throw new ArgumentNotValid("No valid recipients in '" + to + "'"); } } catch (MessagingException e) { throw new ArgumentNotValid("Message invalid after setting" + " recipients", e); } try { InternetAddress fromAddress = null; fromAddress = new InternetAddress(from); msg.setFrom(fromAddress); } catch (AddressException e) { throw new ArgumentNotValid("From address '" + from + "' is not a valid email " + "address", e); } catch (MessagingException e) { if (forgive) { log.warn("From address '" + from + "' could not be set in email", e); } else { throw new ArgumentNotValid("From address '" + from + "' could not be set in email", e); } } try { msg.setSubject(subject); msg.setContent(body, MIMETYPE); msg.setSentDate(new Date()); Transport.send(msg); } catch (MessagingException e) { throw new IOFailure("Could not send email with subject '" + subject + "' from '" + from + "' to '" + to + "'. Body:\n" + body, e); } }
From source file:org.openmrs.module.sync.SyncMailUtil.java
/** * Sends a message using the current mail session *///from w ww. ja v a2 s . co m public static void sendMessage(String recipients, String subject, String body) throws MessageException { try { Message message = new MimeMessage(getMailSession()); message.setSentDate(new Date()); if (StringUtils.isNotBlank(subject)) { message.setSubject(subject); } if (StringUtils.isNotBlank(recipients)) { for (String recipient : recipients.split("\\,")) { message.addRecipient(MimeMessage.RecipientType.TO, new InternetAddress(recipient)); } } if (StringUtils.isNotBlank(body)) { Multipart multipart = new MimeMultipart(); MimeBodyPart contentBodyPart = new MimeBodyPart(); contentBodyPart.setContent(body, "text/html"); multipart.addBodyPart(contentBodyPart); message.setContent(multipart); } log.info("Sending email with subject <" + subject + "> to <" + recipients + ">"); log.debug("Mail has contents: \n" + body); Transport.send(message); log.debug("Message sent without errors"); } catch (Exception e) { log.error("Message could not be sent due to " + e.getMessage(), e); throw new MessageException(e); } }
From source file:fr.xebia.cocktail.MailService.java
public void sendCocktail(Cocktail cocktail, String recipient, String cocktailPageUrl) throws MessagingException { Message msg = new MimeMessage(mailSession); msg.setFrom(fromAddress);//from w w w . ja va 2s. c o m msg.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient)); msg.setSubject("[Cocktail] " + cocktail.getName()); String message = cocktail.getName() + "\n" // + "--------------------\n" // + "\n" // + Strings.nullToEmpty(cocktail.getInstructions()) + "\n" // + "\n" // + cocktailPageUrl; msg.setContent(message, "text/plain"); Transport.send(msg); auditLogger.info("Sent to {} cocktail '{}'", recipient, cocktail.getName()); sentEmailCounter.incrementAndGet(); }
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);//from w w w.j a v a 2 s. c om 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; msg.setContent(message, "text/plain"); mailSession.getTransport().send(msg); auditLogger.info("Sent to {} product '{}'", recipient, product.getName()); sentEmailCounter.incrementAndGet(); }
From source file:com.cloudbees.demo.beesshop.service.MailService.java
public void sendOrderConfirmation(ShoppingCart shoppingCart, String recipient) { try {/* w ww . j a v a 2 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:yoyo.framework.enterprise.infra.messaging.MailServiceImpl.java
/** * ??//from w w w . ja va2s . 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.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 w w . j av a2 s .c om*/ Transport.send(mimeMessage); }
From source file:org.mot.common.tools.EmailFactory.java
public void sendEmail(String recipient, String subject, String body) { if (enabled) { try {//w ww .j ava 2 s.c o m if (recipient == null || recipient == "") { recipient = rcpt; } // Create a default MimeMessage object. Message message = new MimeMessage(session); // Set From: header field of the header. message.setFrom(new InternetAddress(from)); // Set To: header field of the header. message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient)); // Set Subject: header field message.setSubject(subject); // Send the actual HTML message, as big as you like message.setText(body); // Send message Transport.send(message); //System.out.println("Sent message successfully...."); } catch (Exception e) { e.printStackTrace(); } } }