Search in sources :

Example 66 with Session

use of javax.mail.Session in project spring-boot by spring-projects.

the class MailSenderAutoConfigurationTests method jndiSessionAvailable.

@Test
public void jndiSessionAvailable() throws NamingException {
    Session session = configureJndiSession("foo");
    load(EmptyConfig.class, "spring.mail.jndi-name:foo");
    Session sessionBean = this.context.getBean(Session.class);
    assertThat(sessionBean).isEqualTo(session);
    assertThat(this.context.getBean(JavaMailSenderImpl.class).getSession()).isEqualTo(sessionBean);
}
Also used : JavaMailSenderImpl(org.springframework.mail.javamail.JavaMailSenderImpl) Session(javax.mail.Session) Test(org.junit.Test)

Example 67 with Session

use of javax.mail.Session in project spring-framework by spring-projects.

the class JavaMailSenderTests method javaMailSenderWithCustomSession.

@Test
public void javaMailSenderWithCustomSession() throws MessagingException {
    final Session session = Session.getInstance(new Properties());
    MockJavaMailSender sender = new MockJavaMailSender() {

        @Override
        protected Transport getTransport(Session sess) throws NoSuchProviderException {
            assertEquals(session, sess);
            return super.getTransport(sess);
        }
    };
    sender.setSession(session);
    sender.setHost("host");
    sender.setUsername("username");
    sender.setPassword("password");
    MimeMessage mimeMessage = sender.createMimeMessage();
    mimeMessage.setSubject("custom");
    mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress("you@mail.org"));
    mimeMessage.setSentDate(new GregorianCalendar(2005, 3, 1).getTime());
    sender.send(mimeMessage);
    assertEquals("host", sender.transport.getConnectedHost());
    assertEquals("username", sender.transport.getConnectedUsername());
    assertEquals("password", sender.transport.getConnectedPassword());
    assertTrue(sender.transport.isCloseCalled());
    assertEquals(1, sender.transport.getSentMessages().size());
    assertEquals(mimeMessage, sender.transport.getSentMessage(0));
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) MimeMessage(javax.mail.internet.MimeMessage) GregorianCalendar(java.util.GregorianCalendar) Properties(java.util.Properties) Session(javax.mail.Session) Test(org.junit.Test)

Example 68 with Session

use of javax.mail.Session in project spring-framework by spring-projects.

the class JavaMailSenderTests method javaMailProperties.

@Test
public void javaMailProperties() throws MessagingException {
    Properties props = new Properties();
    props.setProperty("bogusKey", "bogusValue");
    MockJavaMailSender sender = new MockJavaMailSender() {

        @Override
        protected Transport getTransport(Session sess) throws NoSuchProviderException {
            assertEquals("bogusValue", sess.getProperty("bogusKey"));
            return super.getTransport(sess);
        }
    };
    sender.setJavaMailProperties(props);
    sender.setHost("host");
    sender.setUsername("username");
    sender.setPassword("password");
    MimeMessage mimeMessage = sender.createMimeMessage();
    mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress("you@mail.org"));
    sender.send(mimeMessage);
    assertEquals("host", sender.transport.getConnectedHost());
    assertEquals("username", sender.transport.getConnectedUsername());
    assertEquals("password", sender.transport.getConnectedPassword());
    assertTrue(sender.transport.isCloseCalled());
    assertEquals(1, sender.transport.getSentMessages().size());
    assertEquals(mimeMessage, sender.transport.getSentMessage(0));
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) MimeMessage(javax.mail.internet.MimeMessage) Properties(java.util.Properties) Session(javax.mail.Session) Test(org.junit.Test)

Example 69 with Session

use of javax.mail.Session in project ats-framework by Axway.

the class MailReportSender method send.

/**
     * Email the report
     */
