List of usage examples for javax.mail.internet MimeMessage setSender
public void setSender(Address address) throws MessagingException
From source file:org.grouter.common.mail.MailHandler.java
/** * @param mimeMessage//from w w w . j a v a 2s . c o m * @throws UnsupportedEncodingException * @throws MessagingException */ private void createHeader(MimeMessage mimeMessage, MailDto mailDto) throws UnsupportedEncodingException, MessagingException { //from InternetAddress fromAddress = new InternetAddress(mailDto.getFrom()); fromAddress.setPersonal(mailDto.getSenderNote(), ENCODING); mimeMessage.setSender(fromAddress); //to List<String> senders = mailDto.getTo(); InternetAddress[] toAddress = getRecipients(senders); List<String> sendersCc = mailDto.getCc(); InternetAddress[] toAddressCc = getRecipients(sendersCc); List<String> sendersBcc = mailDto.getBcc(); InternetAddress[] toAddressBcc = getRecipients(sendersBcc); mimeMessage.setRecipients(Message.RecipientType.TO, toAddress); mimeMessage.setRecipients(Message.RecipientType.CC, toAddressCc); mimeMessage.setRecipients(Message.RecipientType.BCC, toAddressBcc); //subject mimeMessage.setSubject(mailDto.getSubject(), ENCODING); //sent date mimeMessage.setSentDate(new Date()); }
From source file:org.openmrs.notification.mail.MailMessageSender.java
/** * Converts the message object to a mime message in order to prepare it to be sent. * * @param message/*from w ww . j av a 2s . co m*/ * @return MimeMessage */ public MimeMessage createMimeMessage(Message message) throws Exception { if (message.getRecipients() == null) { throw new MessageException("Message must contain at least one recipient"); } // set the content-type to the default if it isn't defined in Message if (!StringUtils.hasText(message.getContentType())) { String contentType = Context.getAdministrationService().getGlobalProperty("mail.default_content_type"); message.setContentType(StringUtils.hasText(contentType) ? contentType : "text/plain"); } MimeMessage mimeMessage = new MimeMessage(session); // TODO Need to test the null case. // Transport should use default mail.from value defined in properties. if (message.getSender() != null) { mimeMessage.setSender(new InternetAddress(message.getSender())); } mimeMessage.setRecipients(javax.mail.Message.RecipientType.TO, InternetAddress.parse(message.getRecipients(), false)); mimeMessage.setSubject(message.getSubject()); if (!message.hasAttachment()) { mimeMessage.setContent(message.getContent(), message.getContentType()); } else { mimeMessage.setContent(createMultipart(message)); } return mimeMessage; }
From source file:org.opens.kbaccess.controller.utils.AMailerController.java
/** * Send a mail to the specified recipients. * //w w w . jav a 2s . c om * @param subject The mail's subject * @param message The mail's body * @param recipients The adressee * @return true if the send succeed, * false otherwise */ public boolean sendMail(String subject, String message, String[] recipients) { Session session; MimeMessage mimeMessage; Properties properties; // sanity check if (recipients.length == 0) { return true; } // set-up session properties = new Properties(); properties.put("mail.smtp.host", mailingServiceProperties.getSmtpHost()); session = Session.getDefaultInstance(properties); //session.setDebug(true); try { Address from; Address[] to = new InternetAddress[recipients.length]; // set sender from = new InternetAddress(mailingServiceProperties.getDefaultReturnAddress()); // initialize recipients list for (int i = 0; i < to.length; ++i) { to[i] = new InternetAddress(recipients[i]); } // create message mimeMessage = new MimeMessage(session); mimeMessage.setSender(from); mimeMessage.setFrom(from); mimeMessage.setReplyTo(new Address[] { from }); mimeMessage.setRecipients(Message.RecipientType.TO, to); mimeMessage.setSubject(subject); mimeMessage.setText(message, "utf-8"); // send it Transport.send(mimeMessage); } catch (MessagingException ex) { LogFactory.getLog(GuestController.class).error("Unable to send email", ex); return false; } return true; }