Search in sources :

Example 66 with AddressException

use of javax.mail.internet.AddressException in project ORCID-Source by ORCID.

the class BaseController method validateEmailAddress.

/**
     * Validates if the provided string matches an email address pattern.
     * 
     * @param email
     *            The string to evaluate
     * @return true if the provided string matches an email address pattern,
     *         false otherwise.
     */
protected boolean validateEmailAddress(String email) {
    if (StringUtils.isNotBlank(email)) {
        try {
            InternetAddress addr = new InternetAddress(email);
            addr.validate();
            return true;
        } catch (AddressException ex) {
        }
    }
    return false;
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) AddressException(javax.mail.internet.AddressException)

Example 67 with AddressException

use of javax.mail.internet.AddressException in project ORCID-Source by ORCID.

the class CustomEmailController method validateSender.

@RequestMapping(value = "/validate-sender.json", method = RequestMethod.POST)
@ResponseBody
public CustomEmailForm validateSender(@RequestBody CustomEmailForm customEmailForm) {
    customEmailForm.getSender().setErrors(new ArrayList<String>());
    if (!PojoUtil.isEmpty(customEmailForm.getSender())) {
        try {
            String sender = customEmailForm.getSender().getValue();
            InternetAddress addr = new InternetAddress(sender);
            addr.validate();
        } catch (AddressException ex) {
            customEmailForm.getSender().getErrors().add(getMessage("custom_email.sender.invalid"));
        }
    }
    return customEmailForm;
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) AddressException(javax.mail.internet.AddressException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 68 with AddressException

use of javax.mail.internet.AddressException in project opennms by OpenNMS.

the class JavaMailer method buildMessage.

/**
 * Build a complete message ready for sending.
 *
 * @return completed message, ready to be passed to Transport.sendMessage
 * @throws org.opennms.javamail.JavaMailerException if any of the underlying operations fail
 */
public Message buildMessage() throws JavaMailerException {
    try {
        checkEnvelopeAndContents();
        MimeMessage message = initializeMessage();
        // The next line has been commented, because it prevents the usage of internationalized characters and makes the email unreadable.
        // String encodedText = MimeUtility.encodeText(getMessageText(), m_charSet, m_encoding);
        String encodedText = getMessageText();
        if ((getFileName() == null) && (getInputStream() == null)) {
            message.setContent(encodedText, m_contentType + "; charset=" + m_charSet);
        } else if (getFileName() == null) {
            BodyPart streamBodyPart = new MimeBodyPart();
            streamBodyPart.setDataHandler(new DataHandler(new InputStreamDataSource(m_inputStreamName, m_inputStreamContentType, m_inputStream)));
            streamBodyPart.setFileName(m_inputStreamName);
            streamBodyPart.setHeader("Content-Transfer-Encoding", "base64");
            streamBodyPart.setDisposition(Part.ATTACHMENT);
            MimeMultipart mp = new MimeMultipart();
            mp.addBodyPart(streamBodyPart);
            message.setContent(mp);
        } else {
            BodyPart bp = new MimeBodyPart();
            bp.setContent(encodedText, m_contentType + "; charset=" + m_charSet);
            MimeMultipart mp = new MimeMultipart();
            mp.addBodyPart(bp);
            mp.addBodyPart(createFileAttachment(new File(getFileName())));
            message.setContent(mp);
        }
        message.setHeader("X-Mailer", getMailer());
        message.setSentDate(new Date());
        message.saveChanges();
        return message;
    } catch (AddressException e) {
        LOG.error("Java Mailer Addressing exception: ", e);
        throw new JavaMailerException("Java Mailer Addressing exception: ", e);
    } catch (MessagingException e) {
        LOG.error("Java Mailer messaging exception: ", e);
        throw new JavaMailerException("Java Mailer messaging exception: ", e);
    // } catch (UnsupportedEncodingException e) {
    // log().error("Java Mailer messaging exception: ", e);
    // throw new JavaMailerException("Java Mailer encoding exception: ", e);
    }
}
Also used : MimeBodyPart(javax.mail.internet.MimeBodyPart) BodyPart(javax.mail.BodyPart) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) MessagingException(javax.mail.MessagingException) AddressException(javax.mail.internet.AddressException) DataHandler(javax.activation.DataHandler) MimeBodyPart(javax.mail.internet.MimeBodyPart) File(java.io.File) Date(java.util.Date)

Example 69 with AddressException

use of javax.mail.internet.AddressException in project javautils by jiadongpo.

the class Sender163Test method sendNow.

