use of javax.mail.PasswordAuthentication in project Gargoyle by callakrsos.
the class EmailAttachmentSender method sendEmailWithAttachments.
public static void sendEmailWithAttachments(String host, String port, final String userName, final String password, String toAddress, String subject, String message, String[] attachFiles) throws AddressException, MessagingException {
// sets SMTP server properties
Properties properties = new Properties();
properties.put("mail.smtp.host", host);
properties.put("mail.smtp.port", port);
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.user", userName);
properties.put("mail.password", password);
// creates a new session with an authenticator
Authenticator auth = new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, password);
}
};
Session session = Session.getInstance(properties, auth);
// creates a new e-mail message
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(userName));
InternetAddress[] toAddresses = { new InternetAddress(toAddress) };
msg.setRecipients(Message.RecipientType.TO, toAddresses);
msg.setSubject(subject);
msg.setSentDate(new Date());
// creates message part
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(message, "text/html");
// creates multi-part
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
// adds attachments
if (attachFiles != null && attachFiles.length > 0) {
for (String filePath : attachFiles) {
MimeBodyPart attachPart = new MimeBodyPart();
try {
attachPart.attachFile(filePath);
} catch (IOException ex) {
ex.printStackTrace();
}
multipart.addBodyPart(attachPart);
}
}
// sets the multi-part as e-mail's content
msg.setContent(multipart);
// sends the e-mail
Transport.send(msg);
}
use of javax.mail.PasswordAuthentication in project GNS by MobilityFirst.
the class Email method emailTLS.
/**
* Attempts to use TLS 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
*/
// TLS doesn't work with Dreamhost
public static boolean emailTLS(String subject, String recipient, String text, boolean suppressWarning) {
final String username = Config.getGlobalString(GNSConfig.GNSC.ADMIN_EMAIL);
final String contactString = Config.getGlobalString(GNSConfig.GNSC.ADMIN_PASSWORD);
try {
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props, new javax.mail.Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, contactString);
}
});
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(Config.getGlobalString(GNSConfig.GNSC.SUPPORT_EMAIL)));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipient));
message.setSubject(subject);
message.setText(text);
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;
}
}
use of javax.mail.PasswordAuthentication in project GNS by MobilityFirst.
the class Email method emailSSL.
/**
* Attempts to use SSL 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 emailSSL(String subject, String recipient, String text, boolean suppressWarning) {
try {
Properties props = new Properties();
props.put("mail.smtp.host", SMTP_HOST);
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
Session session = Session.getInstance(props, new javax.mail.Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(Config.getGlobalString(GNSConfig.GNSC.ADMIN_EMAIL), Config.getGlobalString(GNSConfig.GNSC.ADMIN_PASSWORD));
}
});
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(Config.getGlobalString(GNSConfig.GNSC.SUPPORT_EMAIL)));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipient));
message.setSubject(subject);
message.setText(text);
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;
}
}
use of javax.mail.PasswordAuthentication in project wildfly by wildfly.
the class MailSubsystem21TestCase method testRuntime.
@Test
public void testRuntime() throws Exception {
KernelServicesBuilder builder = createKernelServicesBuilder(new MailSubsystem10TestCase.Initializer()).setSubsystemXml(getSubsystemXml());
KernelServices mainServices = builder.build();
if (!mainServices.isSuccessfulBoot()) {
Assert.fail(mainServices.getBootError().toString());
}
ServiceController<?> javaMailService = mainServices.getContainer().getService(MailSessionAdd.MAIL_SESSION_SERVICE_NAME.append("defaultMail"));
javaMailService.setMode(ServiceController.Mode.ACTIVE);
Session session = (Session) javaMailService.getValue();
Assert.assertNotNull("session should not be null", session);
Properties properties = session.getProperties();
Assert.assertNotNull("smtp host should be set", properties.getProperty("mail.smtp.host"));
Assert.assertNotNull("pop3 host should be set", properties.getProperty("mail.pop3.host"));
Assert.assertNotNull("imap host should be set", properties.getProperty("mail.imap.host"));
PasswordAuthentication auth = session.requestPasswordAuthentication(InetAddress.getLocalHost(), 25, "smtp", "", "");
Assert.assertEquals("nobody", auth.getUserName());
Assert.assertEquals("pass", auth.getPassword());
ServiceController<?> defaultMailService = mainServices.getContainer().getService(MailSessionAdd.MAIL_SESSION_SERVICE_NAME.append("default2"));
session = (Session) defaultMailService.getValue();
Assert.assertEquals("Debug should be true", true, session.getDebug());
ServiceController<?> customMailService = mainServices.getContainer().getService(MailSessionAdd.MAIL_SESSION_SERVICE_NAME.append("custom"));
session = (Session) customMailService.getValue();
properties = session.getProperties();
String host = properties.getProperty("mail.smtp.host");
Assert.assertNotNull("smtp host should be set", host);
Assert.assertEquals("mail.example.com", host);
//this one should be read out of socket binding
Assert.assertEquals("localhost", properties.get("mail.pop3.host"));
//this one should be extra property
Assert.assertEquals("some-custom-prop-value", properties.get("mail.pop3.custom_prop"));
//this one should be extra property
Assert.assertEquals("fully-qualified-prop-name", properties.get("some.fully.qualified.property"));
MailSessionService service = (MailSessionService) customMailService.getService();
Credentials credentials = service.getConfig().getCustomServers()[0].getCredentials();
Assert.assertEquals(credentials.getUsername(), "username");
Assert.assertEquals(credentials.getPassword(), "password");
}
use of javax.mail.PasswordAuthentication in project oxCore by GluuFederation.
the class MailService method sendMail.
public boolean sendMail(SmtpConfiguration mailSmtpConfiguration, String from, String to, String subject, String message) {
if (mailSmtpConfiguration == null) {
log.error("Failed to send message from '{0}' to '{1}' because the SMTP configuration isn't valid!", from, to);
return false;
}
log.debug("Host name: " + mailSmtpConfiguration.getHost() + ", port: " + mailSmtpConfiguration.getPort() + ", connection time out: " + this.connectionTimeout);
String mailFrom = from;
if (StringHelper.isEmpty(mailFrom)) {
mailFrom = mailSmtpConfiguration.getFromEmailAddress();
}
Properties props = new Properties();
props.put("mail.smtp.host", mailSmtpConfiguration.getHost());
props.put("mail.smtp.port", mailSmtpConfiguration.getPort());
props.put("mail.from", mailFrom);
props.put("mail.smtp.connectiontimeout", this.connectionTimeout);
props.put("mail.smtp.timeout", this.connectionTimeout);
if (mailSmtpConfiguration.isRequiresSsl()) {
props.put("mail.smtp.socketFactory.port", mailSmtpConfiguration.getPort());
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
}
Session session = null;
if (mailSmtpConfiguration.isRequiresAuthentication()) {
props.put("mail.smtp.auth", "true");
final String userName = mailSmtpConfiguration.getUserName();
final String password = mailSmtpConfiguration.getPasswordDecrypted();
session = Session.getInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, password);
}
});
} else {
Session.getInstance(props, null);
}
MimeMessage msg = new MimeMessage(session);
try {
msg.setFrom(new InternetAddress(mailFrom));
msg.setRecipients(Message.RecipientType.TO, to);
msg.setSubject(subject, "UTF-8");
msg.setSentDate(new Date());
msg.setText(message + "\n", "UTF-8");
Transport.send(msg);
} catch (MessagingException ex) {
log.error("Failed to send message", ex);
return false;
}
return true;
}
Aggregations