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);
}
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));
}
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));
}
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");
}
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;
}
}
Aggregations