Joodoo Joodoo - 2 months ago 5x
Java Question

Sending an Email from Java using APIs, not with localhost

I'm using the code and APIs found here:

When I run the code, my error output is thus:

com.sun.mail.util.MailConnectException: Couldn't connect to host, port: localhost, 25; timeout -1;
nested exception is: Connection refused
at com.sun.mail.smtp.SMTPTransport.openServer(
at com.sun.mail.smtp.SMTPTransport.protocolConnect(
at javax.mail.Service.connect(
at javax.mail.Service.connect(
at javax.mail.Service.connect(
at javax.mail.Transport.send0(
at javax.mail.Transport.send(

As is plain to see from the first line, the primary issue is "Couldn't connect to host, port: localhost" blah blah.

Alright. So, does anyone have any ideas what I should be using INSTEAD of localhost? This is totally not my area of expertise.

(The error log is considerably longer, but, there's a lot of code that is being bounced around. If for whatever reason you want the whole thing, let me know and I'll update it)


I want to thank the StackOverflow community for all of the posts I've seen around the site on this topic, and to those who helped me answer this issue. Please find below my finished code that will receive an email object (from another class) and send it out! Note, I took out the username and password to the Gmail account, obviously :)

import java.util.ArrayList;
import java.util.Properties;

import javax.mail.Authenticator;
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 SendEmail
private class SMTPAuthenticator extends Authenticator
public PasswordAuthentication getPasswordAuthentication()
return new PasswordAuthentication("", "password");

public void createAndSendEmailMessage(ArrayList<?> messageContents) throws MessagingException {
Email email = new Email();
email.setRecipient(messageContents.get(0) + "");
email.setSubject(messageContents.get(1) + "");

public void sendEmailMessage(Email email) throws MessagingException {

// Get system properties
Properties props = System.getProperties();
props = new Properties();
props.put("mail.smtp.user", "");
props.put("", "");
props.put("mail.smtp.port", "587");
props.put("mail.smtp.debug", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.socketFactory.port", "587");
props.put("mail.smtp.socketFactory.class", "");
props.put("mail.smtp.socketFactory.fallback", "false");

SMTPAuthenticator auth = new SMTPAuthenticator();
Session session = Session.getInstance(props, auth);

MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(email.getSender()));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(email.getRecipient()));

Transport transport = session.getTransport("smtps");
transport.connect("", 465, "username", "password");
transport.sendMessage(msg, msg.getAllRecipients());



Since I find it annoying when people post one part of the code, but not the code that calls it, I'm going to show you that, as well!

ArrayList<String> emailInfo = new ArrayList<String>();
emailInfo.add("An account has been created for you!");
emailInfo.add("Here is a message");
SendEmail newEmail = new SendEmail();
try {
} catch (MessagingException e1) {
// TODO Auto-generated catch block


This is trying to use an SMTP server on the local machine. If you're on a Linux box, you could install sendmail and configure that... otherwise you will need to look into using your email provider's SMTP service.

This won't be simple: you'll need authentication and you'll need SSL.

Update: you're using gmail, so you should be able to look up what the gmail SMTP sever is and what config it needs. Probably either or It definitely needs authentication and it definitely requires SSL.

You'll need to change the localhost to the right SMTP server address, as a starter. And authentication is covered at the bottom of the tutorial you linked to. It'll want your gmail username and password.