public void send() {
    log.info("Sending log mail report");
    // get the info needed for sending a mail
    ReportConfigurator reportConfigurator = ReportConfigurator.getInstance();
    String smtpServerName = reportConfigurator.getSmtpServerName();
    String smtpServerPort = reportConfigurator.getSmtpServerPort();
    String[] addressesTo = reportConfigurator.getAddressesTo();
    String[] addressesCc = reportConfigurator.getAddressesCc();
    String[] addressesBcc = reportConfigurator.getAddressesBcc();
    String addressFrom = reportConfigurator.getAddressFrom();
    // Attaching to default Session
    Properties mailProperties = new Properties();
    mailProperties.put("mail.smtp.host", smtpServerName);
    mailProperties.put("mail.smtp.port", smtpServerPort);
    Session session = Session.getDefaultInstance(mailProperties);
    Message msg = new MimeMessage(session);
    String errMsg = "Error creating mail object";
    try {
        // mail addresses
        msg.setFrom(new InternetAddress(addressFrom));
        msg.setRecipients(Message.RecipientType.TO, transformAdresses(addressesTo));
        msg.setRecipients(Message.RecipientType.CC, transformAdresses(addressesCc));
        msg.setRecipients(Message.RecipientType.BCC, transformAdresses(addressesBcc));
        // mail subject
        msg.setSubject(subject);
        // mail content
        msg.setContent(body, "text/html");
        // other header information
        msg.setSentDate(new Date());
    } catch (AddressException e) {
        throw new MailReportSendException(errMsg, e);
    } catch (MessagingException e) {
        throw new MailReportSendException(errMsg, e);
    }
    // send the message
    errMsg = "Error sending mail";
    try {
        Transport.send(msg);
    } catch (MessagingException e) {
        throw new MailReportSendException(errMsg, e);
    }
    log.info("Log mail report sent ok");
}
Also used : ReportConfigurator(com.axway.ats.log.report.model.ReportConfigurator) InternetAddress(javax.mail.internet.InternetAddress) Message(javax.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) MailReportSendException(com.axway.ats.log.report.exceptions.MailReportSendException) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) AddressException(javax.mail.internet.AddressException) Properties(java.util.Properties) Date(java.util.Date) Session(javax.mail.Session)

Example 70 with Session

use of javax.mail.Session in project GNS by MobilityFirst.

the class Email method emailLocal.

/**
   * Attempts to use the local emailer to send a message to the recipient.
   * If suppressWarning is true no warning message will be logged if this fails.
   *
   * @param subject
   * @param recipient
   * @param text
   * @param suppressWarning
   * @return true if the message was sent
   */
public static boolean emailLocal(String subject, String recipient, String text, boolean suppressWarning) {
    // Get system properties
    Properties properties = System.getProperties();
    properties.setProperty("mail.smtp.host", "localhost");
    //properties.setProperty("mail.user", "westy");
    //properties.setProperty("mail.password", "");
    // Get the default Session object.
    Session session = Session.getDefaultInstance(properties);
    try {
        // Create a default MimeMessage object.
        MimeMessage message = new MimeMessage(session);
        // Set From: header field of the header.
        message.setFrom(new InternetAddress(Config.getGlobalString(GNSConfig.GNSC.SUPPORT_EMAIL)));
        // Set To: header field of the header.
        message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient));
        // Set Subject: header field
        message.setSubject(subject);
        // Now set the actual message
        message.setText(text);
        // Send message
        Transport.send(message);
        getLogger().log(Level.FINE, "Successfully sent email to {0} with message: {1}", new Object[] { recipient, text });
        return true;
    } catch (Exception e) {
        if (!suppressWarning) {
            getLogger().log(Level.WARNING, "Unable to send email: {0}", e);
        }
        return false;
    }
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) MimeMessage(javax.mail.internet.MimeMessage) Properties(java.util.Properties) MessagingException(javax.mail.MessagingException) GeneralSecurityException(java.security.GeneralSecurityException) Session(javax.mail.Session)

Aggregations

Session (javax.mail.Session)110 MimeMessage (javax.mail.internet.MimeMessage)72 Properties (java.util.Properties)66 InternetAddress (javax.mail.internet.InternetAddress)49 MessagingException (javax.mail.MessagingException)47 Message (javax.mail.Message)31 Test (org.junit.Test)30 Transport (javax.mail.Transport)28 JMSession (com.zimbra.cs.util.JMSession)20 PasswordAuthentication (javax.mail.PasswordAuthentication)19 Date (java.util.Date)17 MimeBodyPart (javax.mail.internet.MimeBodyPart)16 MimeMultipart (javax.mail.internet.MimeMultipart)16 ZMimeMessage (com.zimbra.common.zmime.ZMimeMessage)15 IOException (java.io.IOException)15 SharedByteArrayInputStream (javax.mail.util.SharedByteArrayInputStream)15 Authenticator (javax.mail.Authenticator)12 Multipart (javax.mail.Multipart)11 NoSuchProviderException (javax.mail.NoSuchProviderException)10 BodyPart (javax.mail.BodyPart)9