List of usage examples for javax.mail Transport close
public synchronized void close() throws MessagingException
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(); server.shutdown(1000);/*w w w .j ava2 s . co m*/ 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()); }
From source file:com.zimbra.cs.mailclient.smtp.SmtpTransportTest.java
@Test(timeout = 3000) public void bracketsInMailAndRcpt() 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", "<>"); 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, new Address[] { new InternetAddress("<rcpt@zimbra.com>") }); transport.close(); server.shutdown(1000);//from w ww.j ava2 s . co m Assert.assertEquals("EHLO localhost\r\n", server.replay()); Assert.assertEquals("MAIL FROM:<>\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.vaushell.superpipes.dispatch.ErrorMailer.java
private void sendHTML(final String message) throws MessagingException, IOException { if (message == null || message.isEmpty()) { throw new IllegalArgumentException("message"); }//from w w w. j a v a 2 s .co m final String host = properties.getConfigString("host"); final Properties props = System.getProperties(); props.setProperty("mail.smtp.host", host); final String port = properties.getConfigString("port", null); if (port != null) { props.setProperty("mail.smtp.port", port); } if ("true".equalsIgnoreCase(properties.getConfigString("ssl", null))) { props.setProperty("mail.smtp.ssl.enable", "true"); } final Session session = Session.getInstance(props, null); // session.setDebug( true ); final javax.mail.Message msg = new MimeMessage(session); msg.setFrom(new InternetAddress(properties.getConfigString("from"))); msg.setRecipients(javax.mail.Message.RecipientType.TO, InternetAddress.parse(properties.getConfigString("to"), false)); msg.setSubject("superpipes error message"); msg.setDataHandler(new DataHandler(new ByteArrayDataSource(message, "text/html"))); msg.setHeader("X-Mailer", "superpipes"); Transport t = null; try { t = session.getTransport("smtp"); final String username = properties.getConfigString("username", null); final String password = properties.getConfigString("password", null); if (username == null || password == null) { t.connect(); } else { if (port == null || port.isEmpty()) { t.connect(host, username, password); } else { t.connect(host, Integer.parseInt(port), username, password); } } t.sendMessage(msg, msg.getAllRecipients()); } finally { if (t != null) { t.close(); } } }
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 w w w .ja v a 2 s . co m*/ 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:com.zimbra.cs.mailclient.smtp.SmtpTransportTest.java
@Test(timeout = 3000) public void nullMailFrom() 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"; SMTPMessage msg = new SMTPMessage(session, new SharedByteArrayInputStream(raw.getBytes(Charsets.ISO_8859_1))); msg.setEnvelopeFrom("<>"); // this should override the previously set mail.smtp.from transport.sendMessage(msg, msg.getAllRecipients()); transport.close(); server.shutdown(1000);/*from ww w .jav a2s.com*/ Assert.assertEquals("EHLO localhost\r\n", server.replay()); Assert.assertEquals("MAIL FROM:<>\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.liferay.mail.imap.IMAPAccessor.java
public void sendMessage(String personalName, String sender, Address[] to, Address[] cc, Address[] bcc, String subject, String body, List<MailFile> mailFiles) throws PortalException { Folder jxFolder = null;/*from w w w .j av a 2s .c o m*/ try { jxFolder = openFolder(_account.getSentFolderId()); Message jxMessage = createMessage(personalName, sender, to, cc, bcc, subject, body, mailFiles); Transport transport = _imapConnection.getTransport(); transport.sendMessage(jxMessage, jxMessage.getAllRecipients()); transport.close(); jxFolder.addMessageCountListener(new IMAPMessageCountListener(_user, _account, _password)); jxFolder.appendMessages(new Message[] { jxMessage }); } catch (MessagingException me) { throw new MailException(me); } catch (UnsupportedEncodingException uee) { throw new MailException(uee); } finally { closeFolder(jxFolder, false); } }
From source file:com.googlecode.gmail4j.javamail.ImapGmailClient.java
@Override public void send(final GmailMessage message) { if (message instanceof JavaMailGmailMessage) { Transport transport = null; try {/*ww w .j a v a 2s. c o m*/ final JavaMailGmailMessage msg = (JavaMailGmailMessage) message; transport = getGmailTransport(); transport.sendMessage(msg.getMessage(), msg.getMessage().getAllRecipients()); } catch (final Exception e) { throw new GmailException("Failed sending message: " + message, e); } finally { if (transport.isConnected()) { try { transport.close(); } catch (final Exception e) { LOG.warn("Cannot Close ImapGmailConnection : " + transport, e); } } } } else { throw new GmailException("ImapGmailClient requires JavaMailGmailMessage!"); } }
From source file:com.cubusmail.mail.MessageHandler.java
/** * @throws MessagingException//from w w w .ja va 2 s . com * @throws IOException */ public void send() throws MessagingException, IOException { buildBodyContent(); this.message.setHeader("X-Mailer", CubusConstants.APPLICATION_NAME); this.message.setSentDate(new Date()); Transport transport = this.session.getTransport(); if (!transport.isConnected()) { transport.connect(); } transport.sendMessage(this.message, this.message.getAllRecipients()); transport.close(); }
From source file:bean.RedSocial.java
/** * /* www. j a v a 2s .c o m*/ * @param _username * @param _email * @param _password */ @Transactional(propagation = Propagation.REQUIRES_NEW, readOnly = false, rollbackFor = transactionalBusinessException.ComentariosViaException.class) public void solicitarAcceso(String _username, String _email, String _password) { if (daoUsuario.obtenerUsuario(_username) != null) { throw new exceptionsBusiness.UsernameNoDisponible(); } String hash = BCrypt.hashpw(_password, BCrypt.gensalt()); Token token = new Token(_username, _email, hash); daoToken.guardarToken(token); //enviar token de acceso a la direccion email String correoEnvia = "skala2climbing@gmail.com"; String claveCorreo = "vNspLa5H"; // La configuracin para enviar correo Properties properties = new Properties(); properties.put("mail.smtp.host", "smtp.gmail.com"); properties.put("mail.smtp.starttls.enable", "true"); properties.put("mail.smtp.port", "587"); properties.put("mail.smtp.auth", "true"); properties.put("mail.user", correoEnvia); properties.put("mail.password", claveCorreo); // Obtener la sesion Session session = Session.getInstance(properties, null); try { // Crear el cuerpo del mensaje MimeMessage mimeMessage = new MimeMessage(session); // Agregar quien enva el correo mimeMessage.setFrom(new InternetAddress(correoEnvia, "Skala2Climbing")); // Los destinatarios InternetAddress[] internetAddresses = { new InternetAddress(token.getEmail()) }; // Agregar los destinatarios al mensaje mimeMessage.setRecipients(Message.RecipientType.TO, internetAddresses); // Agregar el asunto al correo mimeMessage.setSubject("Confirmacin de registro"); // Creo la parte del mensaje MimeBodyPart mimeBodyPart = new MimeBodyPart(); String ip = "90.165.24.228"; mimeBodyPart.setText("Confirme su registro pulsando en el siguiente enlace: http://" + ip + ":8383/redsocialcolaborativaclientangularjs/confirmacion.html?" + "token=" + token.getToken()); //mimeBodyPart.setText("Confirme su registro pulsando en el siguiente enlace: "+"Enlace an no disponible"); // Crear el multipart para agregar la parte del mensaje anterior Multipart multipart = new MimeMultipart(); multipart.addBodyPart(mimeBodyPart); // Agregar el multipart al cuerpo del mensaje mimeMessage.setContent(multipart); // Enviar el mensaje Transport transport = session.getTransport("smtp"); transport.connect(correoEnvia, claveCorreo); transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients()); transport.close(); } catch (UnsupportedEncodingException | MessagingException ex) { throw new ErrorEnvioEmail(); } }
From source file:nl.nn.adapterframework.senders.MailSender.java
protected void putOnTransport(Message msg) throws SenderException { // connect to the transport Transport transport = null; try {//w w w . j av a 2s. c o m CredentialFactory cf = new CredentialFactory(getSmtpAuthAlias(), getSmtpUserid(), getSmtpPassword()); transport = session.getTransport("smtp"); transport.connect(getSmtpHost(), cf.getUsername(), cf.getPassword()); if (log.isDebugEnabled()) { log.debug("MailSender [" + getName() + "] connected transport to URL [" + transport.getURLName() + "]"); } transport.sendMessage(msg, msg.getAllRecipients()); transport.close(); } catch (Exception e) { throw new SenderException("MailSender [" + getName() + "] cannot connect send message to smtpHost [" + getSmtpHost() + "]", e); } finally { if (transport != null) { try { transport.close(); } catch (MessagingException e1) { log.warn("MailSender [" + getName() + "] got exception closing connection", e1); } } } }