如何将相同的邮件发送给组的每个收件人?

时间:2011-08-17 08:12:40

标签: java jsf javamail

我正在尝试从我的jsf页面发送邮件。这是我的方法:

public String voegGroepToe()
{
    String resultaat = "overzichtGroepEnProject";
    if(project.getGroepen().size()<project.getMaxAantalGroepen())        
        project.voegGroepToe(groep);

    for(Student s : groep.getStudenten())
    {

    Properties props = new Properties();
    props.put("mail.smtp.host", "smtp.live.com");
    props.put("mail.smtp.port", "587");

    Session mailSession = Session.getDefaultInstance(props);
    Message simpleMessage = new MimeMessage(mailSession);

    InternetAddress fromAddress = null;
    InternetAddress toAddress = null;
    try {
        fromAddress = new InternetAddress(from);
        toAddress = new InternetAddress(s.getEmail());
    } catch (AddressException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    try {
        simpleMessage.setFrom(fromAddress);
        simpleMessage.setRecipient(RecipientType.TO, toAddress);
        simpleMessage.setSubject(subject);
        simpleMessage.setText(message);

        Transport.send(simpleMessage);          
    } catch (MessagingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    }

    return resultaat;
}

resultaat是应用程序在发送电子邮件后应该访问的jsf页面。 groep意味着与学生一起组。邮件应该发送给该组的每个学生。 from =“myownemailadress@hotmail.com”

但这不起作用,它没有给出错误消息,看起来它被卡在了Transport.send ......我做错了什么?

1 个答案:

答案 0 :(得分:3)

您希望在循环之前只连接一次,然后在循环内使用Transport#sendMessage()发送每条消息,然后在循环之后关闭连接 。第二个问题是你似乎没有在任何地方传递用户名/密码。

这是你应该怎么做的:

String host = "smtp.live.com";
int port = 587;
String username = "you@live.com";
String password = "yourpassword";

Properties props = new Properties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", String.valueOf(port));
Session mailSession = Session.getDefaultInstance(props);
Transport transport = null;

try {
    transport = mailSession.getTransport("smtp");
    transport.connect(host, username, password);
    InternetAddress fromAddress = new InternetAddress(from);

    for (Student s : groep.getStudenten()) {
        InternetAddress[] toAddresses = { new InternetAddress(s.getEmail()) };
        Message simpleMessage = new MimeMessage(mailSession);
        simpleMessage.setFrom(fromAddress);
        simpleMessage.setRecipients(RecipientType.TO, toAddresses);
        simpleMessage.setSubject(subject);
        simpleMessage.setText(message);
        simpleMessage.setSentDate(new Date()); // Otherwise you end up in junk.
        simpleMessage.saveChanges(); // Transport#sendMessage() doesn't do it.
        transport.sendMessage(simpleMessage, toAddresses);
    }
} catch (MessagingException e) {
    // Handle it! Display a FacesMessage or something.
} finally {
    if (transport != null) try { transport.close(); } catch (MessagingException ignore) {}
}

(我不保证它会以这种方式工作,我没有使用Live.com SMTP服务器的经验,也许你需要额外的Authenticator

作为完全不同的替代方案,您还可以向groupname@yourdomain.com发送一条消息,并将所有收件人作为BCC发送。

另见:


请注意,此问题与JSF完全无关。在使用main()方法的普通Java类中执行此操作时,您会遇到完全相同的问题。