尝试使用jsp servlet发送电子邮件时会出现异常:javax.mail.internet.AddressException

时间:2014-04-06 07:43:37

标签: java jsp email servlets

我尝试使用jsp发送电子邮件。所有的事情都很完美,但当我尝试使用多个电子邮件地址时,它会显示错误, javax.mail.internet.AddressException:字符串“

中的非法地址

这是我的代码

public EmailSender(String[] to, String subject, String msg) {


        final String txtFromAcc = "abeywicrema@gmail.com";
        final String pwfFromPW = "mjkkmqzrkkgsydqk";
        String txtFrom = "norply@politeknick.com";
        //String txtTo = "abeywicrema@gmail.comshan.a@jinasena.com.lk";//to;
        String txtSubject = subject;
        String txaMessage = msg;
        String[] sendMore = to;//{"abeywicrema@gmail.com", "shan.a@jinasena.com.lk"};



        Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
        Properties prop = new Properties();
        prop.put("mail.smtp.host", "smtp.gmail.com");
        prop.put("mail.smtp.auth", "true");
        prop.put("mail.smtp.port", "465");
        prop.put("mail.smtp.socketFactory.port", "465");
        prop.put("mail.debug", "true");
        prop.put("mail.smtp.socketFactory.port", "465");
        prop.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        prop.put("mail.smtp.socketFactory.fallback", "false");
        Authenticator auth = new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(txtFromAcc, pwfFromPW);
            }
        };
        Session session = Session.getDefaultInstance(prop, auth);

        try {
            Message mail = new MimeMessage(session);
            mail.setFrom(new InternetAddress(txtFrom));
            InternetAddress[] addressTo = new InternetAddress[sendMore.length];
            for (int i = 0; i < sendMore.length; i++) {


                addressTo[i] = new InternetAddress(sendMore[i]);

            }


            mail.setRecipients(Message.RecipientType.TO, addressTo);

            mail.setSubject(txtSubject);
            mail.setContent(txaMessage, "text/plain");
            Transport.send(mail);
            //OptionPane.showMessageDialog(this, "Sent");
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(">>>>>>>>>>>>>>>>msg has been sent ");
    }

0 个答案:

没有答案