List of usage examples for javax.mail.internet MimeMessage getAllRecipients
@Override public Address[] getAllRecipients() throws MessagingException
From source file:com.zimbra.cs.mailclient.smtp.SmtpTransportTest.java
@Test(timeout = 3000) public void sendPartially() throws Exception { server = MockTcpServer.scenario().sendLine("220 test ready").recvLine() // EHLO .sendLine("250 OK").recvLine() // MAIL FROM .sendLine("250 OK").recvLine() // RCPT TO 1 .sendLine("250 OK").recvLine() // RCPT TO 2 .sendLine("550 not found").recvLine() // RCPT TO 3 .sendLine("550 not found").recvLine() // DATA .sendLine("354 OK").swallowUntil("\r\n.\r\n").sendLine("250 OK").recvLine() // QUIT .sendLine("221 bye").build().start(PORT); Session session = JMSession.getSession(); session.getProperties().setProperty("mail.smtp.sendpartial", "true"); Transport transport = session.getTransport("smtp"); transport.connect("localhost", PORT, null, null); String raw = "From: sender@zimbra.com\n" + "To: rcpt1@zimbra.com, rcpt2@zimbra.com, rcpt3@zimbra.com\nSubject: test\n\ntest"; MimeMessage msg = new ZMimeMessage(session, new SharedByteArrayInputStream(raw.getBytes(Charsets.ISO_8859_1))); try {/* w ww . j a v a2s .com*/ transport.sendMessage(msg, msg.getAllRecipients()); } catch (SendFailedException e) { Assert.assertEquals(1, e.getValidSentAddresses().length); Assert.assertEquals(0, e.getValidUnsentAddresses().length); Assert.assertEquals(2, e.getInvalidAddresses().length); } finally { transport.close(); } server.shutdown(1000); Assert.assertEquals("EHLO localhost\r\n", server.replay()); Assert.assertEquals("MAIL FROM:<sender@zimbra.com>\r\n", server.replay()); Assert.assertEquals("RCPT TO:<rcpt1@zimbra.com>\r\n", server.replay()); Assert.assertEquals("RCPT TO:<rcpt2@zimbra.com>\r\n", server.replay()); Assert.assertEquals("RCPT TO:<rcpt3@zimbra.com>\r\n", server.replay()); Assert.assertEquals("DATA\r\n", server.replay()); Assert.assertEquals("QUIT\r\n", server.replay()); Assert.assertNull(server.replay()); }
From source file:com.predic8.membrane.core.interceptor.authentication.session.EmailTokenProvider.java
private void sendEmail(String sender, String recipient, String subject, String text) { try {/*from w w w . ja v a 2 s.c o m*/ Properties props = System.getProperties(); props.put("mail.smtp.port", "" + smtpPort); props.put("mail.smtp.socketFactory.port", "" + smtpPort); if (ssl) { props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.starttls.required", "true"); } if (smtpUser != null) { props.put("mail.smtp.auth", "true"); } Session session = Session.getInstance(props, null); final MimeMessage msg = new MimeMessage(session); msg.setFrom(new InternetAddress(sender)); msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipient, false)); msg.setSubject(subject); msg.setText(text, Constants.UTF_8); msg.setSentDate(new Date()); SMTPTransport t = (SMTPTransport) session.getTransport(ssl ? "smtps" : "smtp"); t.connect(smtpHost, smtpUser, smtpPassword); t.sendMessage(msg, msg.getAllRecipients()); t.close(); } catch (MessagingException e) { throw new RuntimeException(e); } }
From source file:com.zimbra.cs.mailclient.smtp.SmtpConnection.java
/** * Sends the message.//from w ww.j av a2 s . com * <p> * Uses the sender and recipients from the headers in the {@link MimeMessage}. * * @see #sendMessage(String, String[], MimeMessage) */ public void sendMessage(MimeMessage msg) throws IOException, MessagingException { sendMessage(getSender(msg), toString(msg.getAllRecipients()), msg); }
From source file:com.otz.plugins.transport.aws.ses.SpringEmailSender.java
public void send(String templateName, Locale locale, String to, Map<String, String> parameters) throws MessagingException { MimeMessage msg = new MimeMessage(session); msg.setFrom(new InternetAddress(emailTemplateRepository.getFrom(templateName, locale))); msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to)); msg.setSubject(emailTemplateRepository.getSubject(templateName, locale)); msg.setContent(emailTemplateRepository.getContent(templateName, locale, parameters), emailTemplateRepository.getContentType(templateName, locale)); try {//from ww w .j a v a2 s .co m // Create a transport. transport = session.getTransport(); transport.connect(emailConfigParameters.getHost(), emailConfigParameters.getAccessKey(), emailConfigParameters.getSecretKey()); transport.sendMessage(msg, msg.getAllRecipients()); } catch (Exception e) { // TODO deal with failure to send // send message to hipchat e.printStackTrace(); } }
From source file:edu.harvard.med.screensaver.service.SmtpEmailService.java
private void sendMessage(String subject, String message, InternetAddress from, InternetAddress[] recipients, InternetAddress[] cclist, File attachedFile) throws MessagingException, IOException { log.info("try to send: " + printEmail(subject, message, from, this.replyTos, recipients, cclist, (attachedFile == null ? "" : "" + attachedFile.getCanonicalFile()))); log.info("host: " + host + ", useSMTPS: " + useSmtps); Properties props = new Properties(); String protocol = "smtp"; if (useSmtps) // need smtps to test with gmail {//from www . ja v a 2s . c om props.put("mail.smtps.auth", "true"); protocol = "smtps"; } Session session = Session.getDefaultInstance(props, null); Transport t = session.getTransport(protocol); try { MimeMessage msg = new MimeMessage(session); if (this.replyTos != null) msg.setReplyTo(replyTos); msg.setFrom(from); msg.setSubject(subject); if (attachedFile != null) { setFileAsAttachment(msg, message, attachedFile); } else { msg.setContent(message, "text/plain"); } msg.addRecipients(Message.RecipientType.TO, recipients); if (cclist != null) msg.addRecipients(Message.RecipientType.CC, cclist); t.connect(host, username, password); t.sendMessage(msg, msg.getAllRecipients()); } finally { t.close(); } log.info("sent: " + printEmailHeader(subject, from, this.replyTos, recipients, cclist, (attachedFile == null ? "" : "" + attachedFile.getCanonicalFile()))); }
From source file:eu.optimis.sm.gui.server.ServiceManagerWebServiceImpl.java
public static void Send(final String username, final String password, String recipientEmail, String ccEmail, String title, String message) throws AddressException, MessagingException { final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory"; Properties props = System.getProperties(); props.setProperty("mail.smtps.host", "smtp.gmail.com"); props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY); props.setProperty("mail.smtp.socketFactory.fallback", "false"); props.setProperty("mail.smtp.port", "465"); props.setProperty("mail.smtp.socketFactory.port", "465"); props.setProperty("mail.smtps.auth", "true"); props.put("mail.smtps.quitwait", "false"); Session session = Session.getInstance(props, null); final MimeMessage msg = new MimeMessage(session); msg.setFrom(new InternetAddress(username + "@gmail.com")); msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipientEmail, false)); if (ccEmail.length() > 0) { msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(ccEmail, false)); }/*from w ww . j a v a 2s . c om*/ msg.setSubject(title); msg.setText(message, "utf-8"); msg.setSentDate(new Date()); SMTPTransport t = (SMTPTransport) session.getTransport("smtps"); t.connect("smtp.gmail.com", username, password); t.sendMessage(msg, msg.getAllRecipients()); t.close(); }
From source file:org.beanfuse.notification.mail.AbstractMailNotifier.java
public void sendMessage(Message msg) throws NotificationException { // contruct a MailMessage MailMessage mailConext = null;/*from w w w. j a v a 2s .c om*/ if (msg instanceof MailMessage) { mailConext = (MailMessage) msg; } else { mailConext = new MailMessage(); mailConext.setSubject(msg.getSubject()); mailConext.setText(msg.getText()); mailConext.setProperties(msg.getProperties()); String[] to = new String[msg.getRecipients().size()]; msg.getRecipients().toArray(to); mailConext.setTo(to); } MimeMessage mimeMsg = javaMailSender.createMimeMessage(); try { mimeMsg.setSentDate(new Date()); MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMsg); messageHelper.setText(buildText(mailConext), Message.HTML.equals(mailConext.getContentType())); String subject = buildSubject(mailConext); messageHelper.setSubject(subject); messageHelper.setFrom(fromMailbox, fromName); addRecipient(mimeMsg, javax.mail.Message.RecipientType.TO, mailConext.getTo()); addRecipient(mimeMsg, javax.mail.Message.RecipientType.CC, mailConext.getCc()); addRecipient(mimeMsg, javax.mail.Message.RecipientType.BCC, mailConext.getBcc()); beforeSend(mailConext, mimeMsg); if (mimeMsg.getAllRecipients() != null && ((Address[]) mimeMsg.getAllRecipients()).length > 0) { javaMailSender.send(mimeMsg); logger.info("mail sended from {} to {} with subject {}", new Object[] { fromMailbox, mailConext.getRecipients(), subject }); } } catch (AddressException ex) { throw new NotificationException("Exception while sending message.", ex); } catch (MessagingException ex) { throw new NotificationException("Exception while sending message.", ex); } catch (UnsupportedEncodingException ex) { throw new NotificationException("Exception while sending message.", ex); } afterSend(mailConext, mimeMsg); }
From source file:com.zimbra.cs.mailclient.smtp.SmtpTransportTest.java
@Test(timeout = 3000) public void dataException() throws Exception { server = MockTcpServer.scenario().sendLine("220 test ready").recvLine() // EHLO .sendLine("250 OK").recvLine() // MAIL FROM .sendLine("250 OK").recvLine() // RCPT TO .sendLine("250 OK").recvLine() // DATA .sendLine("354 OK").swallowUntil("\r\n.\r\n").build().start(PORT); Session session = JMSession.getSession(); Transport transport = session.getTransport("smtp"); transport.connect("localhost", PORT, null, null); String raw = "From: sender@zimbra.com\nTo: rcpt@zimbra.com\nSubject: test\n\ntest"; MimeMessage msg = new ZMimeMessage(session, new SharedByteArrayInputStream(raw.getBytes(Charsets.ISO_8859_1))) { @Override// w ww .jav a2 s .com public void writeTo(OutputStream os, String[] ignoreList) throws MessagingException { throw new MessagingException(); // exception while encoding } }; try { transport.sendMessage(msg, msg.getAllRecipients()); Assert.fail(); } catch (SendFailedException expected) { } finally { transport.close(); } server.shutdown(1000); Assert.assertEquals("EHLO localhost\r\n", server.replay()); Assert.assertEquals("MAIL FROM:<sender@zimbra.com>\r\n", server.replay()); Assert.assertEquals("RCPT TO:<rcpt@zimbra.com>\r\n", server.replay()); Assert.assertEquals("DATA\r\n", server.replay()); Assert.assertNull(server.replay()); }
From source file:hudson.tasks.MailSender.java
public boolean execute(AbstractBuild<?, ?> build, BuildListener listener) throws InterruptedException { try {//from w w w .j ava 2 s.com MimeMessage mail = getMail(build, listener); if (mail != null) { // if the previous e-mail was sent for a success, this new e-mail // is not a follow up AbstractBuild<?, ?> pb = build.getPreviousBuild(); if (pb != null && pb.getResult() == Result.SUCCESS) { mail.removeHeader("In-Reply-To"); mail.removeHeader("References"); } Address[] allRecipients = mail.getAllRecipients(); if (allRecipients != null) { StringBuilder buf = new StringBuilder("Sending e-mails to:"); for (Address a : allRecipients) buf.append(' ').append(a); listener.getLogger().println(buf); Transport.send(mail); build.addAction(new MailMessageIdAction(mail.getMessageID())); } else { listener.getLogger().println(Messages.MailSender_ListEmpty()); } } } catch (MessagingException e) { e.printStackTrace(listener.error(e.getMessage())); } catch (UnsupportedEncodingException e) { e.printStackTrace(listener.error(e.getMessage())); } finally { CHECKPOINT.report(); } return true; }
From source file:fr.xebia.cloud.amazon.aws.iam.AmazonAwsIamAccountCreator.java
private void sendEmail(Map<String, String> templatesParams, List<BodyPart> attachments, String toAddress) throws MessagingException { MimeBodyPart htmlAndPlainTextAlternativeBody = new MimeBodyPart(); // TEXT AND HTML MESSAGE (gmail requires plain text alternative, otherwise, it displays the 1st plain text attachment in the preview) MimeMultipart cover = new MimeMultipart("alternative"); htmlAndPlainTextAlternativeBody.setContent(cover); BodyPart textHtmlBodyPart = new MimeBodyPart(); String textHtmlBody = FreemarkerUtils.generate(templatesParams, "/fr/xebia/cloud/amazon/aws/iam/amazon-aws-iam-credentials-email-" + environment.getIdentifier() + ".html.ftl"); textHtmlBodyPart.setContent(textHtmlBody, "text/html"); cover.addBodyPart(textHtmlBodyPart); BodyPart textPlainBodyPart = new MimeBodyPart(); cover.addBodyPart(textPlainBodyPart); String textPlainBody = FreemarkerUtils.generate(templatesParams, "/fr/xebia/cloud/amazon/aws/iam/amazon-aws-iam-credentials-email-" + environment.getIdentifier() + ".txt.ftl"); textPlainBodyPart.setContent(textPlainBody, "text/plain"); MimeMultipart content = new MimeMultipart("related"); content.addBodyPart(htmlAndPlainTextAlternativeBody); // ATTACHMENTS for (BodyPart bodyPart : attachments) { content.addBodyPart(bodyPart);/*from w ww .ja v a 2 s .c o m*/ } MimeMessage msg = new MimeMessage(mailSession); msg.setFrom(mailFrom); msg.addRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress(toAddress)); msg.addRecipient(javax.mail.Message.RecipientType.CC, mailFrom); String subject = "[Xebia Amazon AWS " + environment.getIdentifier() + "] Credentials"; msg.setSubject(subject); msg.setContent(content); mailTransport.sendMessage(msg, msg.getAllRecipients()); }