Reputation: 73
I am using this code to send emails to users
static void SendEmail(String email)
{
System.out.println(email);
String recipient = email;
String sender = "*my email*";
String host = "127.0.0.1";
Properties properties = System.getProperties();
properties.setProperty("mail.smtp.host", host);
Session session = Session.getDefaultInstance(properties);
try
{
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(sender));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient));
message.setSubject("Vote Results");
message.setText("Votes have ended, here are the results: Apple:" + votes1 + " Banana:" + votes2);
Transport.send(message);
System.out.println("Sent message successfully....");
}
catch (MessagingException e)
{
e.printStackTrace();
}
}
but it keeps returning this exception:
com.sun.mail.util.MailConnectException: Couldn't connect to host, port: 127.0.0.1, 25; timeout -1; nested exception is:
Upvotes: 0
Views: 337
Reputation: 26
I think the problem is that you are not an SMTP server, and you cannot send emails directly. You could try adding Google SMTP: it is free but is kind of restrictive. Add something like this to your code:
prop.put("mail.smtp.host", "smtp.gmail.com");
prop.put("mail.smtp.port", "587");
prop.put("mail.smtp.auth", "true");
prop.put("mail.smtp.starttls.enable", "true");
Upvotes: 1