List of usage examples for javax.mail.internet MimeMessage setRecipients
public void setRecipients(Message.RecipientType type, String addresses) throws MessagingException
From source file:com.xpn.xwiki.plugin.mailsender.MailSenderPlugin.java
/** * Creates a MIME message (message with binary content carrying capabilities) from an existing Mail * /* w w w . j a va 2 s . co m*/ * @param mail The original Mail object * @param session Mail session * @return The MIME message */ private MimeMessage createMimeMessage(Mail mail, Session session, XWikiContext context) throws MessagingException, XWikiException, IOException { // this will also check for email error InternetAddress from = new InternetAddress(mail.getFrom()); String recipients = mail.getHeader("To"); if (StringUtils.isBlank(recipients)) { recipients = mail.getTo(); } else { recipients = mail.getTo() + "," + recipients; } InternetAddress[] to = toInternetAddresses(recipients); recipients = mail.getHeader("Cc"); if (StringUtils.isBlank(recipients)) { recipients = mail.getCc(); } else { recipients = mail.getCc() + "," + recipients; } InternetAddress[] cc = toInternetAddresses(recipients); recipients = mail.getHeader("Bcc"); if (StringUtils.isBlank(recipients)) { recipients = mail.getBcc(); } else { recipients = mail.getBcc() + "," + recipients; } InternetAddress[] bcc = toInternetAddresses(recipients); if ((to == null) && (cc == null) && (bcc == null)) { LOGGER.info("No recipient -> skipping this email"); return null; } MimeMessage message = new MimeMessage(session); message.setSentDate(new Date()); message.setFrom(from); if (to != null) { message.setRecipients(javax.mail.Message.RecipientType.TO, to); } if (cc != null) { message.setRecipients(javax.mail.Message.RecipientType.CC, cc); } if (bcc != null) { message.setRecipients(javax.mail.Message.RecipientType.BCC, bcc); } message.setSubject(mail.getSubject(), EMAIL_ENCODING); for (Map.Entry<String, String> header : mail.getHeaders().entrySet()) { message.setHeader(header.getKey(), header.getValue()); } if (mail.getHtmlPart() != null || mail.getAttachments() != null) { Multipart multipart = createMimeMultipart(mail, context); message.setContent(multipart); } else { message.setText(mail.getTextPart()); } message.setSentDate(new Date()); message.saveChanges(); return message; }
From source file:com.gamesalutes.utils.email.DefaultEmailServiceImpl.java
private void setEmailRecipients(MimeMessage msg, List<String> addresses, RecipientType type) throws EmailException { InternetAddress[] inetAddr = new InternetAddress[addresses.size()]; try {/* w ww .j a v a2 s. co m*/ for (int i = 0; i < addresses.size(); ++i) { String addr = addresses.get(i); if (MiscUtils.isEmpty(addr)) throw new IllegalArgumentException("model.get" + type + " has empty address at index:" + i); addr = addr.trim(); doValidateEmailAddress(addr); inetAddr[i] = new InternetAddress(addr); } msg.setRecipients(type, inetAddr); } catch (MessagingException e) { throw new EmailAddressException(e); } }
From source file:edu.harvard.iq.dataverse.MailServiceBean.java
public void sendMail(String from, String to, String subject, String messageText, Map<Object, Object> extraHeaders) { try {// w w w . j a v a 2s . c om MimeMessage msg = new MimeMessage(session); if (from.matches(EMAIL_PATTERN)) { msg.setFrom(new InternetAddress(from)); } else { // set fake from address; instead, add it as part of the message //msg.setFrom(new InternetAddress("invalid.email.address@mailinator.com")); msg.setFrom(getSystemAddress()); messageText = "From: " + from + "\n\n" + messageText; } msg.setSentDate(new Date()); msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false)); msg.setSubject(subject, charset); msg.setText(messageText, charset); if (extraHeaders != null) { for (Object key : extraHeaders.keySet()) { String headerName = key.toString(); String headerValue = extraHeaders.get(key).toString(); msg.addHeader(headerName, headerValue); } } Transport.send(msg); } catch (AddressException ae) { ae.printStackTrace(System.out); } catch (MessagingException me) { me.printStackTrace(System.out); } }
From source file:org.apache.mailet.base.test.MimeMessageBuilder.java
public MimeMessage build() throws MessagingException { Preconditions.checkState(!(text.isPresent() && content.isPresent()), "Can not get at the same time a text and a content"); MimeMessage mimeMessage = new MimeMessage(Session.getInstance(new Properties())); if (text.isPresent()) { BodyPart bodyPart = new MimeBodyPart(); bodyPart.setText(text.get());/*from w w w. j a va2 s . c o m*/ mimeMessage.setContent(bodyPart, "text/plain"); } if (content.isPresent()) { mimeMessage.setContent(content.get()); } if (sender.isPresent()) { mimeMessage.setSender(sender.get()); } if (from.isPresent()) { mimeMessage.setFrom(from.get()); } if (subject.isPresent()) { mimeMessage.setSubject(subject.get()); } List<InternetAddress> toAddresses = to.build(); if (!toAddresses.isEmpty()) { mimeMessage.setRecipients(Message.RecipientType.TO, toAddresses.toArray(new InternetAddress[toAddresses.size()])); } List<InternetAddress> ccAddresses = cc.build(); if (!ccAddresses.isEmpty()) { mimeMessage.setRecipients(Message.RecipientType.CC, ccAddresses.toArray(new InternetAddress[ccAddresses.size()])); } List<InternetAddress> bccAddresses = bcc.build(); if (!bccAddresses.isEmpty()) { mimeMessage.setRecipients(Message.RecipientType.BCC, bccAddresses.toArray(new InternetAddress[bccAddresses.size()])); } List<Header> headerList = headers.build(); for (Header header : headerList) { mimeMessage.addHeader(header.name, header.value); } mimeMessage.saveChanges(); return mimeMessage; }
From source file:edu.cornell.mannlib.vitro.webapp.controller.freemarker.ContactMailController.java
private void sendMessage(Session s, String webuseremail, String webusername, String[] recipients, String deliveryfrom, String msgText) throws AddressException, SendFailedException, MessagingException { // Construct the message MimeMessage msg = new MimeMessage(s); //System.out.println("trying to send message from servlet"); // Set the from address try {// w ww. j a v a2 s. c om msg.setFrom(new InternetAddress(webuseremail, webusername)); } catch (UnsupportedEncodingException e) { log.error("Can't set message sender with personal name " + webusername + " due to UnsupportedEncodingException"); msg.setFrom(new InternetAddress(webuseremail)); } // Set the recipient address InternetAddress[] address = new InternetAddress[recipients.length]; for (int i = 0; i < recipients.length; i++) { address[i] = new InternetAddress(recipients[i]); } msg.setRecipients(Message.RecipientType.TO, address); // Set the subject and text msg.setSubject(deliveryfrom); // add the multipart to the message msg.setContent(msgText, "text/html"); // set the Date: header msg.setSentDate(new Date()); Transport.send(msg); // try to send the message via smtp - catch error exceptions }
From source file:edu.harvard.iq.dataverse.MailServiceBean.java
public void sendMail(String host, String from, String to, String subject, String messageText) { Properties props = System.getProperties(); props.put("mail.smtp.host", host); Session session = Session.getDefaultInstance(props, null); try {// w ww .j a v a 2 s .c o m MimeMessage msg = new MimeMessage(session); String[] recipientStrings = to.split(","); InternetAddress[] recipients = new InternetAddress[recipientStrings.length]; try { msg.setFrom(new InternetAddress(from, charset)); for (int i = 0; i < recipients.length; i++) { recipients[i] = new InternetAddress(recipientStrings[i], "", charset); } } catch (UnsupportedEncodingException ex) { logger.severe(ex.getMessage()); } msg.setRecipients(Message.RecipientType.TO, recipients); msg.setSubject(subject, charset); msg.setText(messageText, charset); Transport.send(msg, recipients); } catch (AddressException ae) { ae.printStackTrace(System.out); } catch (MessagingException me) { me.printStackTrace(System.out); } }
From source file:org.apache.james.James.java
/** * Generates a bounce mail that is a bounce of the original message. * * @param bounceText the text to be prepended to the message to describe the bounce condition * * @return the bounce mail/*ww w .ja v a2 s. com*/ * * @throws MessagingException if the bounce mail could not be created */ private MailImpl rawBounce(Mail mail, String bounceText) throws MessagingException { //This sends a message to the james component that is a bounce of the sent message MimeMessage original = mail.getMessage(); MimeMessage reply = (MimeMessage) original.reply(false); reply.setSubject("Re: " + original.getSubject()); reply.setSentDate(new Date()); Collection recipients = new HashSet(); recipients.add(mail.getSender()); InternetAddress addr[] = { new InternetAddress(mail.getSender().toString()) }; reply.setRecipients(Message.RecipientType.TO, addr); reply.setFrom(new InternetAddress(mail.getRecipients().iterator().next().toString())); reply.setText(bounceText); reply.setHeader(RFC2822Headers.MESSAGE_ID, "replyTo-" + mail.getName()); return new MailImpl("replyTo-" + mail.getName(), new MailAddress(mail.getRecipients().iterator().next().toString()), recipients, reply); }
From source file:com.meltmedia.cadmium.email.TestingMessageTransformer.java
public MimeMessage transform(MimeMessage message) throws MessagingException { MimeBodyPart sentToBodyPart = newSentToBodyPart(message); MimeBodyPart originalBodyPart = newOriginalBodyPart(message); // create a new multipart content for this message. MimeMultipart multipart = new MimeMultipart(EmailUtil.SUBTYPE_MIXED); // add the parts to the body. multipart.addBodyPart(originalBodyPart); multipart.addBodyPart(sentToBodyPart); // get the new values for all of the headers. InternetAddress newFromAddress = newFromAddress(message); InternetAddress[] newToAddresses = newToAddresses(message); InternetAddress[] newCcAddresses = newCcAddresses(message); InternetAddress[] newBccAddresses = newBccAddresses(message); String newSubject = newSubject(message); // update the message. message.setFrom(newFromAddress);/*from www. j av a 2s . c om*/ message.setRecipients(Message.RecipientType.TO, newToAddresses); message.setRecipients(Message.RecipientType.CC, newCcAddresses); message.setRecipients(Message.RecipientType.BCC, newBccAddresses); message.setSubject(newSubject); message.setContent(multipart); // save the message. message.saveChanges(); if (getLogProperty()) { try { ByteArrayOutputStream out = new ByteArrayOutputStream(); message.writeTo(out); log.info("Email Message Sent:\n{}", out.toString()); } catch (IOException ioe) { throw new MessagingException("Exception thrown while writing message to log.", ioe); } } if (!getSendProperty()) { message = null; } // return the message. return message; }
From source file:org.tightblog.service.EmailService.java
/** * This method is used to send an HTML Message * * @param from e-mail address of sender * @param to e-mail address(es) of recipients * @param subject subject of e-mail// w w w .jav a2 s . co m * @param content the body of the e-mail */ private void sendMessage(String from, String[] to, String[] cc, String subject, String content) { try { MimeMessage message = mailSender.createMimeMessage(); // n.b. any default from address is expected to be determined by caller. if (!StringUtils.isEmpty(from)) { InternetAddress sentFrom = new InternetAddress(from); message.setFrom(sentFrom); log.debug("e-mail from: {}", sentFrom); } if (to != null) { InternetAddress[] sendTo = new InternetAddress[to.length]; for (int i = 0; i < to.length; i++) { sendTo[i] = new InternetAddress(to[i]); log.debug("sending e-mail to: {}", to[i]); } message.setRecipients(Message.RecipientType.TO, sendTo); } if (cc != null) { InternetAddress[] copyTo = new InternetAddress[cc.length]; for (int i = 0; i < cc.length; i++) { copyTo[i] = new InternetAddress(cc[i]); log.debug("copying e-mail to: {}", cc[i]); } message.setRecipients(Message.RecipientType.CC, copyTo); } message.setSubject((subject == null) ? "(no subject)" : subject, "UTF-8"); message.setContent(content, "text/html; charset=utf-8"); message.setSentDate(new java.util.Date()); // First collect all the addresses together. boolean bFailedToSome = false; SendFailedException sendex = new SendFailedException("Unable to send message to some recipients"); try { // Send to the list of remaining addresses, ignoring the addresses attached to the message mailSender.send(message); } catch (MailAuthenticationException | MailSendException ex) { bFailedToSome = true; sendex.setNextException(ex); } if (bFailedToSome) { throw sendex; } } catch (MessagingException e) { log.error("ERROR: Problem sending email with subject {}", subject, e); } }
From source file:userInterface.HospitalAdminRole.ManagePatientsJPanel.java
public void sendMailToCommunityMember(String to, String subject, String message, String from, String password) { String host = "smtp.gmail.com"; Properties props = System.getProperties(); 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); props.put("mail.smtp.user", from); Session session = Session.getDefaultInstance(props); MimeMessage mimeMessage = new MimeMessage(session); try {// w ww. jav a2 s.c o m mimeMessage.setFrom(new InternetAddress(from)); mimeMessage.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to)); mimeMessage.setSubject("Alert from Hospital Organization"); mimeMessage.setText(message); SMTPTransport transport = (SMTPTransport) session.getTransport("smtps"); transport.connect(host, from, password); transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients()); transport.close(); } catch (MessagingException me) { } }