質問

I'm trying to send a mail from my jsf page. this is my method:

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 is the jsf page where the application should go to after it sended the emails. groep means a group with students. the mail should be sent to every student of the group. the from = "myownemailadress@hotmail.com"

But this isn't working and it doesn't give an errormessage, it looks like it get stuck at Transport.send... What am i doing wrong?

役に立ちましたか?

解決

You would rather like to connect only once before the loop and then send every message using Transport#sendMessage() inside the loop and then close the connection after the loop. The second problem is that you don't seem to pass in the username/password anywhere.

Here's how you should do it:

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) {}
}

(I don't guarantee that it will work this way, I don't have experience with Live.com SMTP servers, perhaps you need an additional Authenticator)

As a completely different alternative, you could also send a single message to groupname@yourdomain.com with all those recipients as BCC.

See also:


Please note that this problem is completely unrelated to JSF. You would have exactly the same problem when doing so in a plain vanilla Java class with a main() method.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top