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:
(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.comwith 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.