Search in sources :

Example 16 with PasswordAuthentication

use of javax.mail.PasswordAuthentication in project oxTrust by GluuFederation.

the class MailUtils method sendMail.

public void sendMail(String from, String to, String subject, String message) throws MessagingException {
    log.debug("HostName: " + this.hostName + " Port: " + this.port + " ConnectionTimeOut: " + this.connectionTimeout);
    log.debug("UserName: " + this.userName + " Password: " + this.password);
    Properties props = new Properties();
    props.put("mail.smtp.host", this.hostName);
    props.put("mail.smtp.port", this.port);
    props.put("mail.from", from);
    props.put("mail.smtp.connectiontimeout", this.connectionTimeout);
    props.put("mail.smtp.timeout", this.connectionTimeout);
    props.put("mail.debug", true);
    props.put("mail.transport.protocol", "smtp");
    if (requiresSsl) {
        // props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.starttls.enable", true);
    //			props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    }
    Session session = null;
    if (requiresAuthentication) {
        props.put("mail.smtp.auth", "true");
        final String userName = this.userName;
        final String password = this.password;
        session = Session.getInstance(props, new javax.mail.Authenticator() {

            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(userName, password);
            }
        });
    } else {
        session = Session.getInstance(props, null);
    }
    MimeMessage msg = new MimeMessage(session);
    msg.setFrom();
    msg.setRecipients(Message.RecipientType.TO, to);
    msg.setSubject(subject);
    msg.setSentDate(new Date());
    msg.setText(message + "\n");
    Transport.send(msg);
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) Properties(java.util.Properties) Date(java.util.Date) Session(javax.mail.Session) PasswordAuthentication(javax.mail.PasswordAuthentication)

Example 17 with PasswordAuthentication

use of javax.mail.PasswordAuthentication in project springboot_op by SnailFastGo.

the class MailSender method send.

