use of javax.mail.Authenticator in project IPK-BrAPI-Validator by plantbreeding.
the class EmailSender method sendEmail.
static void sendEmail(final String message, final String subject, final String emailAddress) {
javax.mail.Session session;
InternetAddress addressFrom = null;
final Properties properties = new Properties();
properties.put("mail.smtp.host", Config.get("mailSmtpHost"));
if (Config.get("mailSmtpPort") != null) {
properties.put("mail.smtp.port", Config.get("mailSmtpPort"));
}
if (Config.get("mailSmtpLogin") == null || Config.get("mailSmtpLogin").isEmpty()) {
session = javax.mail.Session.getDefaultInstance(properties);
try {
addressFrom = new InternetAddress(Config.get("fromEmailAddress"), Config.get("fromPersonalName"));
} catch (final UnsupportedEncodingException e) {
e.printStackTrace();
LOGGER.warn(emailAddress + " : " + e.getMessage());
}
} else {
properties.put("mail.smtp.auth", "true");
final Authenticator authenticator = new Authenticator() {
private PasswordAuthentication authentication;
{
this.authentication = new PasswordAuthentication(Config.get("mailSmtpLogin"), Config.get("mailSmtpPassword"));
}
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return this.authentication;
}
};
session = javax.mail.Session.getInstance(properties, authenticator);
try {
addressFrom = new InternetAddress(Config.get("fromEmailAddress"), Config.get("fromPersonalName"));
} catch (final UnsupportedEncodingException e) {
e.printStackTrace();
LOGGER.warn(emailAddress + " : " + e.getMessage());
}
}
try {
final Message mail = new MimeMessage(session);
final InternetAddress addressTo = new InternetAddress(emailAddress);
mail.setRecipient(Message.RecipientType.TO, addressTo);
mail.setSubject(subject);
mail.setFrom(addressFrom);
Multipart multipart = new MimeMultipart();
MimeBodyPart content = new MimeBodyPart();
content.setText(message, "UTF-8", "html");
multipart.addBodyPart(content);
mail.setContent(multipart);
Transport.send(mail);
// System.out.println(message);
} catch (final MessagingException e) {
e.printStackTrace();
LOGGER.warn(emailAddress + " : " + e.getMessage());
}
}
use of javax.mail.Authenticator in project Lucee by lucee.
the class SMTPVerifier method _verify.
private static boolean _verify(String host, String username, String password, int port) throws MessagingException {
boolean hasAuth = !StringUtil.isEmpty(username);
Properties props = new Properties();
props.put("mail.smtp.host", host);
if (hasAuth)
props.put("mail.smtp.auth", "true");
if (hasAuth)
props.put("mail.smtp.user", username);
if (hasAuth)
props.put("mail.transport.connect-timeout", "30");
if (port > 0)
props.put("mail.smtp.port", String.valueOf(port));
Authenticator auth = null;
if (hasAuth)
auth = new DefaultAuthenticator(username, password);
Session session = Session.getInstance(props, auth);
Transport transport = session.getTransport("smtp");
if (hasAuth)
transport.connect(host, username, password);
else
transport.connect();
boolean rtn = transport.isConnected();
transport.close();
return rtn;
}
use of javax.mail.Authenticator in project MassBank-web by MassBank.
the class SendMail method send.
/**
* メール送信関数
* @param info メール送信情報オブジェクト
* @return 結果
*/
public static boolean send(SendMailInfo info) {
// メール送信情報チェック
if (!info.isCheck()) {
Logger.global.severe("The mail sending failed.");
return false;
}
String[] smtpItems = info.getSmtp().split(",");
if (smtpItems.length >= 1) {
host = smtpItems[0].trim();
}
if (smtpItems.length >= 2) {
port = smtpItems[1].trim();
}
if (smtpItems.length >= 3) {
user = smtpItems[2].trim();
}
if (smtpItems.length >= 4) {
pass = smtpItems[3].trim();
}
if (port.equals("")) {
port = "25";
}
System.out.println(host + "/" + port + "/" + user + "/" + pass);
try {
// SMTPサーバーのアドレスを設定
Properties props = new Properties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", port);
if (port.equals("465") || port.equals("587")) {
props.put("mail.smtp.ssl.trust", host);
if (port.equals("465")) {
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
} else if (port.equals("587")) {
props.put("mail.smtp.starttls.enable", "true");
}
}
Session session = null;
if (user.equals("") || pass.equals("")) {
props.put("mail.smtp.auth", "false");
session = Session.getDefaultInstance(props, null);
} else {
props.put("mail.smtp.auth", "true");
session = Session.getInstance(props, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, pass);
}
});
}
MimeMessage mimeMsg = new MimeMessage(session);
// 送信元メールアドレスと送信者名を設定
mimeMsg.setFrom(new InternetAddress(info.getFrom(), info.getFromName(), "utf-8"));
// 送信先メールアドレスを設定
mimeMsg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(info.getTo()));
if (!info.getCc().equals("")) {
mimeMsg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(info.getCc()));
}
if (!info.getBcc().equals("")) {
mimeMsg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(info.getBcc()));
}
// メールタイトルを設定
mimeMsg.setSubject(info.getSubject(), "utf-8");
// メールボディ用マルチパートオブジェクト生成
MimeMultipart mp = new MimeMultipart();
MimeBodyPart mbp = new MimeBodyPart();
// 本文
mbp.setText(info.getContents(), "utf-8");
mp.addBodyPart(mbp);
File[] files = info.getFiles();
if (files != null) {
for (int i = 0; i < files.length; i++) {
// 添付ファイル
mbp = new MimeBodyPart();
FileDataSource fds = new FileDataSource(files[i]);
mbp.setDataHandler(new DataHandler(fds));
mbp.setFileName(MimeUtility.encodeWord(fds.getName()));
mp.addBodyPart(mbp);
}
}
// メール内容にマルチパートオブジェクトと送信日付を設定して送信
mimeMsg.setContent(mp);
mimeMsg.setSentDate(new Date());
Transport.send(mimeMsg);
} catch (Exception e) {
Logger.global.severe("The mail sending failed.");
e.printStackTrace();
return false;
}
return true;
}
use of javax.mail.Authenticator in project carbon-apimgt by wso2.
the class NewApiVersionMailNotifier method sendNotifications.
@Override
public void sendNotifications(NotificationDTO notificationDTO) throws APIManagementException {
Properties props = notificationDTO.getProperties();
// get Notifier email List
Set<String> emailList = getEmailNotifierList(notificationDTO);
if (emailList.isEmpty()) {
log.debug("Email Notifier Set is Empty");
return;
}
for (String mail : emailList) {
try {
Authenticator auth = new SMTPAuthenticator();
Session mailSession = Session.getDefaultInstance(props, auth);
MimeMessage message = new MimeMessage(mailSession);
notificationDTO.setTitle((String) notificationDTO.getProperty(NotifierConstants.TITLE_KEY));
notificationDTO.setMessage((String) notificationDTO.getProperty(NotifierConstants.TEMPLATE_KEY));
notificationDTO = loadMailTemplate(notificationDTO);
message.setSubject(notificationDTO.getTitle());
message.setContent(notificationDTO.getMessage(), NotifierConstants.TEXT_TYPE);
message.setFrom(new InternetAddress(mailConfigurations.getFromUser()));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(mail));
Transport.send(message);
} catch (MessagingException e) {
log.error("Exception Occurred during Email notification Sending", e);
}
}
}
Aggregations