Search in sources :

Example 71 with AddressException

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

the class Sender163QiYe 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
    // props.put("mail.smtp.host", "smtp.163.com");//没有开外网,程序就跑不动了,163邮箱
    // 163邮箱企业
    props.put("mail.smtp.host", "smtphm.qiye.163.com");
    // 验证信息需要通过Session传给邮件服务器,其中Authenticator负责密码校验,如果不需要验证身份就用null或用单参数的getInstance()。
    session = Session.getDefaultInstance(props, new Authenticator() {

        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            // PasswordAuthentication是一个包装类,里面包含了用户名和密码
            return new PasswordAuthentication("dc_mail@suixingpay.com", "DATAmail301512");
        }
    });
    // 允许调试,因此可以用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("dc_mail@suixingpay.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)

Example 72 with AddressException

use of javax.mail.internet.AddressException in project java-docs-samples by GoogleCloudPlatform.

the class MailServlet method sendMultipartMail.

private void sendMultipartMail() {
    Properties props = new Properties();
    Session session = Session.getDefaultInstance(props, null);
    String msgBody = "...";
    try {
        Message msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress("admin@example.com", "Example.com Admin"));
        msg.addRecipient(Message.RecipientType.TO, new InternetAddress("user@example.com", "Mr. User"));
        msg.setSubject("Your Example.com account has been activated");
        msg.setText(msgBody);
        // [START multipart_example]
        // ...
        String htmlBody = "";
        // ...
        byte[] attachmentData = null;
        Multipart mp = new MimeMultipart();
        MimeBodyPart htmlPart = new MimeBodyPart();
        htmlPart.setContent(htmlBody, "text/html");
        mp.addBodyPart(htmlPart);
        MimeBodyPart attachment = new MimeBodyPart();
        InputStream attachmentDataStream = new ByteArrayInputStream(attachmentData);
        attachment.setFileName("manual.pdf");
        attachment.setContent(attachmentDataStream, "application/pdf");
        mp.addBodyPart(attachment);
        msg.setContent(mp);
        // [END multipart_example]
        Transport.send(msg);
    } catch (AddressException e) {
    // ...
    } catch (MessagingException e) {
    // ...
    } catch (UnsupportedEncodingException e) {
    // ...
    }
}
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) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Properties(java.util.Properties) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) ByteArrayInputStream(java.io.ByteArrayInputStream) AddressException(javax.mail.internet.AddressException) MimeBodyPart(javax.mail.internet.MimeBodyPart) Session(javax.mail.Session)

Example 73 with AddressException

use of javax.mail.internet.AddressException in project java-docs-samples by GoogleCloudPlatform.

the class MailServlet method sendSimpleMail.

private void sendSimpleMail() {
    // [START simple_example]
    Properties props = new Properties();
    Session session = Session.getDefaultInstance(props, null);
    try {
        Message msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress("admin@example.com", "Example.com Admin"));
        msg.addRecipient(Message.RecipientType.TO, new InternetAddress("user@example.com", "Mr. User"));
        msg.setSubject("Your Example.com account has been activated");
        msg.setText("This is a test");
        Transport.send(msg);
    } catch (AddressException e) {
    // ...
    } catch (MessagingException e) {
    // ...
    } catch (UnsupportedEncodingException e) {
    // ...
    }
// [END simple_example]
}
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) AddressException(javax.mail.internet.AddressException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Properties(java.util.Properties) Session(javax.mail.Session)

Example 74 with AddressException

use of javax.mail.internet.AddressException in project dataverse by IQSS.

the class MailServiceBean method sendSystemEmail.

public boolean sendSystemEmail(String to, String subject, String messageText) {
    boolean sent = false;
    String rootDataverseName = dataverseService.findRootDataverse().getName();
    String body = messageText + BundleUtil.getStringFromBundle("notification.email.closing", Arrays.asList(BrandingUtil.getInstallationBrandName(rootDataverseName)));
    logger.fine("Sending email to " + to + ". Subject: <<<" + subject + ">>>. Body: " + body);
    try {
        MimeMessage msg = new MimeMessage(session);
        InternetAddress systemAddress = getSystemAddress();
        if (systemAddress != null) {
            msg.setFrom(systemAddress);
            msg.setSentDate(new Date());
            String[] recipientStrings = to.split(",");
            InternetAddress[] recipients = new InternetAddress[recipientStrings.length];
            for (int i = 0; i < recipients.length; i++) {
                try {
                    recipients[i] = new InternetAddress(recipientStrings[i], "", charset);
                } catch (UnsupportedEncodingException ex) {
                    logger.severe(ex.getMessage());
                }
            }
            msg.setRecipients(Message.RecipientType.TO, recipients);
            msg.setSubject(subject, charset);
            msg.setText(body, charset);
            try {
                Transport.send(msg, recipients);
                sent = true;
            } catch (SMTPSendFailedException ssfe) {
                logger.warning("Failed to send mail to: " + to);
                logger.warning("SMTPSendFailedException Message: " + ssfe);
            }
        } else {
            logger.fine("Skipping sending mail to " + to + ", because the \"no-reply\" address not set (" + Key.SystemEmail + " setting).");
        }
    } catch (AddressException ae) {
        logger.warning("Failed to send mail to " + to);
        ae.printStackTrace(System.out);
    } catch (MessagingException me) {
        logger.warning("Failed to send mail to " + to);
        me.printStackTrace(System.out);
    }
    return sent;
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) AddressException(javax.mail.internet.AddressException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) SMTPSendFailedException(com.sun.mail.smtp.SMTPSendFailedException) Date(java.util.Date)

Example 75 with AddressException

use of javax.mail.internet.AddressException in project dataverse by IQSS.

the class MailServiceBean method sendMail.

public void sendMail(String from, String to, String subject, String messageText, Map<Object, Object> extraHeaders) {
    try {
        MimeMessage msg = new MimeMessage(session);
        if (from.matches(EMAIL_PATTERN)) {
            msg.setFrom(new InternetAddress(from));
        } else {
            // set fake from address; instead, add it as part of the message
            // msg.setFrom(new InternetAddress("invalid.email.address@mailinator.com"));
            msg.setFrom(getSystemAddress());
            messageText = "From: " + from + "\n\n" + messageText;
        }
        msg.setSentDate(new Date());
        msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));
        msg.setSubject(subject, charset);
        msg.setText(messageText, charset);
        if (extraHeaders != null) {
            for (Object key : extraHeaders.keySet()) {
                String headerName = key.toString();
                String headerValue = extraHeaders.get(key).toString();
                msg.addHeader(headerName, headerValue);
            }
        }
        Transport.send(msg);
    } catch (AddressException ae) {
        ae.printStackTrace(System.out);
    } catch (MessagingException me) {
        me.printStackTrace(System.out);
    }
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) AddressException(javax.mail.internet.AddressException) Date(java.util.Date)

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