public void send() throws Exception {
    if (this.mail.getContentType() == null) {
        this.mail.setContentType(MailContentTypeEnum.HTML.getValue());
    }
    if (null == this.mail.getContent() || "".equals(this.mail.getContent())) {
        throw new Exception("邮件内容没有设置");
    }
    if (null == this.mail.getTitle() || "".equals(this.mail.getTitle())) {
        throw new Exception("邮件标题没有设置");
    }
    if (null == this.mail.getSendAddress() || this.mail.getSendAddress().isEmpty()) {
        throw new Exception("邮件发送地址没有设置");
    }
    final PropertiesUtil util = new PropertiesUtil("mail");
    //设置邮件服务器登陆信息
    final Properties props = new Properties();
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.host", util.getValue("mail.smtp.service"));
    props.put("mail.smtp.port", util.getValue("mail.smtp.port"));
    props.put("mail.user", util.getValue("mail.from.address"));
    props.put("mail.password", util.getValue("mail.from.smtp.pwd"));
    // 构建授权信息,用于进行SMTP进行身份验证
    Authenticator authenticator = new Authenticator() {

        protected PasswordAuthentication getPasswordAuthentication() {
            // 用户名、密码
            String userName = props.getProperty("mail.user");
            String password = props.getProperty("mail.password");
            return new PasswordAuthentication(userName, password);
        }
    };
    // 使用环境属性和授权信息,创建邮件会话
    Session mailSession = Session.getInstance(props, authenticator);
    // 创建邮件消息
    MimeMessage message = new MimeMessage(mailSession);
    // 设置发件人昵称
    String nickName = MimeUtility.encodeText(util.getValue("mail.from.nickname"));
    InternetAddress form = new InternetAddress(nickName + " <" + props.getProperty("mail.user") + ">");
    message.setFrom(form);
    // 设置邮件标题
    message.setSubject(mail.getTitle());
    //html发送邮件
    if (mail.getContentType().equals(MailContentTypeEnum.HTML.getValue())) {
        // 设置邮件内容
        message.setContent(mail.getContent(), mail.getContentType());
    } else //文本发送邮件
    if (mail.getContentType().equals(MailContentTypeEnum.TEXT.getValue())) {
        // 设置邮件内容
        message.setText(mail.getContent());
    }
    //发送邮箱地址
    List<String> targets = mail.getSendAddress();
    for (int i = 0; i < targets.size(); i++) {
        try {
            // 设置收件人的邮箱
            InternetAddress to = new InternetAddress(targets.get(i));
            message.setRecipient(Message.RecipientType.TO, to);
            // 最后当然就是发送邮件啦
            Transport.send(message);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) MimeMessage(javax.mail.internet.MimeMessage) PropertiesUtil(com.myspringboot.utils.PropertiesUtil) Properties(java.util.Properties) Authenticator(javax.mail.Authenticator) PasswordAuthentication(javax.mail.PasswordAuthentication) Session(javax.mail.Session)

Example 18 with PasswordAuthentication

use of javax.mail.PasswordAuthentication in project maven-plugins by apache.

the class ProjectJavamailMailSender method send.

// ----------------------------------------------------------------------
// MailSender Implementation
// ----------------------------------------------------------------------
public void send(MailMessage mail) throws MailSenderException {
    verify(mail);
    try {
        Authenticator auth = null;
        if (getUsername() != null) {
            auth = new Authenticator() {

                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(getUsername(), getPassword());
                }
            };
        }
        Session session = Session.getDefaultInstance(props, auth);
        session.setDebug(getLogger().isDebugEnabled());
        Message msg = new MimeMessage(session);
        InternetAddress addressFrom = new InternetAddress(mail.getFrom().getRfc2822Address());
        msg.setFrom(addressFrom);
        if (mail.getToAddresses().size() > 0) {
            InternetAddress[] addressTo = new InternetAddress[mail.getToAddresses().size()];
            int count = 0;
            for (Object o : mail.getToAddresses()) {
                String address = ((MailMessage.Address) o).getRfc2822Address();
                addressTo[count++] = new InternetAddress(address);
            }
            msg.setRecipients(Message.RecipientType.TO, addressTo);
        }
        if (mail.getCcAddresses().size() > 0) {
            InternetAddress[] addressCc = new InternetAddress[mail.getCcAddresses().size()];
            int count = 0;
            for (Object o : mail.getCcAddresses()) {
                String address = ((MailMessage.Address) o).getRfc2822Address();
                addressCc[count++] = new InternetAddress(address);
            }
            msg.setRecipients(Message.RecipientType.CC, addressCc);
        }
        if (mail.getBccAddresses().size() > 0) {
            InternetAddress[] addressBcc = new InternetAddress[mail.getBccAddresses().size()];
            int count = 0;
            for (Object o : mail.getBccAddresses()) {
                String address = ((MailMessage.Address) o).getRfc2822Address();
                addressBcc[count++] = new InternetAddress(address);
            }
            msg.setRecipients(Message.RecipientType.BCC, addressBcc);
        }
        // Setting the Subject and Content Type
        msg.setSubject(mail.getSubject());
        msg.setContent(mail.getContent(), mail.getContentType() == null ? "text/plain" : mail.getContentType());
        if (mail.getSendDate() != null) {
            msg.setHeader("Date", DateFormatUtils.getDateHeader(mail.getSendDate()));
        } else {
            msg.setHeader("Date", DateFormatUtils.getDateHeader(new Date()));
        }
        // Send the message
        Transport.send(msg);
    } catch (MessagingException e) {
        throw new MailSenderException("Error while sending mail.", e);
    }
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) MailSenderException(org.codehaus.plexus.mailsender.MailSenderException) Message(javax.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) MailMessage(org.codehaus.plexus.mailsender.MailMessage) InternetAddress(javax.mail.internet.InternetAddress) MessagingException(javax.mail.MessagingException) Date(java.util.Date) MimeMessage(javax.mail.internet.MimeMessage) Authenticator(javax.mail.Authenticator) PasswordAuthentication(javax.mail.PasswordAuthentication) Session(javax.mail.Session)

Example 19 with PasswordAuthentication

use of javax.mail.PasswordAuthentication in project tomee by apache.

the class MailSessionFactory method create.

public Session create() {
    final String password = properties.getProperty("password");
    Authenticator auth = null;
    if (password != null) {
        final String protocol = properties.getProperty("mail.transport.protocol", "smtp");
        String user = properties.getProperty("mail." + protocol + ".user");
        if (user == null) {
            user = properties.getProperty("mail.user");
        }
        if (user != null) {
            final PasswordAuthentication pa = new PasswordAuthentication(user, password);
            auth = new Authenticator() {

                protected PasswordAuthentication getPasswordAuthentication() {
                    return pa;
                }
            };
        }
    }
    if (useDefault) {
        if (auth != null) {
            return Session.getDefaultInstance(properties, auth);
        }
        return Session.getDefaultInstance(properties);
    }
    if (auth != null) {
        return Session.getInstance(properties, auth);
    }
    return Session.getInstance(properties);
}
Also used : Authenticator(javax.mail.Authenticator) PasswordAuthentication(javax.mail.PasswordAuthentication)

Example 20 with PasswordAuthentication

use of javax.mail.PasswordAuthentication in project local-data-aragopedia by aragonopendata.

the class SendMailSSL method enviar.

public void enviar(final String usuario, final String password, String destinos, String asunto, String texto) {
    Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {

        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(usuario, password);
        }
    });
    try {
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(usuario));
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(destinos));
        message.setSubject(asunto);
        message.setText(texto);
        Transport.send(message);
    } catch (MessagingException e) {
        log.error("Error sending mail:");
        log.error("\t" + e.getMessage());
    }
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) Message(javax.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) Session(javax.mail.Session) PasswordAuthentication(javax.mail.PasswordAuthentication)

Aggregations

PasswordAuthentication (javax.mail.PasswordAuthentication)21 Session (javax.mail.Session)19 Properties (java.util.Properties)16 MimeMessage (javax.mail.internet.MimeMessage)16 InternetAddress (javax.mail.internet.InternetAddress)15 Message (javax.mail.Message)13 MessagingException (javax.mail.MessagingException)13 Authenticator (javax.mail.Authenticator)9 Date (java.util.Date)5 IOException (java.io.IOException)3 Multipart (javax.mail.Multipart)3 MimeBodyPart (javax.mail.internet.MimeBodyPart)3 MimeMultipart (javax.mail.internet.MimeMultipart)3 GeneralSecurityException (java.security.GeneralSecurityException)2 DataHandler (javax.activation.DataHandler)2 DataSource (javax.activation.DataSource)2 FileDataSource (javax.activation.FileDataSource)2 BodyPart (javax.mail.BodyPart)2 PropertiesUtil (com.myspringboot.utils.PropertiesUtil)1 POP3SSLStore (com.sun.mail.pop3.POP3SSLStore)1