Search in sources :

Example 21 with Authenticator

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());
    }
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) MimeMultipart(javax.mail.internet.MimeMultipart) Multipart(javax.mail.Multipart) Message(javax.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Properties(java.util.Properties) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) MimeBodyPart(javax.mail.internet.MimeBodyPart) Authenticator(javax.mail.Authenticator) PasswordAuthentication(javax.mail.PasswordAuthentication)

Example 22 with Authenticator

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;
}
Also used : DefaultAuthenticator(org.apache.commons.mail.DefaultAuthenticator) Properties(java.util.Properties) Transport(javax.mail.Transport) DefaultAuthenticator(org.apache.commons.mail.DefaultAuthenticator) Authenticator(javax.mail.Authenticator) Session(javax.mail.Session)

Example 23 with Authenticator

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;
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) DataHandler(javax.activation.DataHandler) Properties(java.util.Properties) Date(java.util.Date) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) FileDataSource(javax.activation.FileDataSource) MimeBodyPart(javax.mail.internet.MimeBodyPart) File(java.io.File) Authenticator(javax.mail.Authenticator) Session(javax.mail.Session) PasswordAuthentication(javax.mail.PasswordAuthentication)

Example 24 with Authenticator

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);
        }
    }
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) Properties(java.util.Properties) Authenticator(javax.mail.Authenticator) Session(javax.mail.Session)

Aggregations

Authenticator (javax.mail.Authenticator)24 Properties (java.util.Properties)18 Session (javax.mail.Session)16 MessagingException (javax.mail.MessagingException)14 PasswordAuthentication (javax.mail.PasswordAuthentication)13 InternetAddress (javax.mail.internet.InternetAddress)13 MimeMessage (javax.mail.internet.MimeMessage)13 MimeMultipart (javax.mail.internet.MimeMultipart)8 IOException (java.io.IOException)7 Message (javax.mail.Message)7 MimeBodyPart (javax.mail.internet.MimeBodyPart)7 Date (java.util.Date)4 DataHandler (javax.activation.DataHandler)4 Multipart (javax.mail.Multipart)4 File (java.io.File)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 FileDataSource (javax.activation.FileDataSource)2 Folder (javax.mail.Folder)2 Store (javax.mail.Store)2 URLName (javax.mail.URLName)2