use of com.sun.mail.smtp.SMTPTransport in project GNS by MobilityFirst.
the class Email method simpleMail.
/**
*
* @param subject
* @param recipient
* @param text
* @param suppressWarning
* @return true if the mail was sent
*/
public static boolean simpleMail(String subject, String recipient, String text, boolean suppressWarning) {
try {
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
Properties props = new Properties();
props.setProperty("mail.smtp.ssl.enable", "true");
//props.put("mail.smtp.host", smtpHost);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.ssl.socketFactory", sf);
Session session = Session.getInstance(props);
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);
SMTPTransport t = (SMTPTransport) session.getTransport("smtp");
try {
t.connect(SMTP_HOST, Config.getGlobalString(GNSConfig.GNSC.ADMIN_EMAIL), Config.getGlobalString(GNSConfig.GNSC.ADMIN_PASSWORD));
t.sendMessage(message, message.getAllRecipients());
getLogger().log(Level.FINE, "Email response: {0}", t.getLastServerResponse());
} finally {
t.close();
}
getLogger().log(Level.FINE, "Successfully sent email to {0} with message: {1}", new Object[] { recipient, text });
return true;
} catch (GeneralSecurityException | MessagingException e) {
if (!suppressWarning) {
getLogger().log(Level.WARNING, "Unable to send email: {0}", e);
}
return false;
}
}
use of com.sun.mail.smtp.SMTPTransport in project BIMserver by opensourceBIM.
the class EmailMessage method send.
public void send() throws MessagingException, UserException {
Properties props = new Properties();
ServerSettings serverSettings = bimServer.getServerSettingsCache().getServerSettings();
props.put("mail.smtp.localhost", "bimserver.org");
String smtpProps = serverSettings.getSmtpProtocol() == SmtpProtocol.SMTPS ? "mail.smtps.port" : "mail.smtp.port";
props.put("mail.smtp.connectiontimeout", 10000);
props.put("mail.smtp.timeout", 10000);
props.put("mail.smtp.writetimeout", 10000);
props.put(smtpProps, serverSettings.getSmtpPort());
if (serverSettings.getSmtpProtocol() == SmtpProtocol.STARTTLS) {
props.put("mail.smtp.starttls.enable", "true");
}
Session mailSession = Session.getInstance(props);
Transport transport = null;
try {
if (serverSettings.getSmtpProtocol() == SmtpProtocol.SMTP) {
transport = new SMTPTransport(mailSession, new URLName(serverSettings.getSmtpServer()));
} else if (serverSettings.getSmtpProtocol() == SmtpProtocol.SMTPS) {
transport = new SMTPSSLTransport(mailSession, new URLName(serverSettings.getSmtpServer()));
} else if (serverSettings.getSmtpProtocol() == SmtpProtocol.STARTTLS) {
transport = new SMTPSSLTransport(mailSession, new URLName(serverSettings.getSmtpServer()));
} else {
throw new RuntimeException("Unimplemented SMTP protocol: " + serverSettings.getSmtpProtocol());
}
transport.connect(serverSettings.getSmtpServer(), serverSettings.getSmtpPort(), serverSettings.getSmtpUsername(), serverSettings.getSmtpPassword());
Message message = new MimeMessage(mailSession);
message.setSubject(subject);
message.setRecipients(to, addressTo);
message.setContent(body, contentType);
message.setFrom(from);
transport.sendMessage(message, addressTo);
} catch (MessagingException e) {
LOGGER.error("Error sending email " + body + " " + e.getMessage());
throw new UserException("Error sending email " + e.getMessage());
}
}
use of com.sun.mail.smtp.SMTPTransport in project mxisd by kamax-io.
the class EmailSmtpConnector method send.
@Override
public void send(String senderAddress, String senderName, String recipient, String content) {
if (StringUtils.isBlank(senderAddress)) {
throw new FeatureNotAvailable("3PID Email identity: sender address is empty - " + "You must set a value for notifications to work");
}
if (StringUtils.isBlank(content)) {
throw new InternalServerError("Notification content is empty");
}
try {
InternetAddress sender = new InternetAddress(senderAddress, senderName);
MimeMessage msg = new MimeMessage(session, IOUtils.toInputStream(content, StandardCharsets.UTF_8));
// FIXME set version
msg.setHeader("X-Mailer", "mxisd");
msg.setSentDate(new Date());
msg.setFrom(sender);
msg.setRecipients(Message.RecipientType.TO, recipient);
msg.saveChanges();
log.info("Sending invite to {} via SMTP using {}:{}", recipient, cfg.getHost(), cfg.getPort());
SMTPTransport transport = (SMTPTransport) session.getTransport("smtp");
transport.setStartTLS(cfg.getTls() > 0);
transport.setRequireStartTLS(cfg.getTls() > 1);
log.info("Connecting to {}:{}", cfg.getHost(), cfg.getPort());
transport.connect(cfg.getHost(), cfg.getPort(), cfg.getLogin(), cfg.getPassword());
try {
transport.sendMessage(msg, InternetAddress.parse(recipient));
log.info("Invite to {} was sent", recipient);
} finally {
transport.close();
}
} catch (UnsupportedEncodingException | MessagingException e) {
throw new RuntimeException("Unable to send e-mail invite to " + recipient, e);
}
}
use of com.sun.mail.smtp.SMTPTransport in project service-proxy by membrane.
the class EmailTokenProvider method sendEmail.
private void sendEmail(String sender, String recipient, String subject, String text) {
try {
Properties props = System.getProperties();
props.put("mail.smtp.port", "" + smtpPort);
props.put("mail.smtp.socketFactory.port", "" + smtpPort);
if (ssl) {
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.starttls.required", "true");
}
if (smtpUser != null) {
props.put("mail.smtp.auth", "true");
}
Session session = Session.getInstance(props, null);
final MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(sender));
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipient, false));
msg.setSubject(subject);
msg.setText(text, Constants.UTF_8);
msg.setSentDate(new Date());
SMTPTransport t = (SMTPTransport) session.getTransport(ssl ? "smtps" : "smtp");
t.connect(smtpHost, smtpUser, smtpPassword);
t.sendMessage(msg, msg.getAllRecipients());
t.close();
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
use of com.sun.mail.smtp.SMTPTransport in project cloudstack by apache.
the class SMTPMailSender method sendMail.
public void sendMail(SMTPMailProperties mailProps) {
if (session == null) {
logger.error("Unable to send mail due to null session.");
return;
}
try {
SMTPMessage message = createMessage(mailProps);
if (ArrayUtils.isEmpty(message.getAllRecipients())) {
logger.error(String.format("Unable to send mail [%s] due to the recipient list of the message being empty.", mailProps.getSubject()));
return;
}
SMTPTransport smtpTrans = createSmtpTransport();
smtpTrans.connect();
smtpTrans.sendMessage(message, message.getAllRecipients());
smtpTrans.close();
} catch (MessagingException | UnsupportedEncodingException ex) {
logger.error(String.format("Unable to send mail [%s] to the recipcients [%s].", mailProps.getSubject(), mailProps.getRecipients().toString()), ex);
}
}
Aggregations