List of usage examples for javax.mail.internet MimeMessage getAllRecipients
@Override public Address[] getAllRecipients() throws MessagingException
From source file:com.haulmont.cuba.core.app.EmailerTest.java
@Test public void testSimpleParamList() throws Exception { testMailSender.clearBuffer();/*from ww w . j a va2s.c o m*/ emailer.sendEmail("myemail@example.com", "Test Email", "Test Body 2"); assertEquals(1, testMailSender.getBufferSize()); MimeMessage msg = testMailSender.fetchSentEmail(); assertEquals(1, msg.getAllRecipients().length); assertEquals("myemail@example.com", msg.getAllRecipients()[0].toString()); assertEquals("Test Email", msg.getSubject()); assertEquals("Test Body 2", getBody(msg)); assertTrue(getBodyContentType(msg).startsWith("text/plain;")); }
From source file:com.haulmont.cuba.core.app.EmailerTest.java
private void doTestSynchronous(boolean useFs) throws Exception { emailerConfig.setFileStorageUsed(useFs); testMailSender.clearBuffer();//w w w .j av a2 s.c o m EmailInfo myInfo = new EmailInfo("testemail@example.com", "Test Email", "Test Body"); emailer.sendEmail(myInfo); assertEquals(1, testMailSender.getBufferSize()); MimeMessage msg = testMailSender.fetchSentEmail(); assertEquals(1, msg.getAllRecipients().length); assertEquals("testemail@example.com", msg.getAllRecipients()[0].toString()); assertEquals("Test Email", msg.getSubject()); assertEquals("Test Body", getBody(msg)); assertTrue(getBodyContentType(msg).startsWith("text/plain;")); }
From source file:org.apache.james.mailetcontainer.impl.JamesMailetContext.java
/** * Place a mail on the spool for processing * * @param message the message to send//w w w .j av a 2s . com * @throws MessagingException if an exception is caught while placing the mail on the spool */ @Override public void sendMail(MimeMessage message) throws MessagingException { MailAddress sender = new MailAddress((InternetAddress) message.getFrom()[0]); Collection<MailAddress> recipients = new HashSet<MailAddress>(); Address addresses[] = message.getAllRecipients(); if (addresses != null) { for (Address address : addresses) { // Javamail treats the "newsgroups:" header field as a // recipient, so we want to filter those out. if (address instanceof InternetAddress) { recipients.add(new MailAddress((InternetAddress) address)); } } } sendMail(sender, recipients, message); }
From source file:mx.unam.pixel.controller.MailController.java
public void enviaMensaje(String mensaje) { try {//from w w w . j a v a 2 s . co m Properties props = new Properties(); // Nombre del host de correo, es smtp.gmail.com props.setProperty("mail.smtp.host", "smtp.gmail.com"); // TLS si est disponible props.setProperty("mail.smtp.starttls.enable", "true"); // Puerto de gmail para envio de correos props.setProperty("mail.smtp.port", "587"); // Nombre del usuario props.setProperty("mail.smtp.user", "ejemplo@gmail.com"); // Si requiere o no usuario y password para conectarse. props.setProperty("mail.smtp.auth", "true"); // Preparamos la sesion Session session = Session.getDefaultInstance(props); //session.setDebug(true); //Construimos el mensage MimeMessage message = new MimeMessage(session); InternetAddress cuenta = new InternetAddress("enrique.wps@gmail.com"); message.setFrom(); //Correo electronico que manda el mensaja message.addRecipient(Message.RecipientType.TO, new InternetAddress("vampa@ciencias.unam.mx")); message.setSubject("Test MUFFIN"); message.setText("Test Muffin Cuerpo"); // Abrimos la comunicacion Transport t = session.getTransport("smtp"); t.connect("dasds@gmail.com", "asdsadas"); // Ususario y contrasea t.sendMessage(message, message.getAllRecipients()); // Cierre t.close(); } catch (AddressException ex) { System.err.println("er"); } catch (MessagingException ex) { Logger.getLogger(MailController.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:com.zimbra.cs.mailclient.smtp.SmtpTransportTest.java
@Test(timeout = 3000) public void send() 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").sendLine("250 OK").recvLine() // QUIT .sendLine("221 bye").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))); transport.sendMessage(msg, msg.getAllRecipients()); transport.close();/* w ww. j av a 2 s .c o m*/ 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.assertEquals("QUIT\r\n", server.replay()); Assert.assertNull(server.replay()); }
From source file:com.zimbra.cs.mailclient.smtp.SmtpTransportTest.java
@Test(timeout = 3000) public void quitNoResponse() 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").sendLine("250 OK").recvLine() // QUIT .build().start(PORT);/* w w w . ja va 2 s. c o m*/ 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))); transport.sendMessage(msg, msg.getAllRecipients()); 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.assertEquals("QUIT\r\n", server.replay()); Assert.assertNull(server.replay()); }
From source file:com.zimbra.cs.mailclient.smtp.SmtpTransportTest.java
@Test(timeout = 3000) public void multilineGreeting() throws Exception { server = MockTcpServer.scenario().sendLine("220-first line").sendLine("220 second line").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").sendLine("250 OK").recvLine() // QUIT .sendLine("221 bye").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))); transport.sendMessage(msg, msg.getAllRecipients()); transport.close();// w ww . j a va2 s . c o m 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.assertEquals("QUIT\r\n", server.replay()); Assert.assertNull(server.replay()); }
From source file:com.zimbra.cs.mailclient.smtp.SmtpTransportTest.java
@Test(timeout = 3000) public void mailSmtpFrom() 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").sendLine("250 OK").recvLine() // QUIT .sendLine("221 bye").build().start(PORT); Session session = JMSession.getSession(); session.getProperties().setProperty("mail.smtp.from", "from@zimbra.com"); 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))); transport.sendMessage(msg, msg.getAllRecipients()); transport.close();// ww w . jav a2 s.c om server.shutdown(1000); Assert.assertEquals("EHLO localhost\r\n", server.replay()); Assert.assertEquals("MAIL FROM:<from@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.assertEquals("QUIT\r\n", server.replay()); Assert.assertNull(server.replay()); }
From source file:com.zimbra.cs.mailclient.smtp.SmtpTransportTest.java
@Test(timeout = 3000) public void authPlain() throws Exception { server = MockTcpServer.scenario().sendLine("220 test ready").recvLine() // EHLO .sendLine("250-smtp.zimbra.com").sendLine("250 AUTH PLAIN").recvLine() // AUTH PLAIN initial-response .sendLine("235 Authentication successful").recvLine() // MAIL FROM .sendLine("250 OK").recvLine() // RCPT TO .sendLine("250 OK").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(); Transport transport = session.getTransport("smtp"); transport.connect("localhost", PORT, "zimbra", "secret"); 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))); transport.sendMessage(msg, msg.getAllRecipients()); transport.close();// w w w . jav a 2 s .c om server.shutdown(1000); Assert.assertEquals("EHLO localhost\r\n", server.replay()); Assert.assertEquals("AUTH PLAIN " + base64("\0zimbra\0secret") + "\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.assertEquals("QUIT\r\n", server.replay()); Assert.assertNull(server.replay()); }
From source file:com.zimbra.cs.mailclient.smtp.SmtpTransportTest.java
@Test(timeout = 3000) public void endOfData() 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").recvUntil("\r\n.\r\n").sendLine("250 OK").recvLine() // QUIT .sendLine("221 bye").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\n" + ".\n" + "..\n" + ".\n"; MimeMessage msg = new ZMimeMessage(session, new SharedByteArrayInputStream(raw.getBytes(Charsets.ISO_8859_1))); transport.sendMessage(msg, msg.getAllRecipients()); transport.close();// ww w . ja va2s.co m 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.assertEquals("From: sender@zimbra.com\r\nTo: rcpt@zimbra.com\r\nSubject: test\r\n\r\n" + "..\r\n" + "...\r\n" + "..\r\n" + ".\r\n", server.replay()); Assert.assertEquals("QUIT\r\n", server.replay()); Assert.assertNull(server.replay()); }