web-dev-qa-db-ja.com

Javaメールを送信する違法なセミコロン?

データベースからJavaでメールを送信しようとしています。何らかの理由でmainメソッドを実行した後、次のエラーが発生します。

Exception in thread "main" javax.mail.internet.AddressException: Illegal semicolon, not in group in string ``[email protected];[email protected];[email protected];[email protected];[email protected]'' at position 23
    at javax.mail.internet.InternetAddress.parse(InternetAddress.Java:929)
    at javax.mail.internet.InternetAddress.parse(InternetAddress.Java:638)
    at javax.mail.internet.InternetAddress.parse(InternetAddress.Java:615)
    at EmailSender.sendEmail(TestSendEmails.Java:120)
    at EmailSender.sendEmail(TestSendEmails.Java:128)
    at Main.main(Main.Java:8)

配列リストが間違って作成されていると思います。これが私のコードです:

import Java.sql.Connection;
import Java.sql.DriverManager;
import Java.sql.PreparedStatement;
import Java.sql.ResultSet;
import Java.sql.SQLException;
import Java.util.ArrayList;
import Java.util.List;
import Java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class TestSendEmails {
    private String emailTo;
    private String emailSubject;
    private String emailBody;
    private String emailAttachments;

    public TestSendEmails(){

    }

    public TestSendEmails(String emailTo, String emailSubject, String emailBody, String emailAttachments){
        super();
        this.emailTo = emailTo;
        this.emailSubject = emailSubject;
        this.emailBody = emailBody;
        this.emailAttachments = emailAttachments;
    }

    public String getEmailTo(){
        return emailTo;
    }

    public void setEmailTo(String emailTo){
        this.emailTo = emailTo;
    }

    public String getEmailSubject(){
        return emailSubject;
    }

    public void setEmailSubject(String emailSubject){
        this.emailSubject = emailSubject;
    }

    public String getEmailBody(){
        return emailBody;
    }

    public void setEmailBody(String emailBody){
        this.emailBody = emailBody;
    }

    public String getEmailAttachments(){
        return emailAttachments;
    }

    public void setEmailAttachments(String emailAttachments){
        this.emailAttachments = emailAttachments;
    }
}

class TestSendEmailD{
    private Connection con;

    private static final String GET_EMAILS = "Select * From Emails";

    private void connect() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException{
        Class.forName("com.Microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
        con = DriverManager.getConnection("jdbc:sqlserver://100.000.000.00\\SQLEXPRESS:3333;databaseName=dEmails;user=sys;password=admin");
    }

    public List<TestSendEmails> getTestSendEmails() throws Exception{
        connect();
        PreparedStatement ps = con.prepareStatement(GET_EMAILS);
        ResultSet rs = ps.executeQuery();
        List<TestSendEmails> result = new ArrayList<TestSendEmails>();
        while(rs.next()){
            result.add(new TestSendEmails(rs.getString("emailTo"), rs.getString("emailSubject"),rs.getString("emailBody"),rs.getString("emailAttachments")));
        }
        disconnect();
        return result;
    }

    private void disconnect() throws SQLException{
        if(con != null){
            con.close();
        }
    }
}

class EmailSender{
    private Session session;

    private void init(){
        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.Host", "100.000.000.00");
        props.put("mail.smtp.port", "123");

        session = Session.getInstance(props,
                  new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication("[email protected]", "1234");
                    }
                  });
    }

    public void sendEmail(TestSendEmails s) throws MessagingException{
        init();
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress("[email protected]"));
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(s.getEmailTo()));
        message.setSubject(s.getEmailSubject());
        message.setText(s.getEmailBody());
        Transport.send(message);
    }

    public void sendEmail(List<TestSendEmails> emails) throws MessagingException{
        for(TestSendEmails TestSendEmails:emails ){
            sendEmail(TestSendEmails);
        }
    }
}

これが私のmain.Javaです。

public class Main {
    public static void main(String[] args) throws Exception {
        TestSendEmailD dao=new TestSendEmailD();
        List<TestSendEmails> list=dao.getTestSendEmails();
        EmailSender sender=new EmailSender();
        sender.sendEmail(list);
    }
}

誰かがこれを手伝うことができますか?前もって感謝します。

9
espresso_coffee

デフォルトでは、コンマ(,)セミコロン(;)、

InternetAddress[] parse = InternetAddress.parse("[email protected],[email protected]");
System.out.println(parse[0].getAddress());

出力:

[email protected]
12
CoderCroc