使用JavaMail发送邮件时的地址无效

时间:2021-04-06 21:53:43

This is my function to prepare recipient list.

这是我准备收件人列表的功能。

public static InternetAddress[] getRecipienEmail(boolean flag) {
        dbconf conf = new dbconf();
        try {
            String sql = null;
            if (flag) {
                sql = "select email_id from EMP_EMAIL_TEST WHERE to_char(dob,'MM-DD')=to_char(sysdate,'MM-DD')";
            } else {
                sql = "select email_id from EMP_EMAIL_TEST WHERE to_char(dob,'MM-DD')<>to_char(sysdate,'MM-DD') or DOB is NULL";
            }

            PreparedStatement preStatement = conf.getConnection().prepareStatement(sql);

            ResultSet result = preStatement.executeQuery();
            ArrayList email = new ArrayList();
            while (result.next()) {
                email.add(result.getString("email_id"));
            }
            InternetAddress[] address = new InternetAddress[email.size()];
            for (int i = 0; i < email.size(); i++) {
                address[i] = new InternetAddress(email.get(i).toString());
            }
            conf.getConnection().close();
            return address;
        } catch (SQLException | AddressException ex) {
           System.out.println(ex.getMessage());
            Logger.getLogger(EmpEmail.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;

    }

When I'm getting Invalid Recipient error.

当我收到无效的收件人错误。

This is how I'm calling above function. Please suggest, what is wrong here

这就是我在上面调用函数的方式。请建议,这里有什么问题

message.setRecipients(Message.RecipientType.TO,
                    EmpEmail.getRecipienEmail(true));

Please Note: Table has list of emails and It will always return at least one email address.

请注意:表格包含电子邮件列表,并且始终会返回至少一个电子邮件地址。

Update:

I found probable issue. I just found that after below line of code, application through exception.

我发现了可能的问题。我刚发现在下面的代码行之后,应用程序通过异常。

message.setReplyTo(cc);

I'm using same list of emails in Reply-to. Can't I use multiple email address in Reply-To ?

我在回复中使用相同的电子邮件列表。我不能在回复中使用多个电子邮件地址吗?

2 个解决方案

#1


0  

You can try to see what is inside array email. Might be it has empty string as value. Use debugger or print it to System out like this

您可以尝试查看阵列电子邮件中的内容。可能是空字符串作为值。使用调试器或将其打印到System out中

for (int i = 0; i < email.size(); i++) {
    System.out.println("email[" + i + "]: " + email.get(i).toString());
    address[i] = new InternetAddress(email.get(i).toString());
}

#2


0  

You can filter out empty and null emails in select query

您可以在选择查询中过滤掉空和空电子邮件

select email_id from EMP_EMAIL_TEST WHERE to_char(dob,'MM-DD')=to_char(sysdate,'MM-DD') and email_id is not null 
select email_id from EMP_EMAIL_TEST WHERE to_char(dob,'MM-DD')<>to_char(sysdate,'MM-DD') or DOB is NULL and email_id is not null

also you can use Hibernate email validator annotation @Email http://docs.jboss.org/hibernate/validator/5.1/reference/en-US/html/chapter-bean-constraints.html#validator-defineconstraints-hv-constraints

您也可以使用Hibernate电子邮件验证器注释@Email http://docs.jboss.org/hibernate/validator/5.1/reference/en-US/html/chapter-bean-constraints.html#validator-defineconstraints-hv-constraints

#1


0  

You can try to see what is inside array email. Might be it has empty string as value. Use debugger or print it to System out like this

您可以尝试查看阵列电子邮件中的内容。可能是空字符串作为值。使用调试器或将其打印到System out中

for (int i = 0; i < email.size(); i++) {
    System.out.println("email[" + i + "]: " + email.get(i).toString());
    address[i] = new InternetAddress(email.get(i).toString());
}

#2


0  

You can filter out empty and null emails in select query

您可以在选择查询中过滤掉空和空电子邮件

select email_id from EMP_EMAIL_TEST WHERE to_char(dob,'MM-DD')=to_char(sysdate,'MM-DD') and email_id is not null 
select email_id from EMP_EMAIL_TEST WHERE to_char(dob,'MM-DD')<>to_char(sysdate,'MM-DD') or DOB is NULL and email_id is not null

also you can use Hibernate email validator annotation @Email http://docs.jboss.org/hibernate/validator/5.1/reference/en-US/html/chapter-bean-constraints.html#validator-defineconstraints-hv-constraints

您也可以使用Hibernate电子邮件验证器注释@Email http://docs.jboss.org/hibernate/validator/5.1/reference/en-US/html/chapter-bean-constraints.html#validator-defineconstraints-hv-constraints