public void sendNow() {
    Properties props = new Properties();
    props.put("mail.smtp.auth", "true");
    // 没有开外网,程序就跑不动了,163邮箱
    props.put("mail.smtp.host", "smtp.suixingpay.com");
    session = Session.getDefaultInstance(props, new Authenticator() {

        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            // TODO 密码需要填写明文 PasswordAuthentication是一个包装类,里面包含了用户名和密码
            return new PasswordAuthentication("dc_mail@suixingpay.com", "datamail123");
        }
    });
    session.setDebug(true);
    try {
        msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress("dc_mail@suixingpay.com"));
        // 
        // 收件人
        InternetAddress toAddress = new InternetAddress(receiver);
        // 加收件人
        msg.addRecipient(Message.RecipientType.TO, toAddress);
        // 收件人
        InternetAddress ccAddress = new InternetAddress(cc);
        // 加收件人
        msg.addRecipient(Message.RecipientType.CC, ccAddress);
        msg.setSubject(subject);
        msg.setText(mailContent);
        Transport.send(msg);
    } catch (AddressException e) {
        e.printStackTrace();
    } catch (MessagingException e) {
        while ((e = (MessagingException) e.getNextException()) != null) e.printStackTrace();
    }
// senNow end
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) MimeMessage(javax.mail.internet.MimeMessage) AddressException(javax.mail.internet.AddressException) Properties(java.util.Properties)

Example 70 with AddressException

use of javax.mail.internet.AddressException in project javautils by jiadongpo.

the class Sender163 method sendNow.

public void sendNow() {
    Properties props = new Properties();
    props.put("mail.smtp.auth", "true");
    // 163服务器地址: POP3服务器: pop.163.com SMTP服务器: smtp.163.com IMAP服务器: imap.163.com
    // 没有开外网,程序就跑不动了,163邮箱
    props.put("mail.smtp.host", "smtp.163.com");
    // 腾讯企业邮 接收服务器: imap.exmail.qq.com(使用SSL,端口号993) 发送服务器:  smtp.exmail.qq.com(使用SSL,端口号465)
    // props.put("mail.smtp.host", "imap.exmail.qq.com");//没有开外网,程序就跑不动了,腾讯企业邮
    // 验证信息需要通过Session传给邮件服务器,其中Authenticator负责密码校验,如果不需要验证身份就用null或用单参数的getInstance()。
    session = Session.getDefaultInstance(props, new Authenticator() {

        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            // TODO 密码需要填写明文 PasswordAuthentication是一个包装类,里面包含了用户名和密码
            return new PasswordAuthentication("jiadp2012", "*****");
        }
    });
    // 允许调试,因此可以用getDebug()方法取调试信息,消息很多
    session.setDebug(true);
    try {
        // 创建了自己的Session对象后就可以发送消息了,这时需要用到Message消息类型。由于Message是一个抽象类,所以使用时必须使用一个具体的子类型。在大多数情况下,这个子类是javax.mail.internet.MimeMessage。一个MimeMessage是封装了MIME类型数据和报头的消息。消息的报头严格限制为只能使用US-ASCII字符,尽管非ASCII字符可以被编码到某些报头字段中。
        msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress("jiadp2012@163.com"));
        // 一旦创建了会话和消息,并为消息填充了内容,就需要用Address类为信件标上地址。同Message类一样,Address类也是抽象类。可以使用javax.mail.internet.InternetAddress类。
        // 
        // 收件人
        InternetAddress toAddress = new InternetAddress(receiver);
        // 加收件人
        msg.addRecipient(Message.RecipientType.TO, toAddress);
        // 收件人
        InternetAddress ccAddress = new InternetAddress(cc);
        // 加收件人
        msg.addRecipient(Message.RecipientType.CC, ccAddress);
        msg.setSubject(subject);
        msg.setText(mailContent);
        // 发送消息的最后一步操作是使用Transport类。该类使用特定协议(通常是SMTP语言来发送消息。他是一个抽象类,其操作与Session类有些相似。可以通过只调用表态的send()方法来使用该类的默认版本。
        Transport.send(msg);
    } catch (AddressException e) {
        e.printStackTrace();
    } catch (MessagingException e) {
        while ((e = (MessagingException) e.getNextException()) != null) e.printStackTrace();
    }
// senNow end
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) MimeMessage(javax.mail.internet.MimeMessage) AddressException(javax.mail.internet.AddressException) Properties(java.util.Properties)

Aggregations

AddressException (javax.mail.internet.AddressException)130 InternetAddress (javax.mail.internet.InternetAddress)108 MimeMessage (javax.mail.internet.MimeMessage)43 MessagingException (javax.mail.MessagingException)38 ArrayList (java.util.ArrayList)23 IOException (java.io.IOException)21 UnsupportedEncodingException (java.io.UnsupportedEncodingException)21 Address (javax.mail.Address)21 JavaMailInternetAddress (com.zimbra.common.mime.shim.JavaMailInternetAddress)18 Properties (java.util.Properties)18 Session (javax.mail.Session)15 Date (java.util.Date)14 MimeBodyPart (javax.mail.internet.MimeBodyPart)14 MimeMultipart (javax.mail.internet.MimeMultipart)12 File (java.io.File)11 Message (javax.mail.Message)10 Multipart (javax.mail.Multipart)9 DataHandler (javax.activation.DataHandler)8 Test (org.junit.Test)7 ZMimeMessage (com.zimbra.common.zmime.ZMimeMessage)5