3

I have used the following Java code to send email.

import java.util.*; import javax.mail.*; import javax.mail.internet.*; import javax.activation.*; public class SendEmail { public static void main(String [] args) { String to = "[email protected]"; String from = "[email protected]"; String host = "localhost"; Properties properties = System.getProperties(); properties.setProperty("smtp.gmail.com", host); Session session = Session.getDefaultInstance(properties); try{ MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(from)); message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)) message.setSubject("This is the Subject Line!"); message.setText("This is actual message"); Transport.send(message); System.out.println("Sent message successfully...."); }catch (MessagingException mex) { mex.printStackTrace(); } } } 

When I run the file, I get the following errors:

javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25; nested exception is: java.net.ConnectException: Connection refused: connect at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1282) at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:370) Caused by: java.net.ConnectException: Connection refused: connect at java.net.DualStackPlainSocketImpl.connect0(Native Method) at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source) 

Would really appreciate if someone could help me on this.

How to solve the ConnectException?

4
  • Can you try to telnet to your localhost on the configured SMTP Port (Default:25). telnet localhost 25 and check? Commented Dec 30, 2012 at 9:01
  • Well, do you have an SMTP server running on your machine? Commented Dec 30, 2012 at 9:01
  • I tried telnet localhost 25 bt it ws errored showing Connecting To localhost...Could not open connection to the host, on port 25: Connect failed I have enabled telnet client and telnet server feature in windows feature, I am using windows 7 Commented Dec 30, 2012 at 9:23
  • There might be more info on debugging the telnet timeout here: stackoverflow.com/questions/5179807/… Commented Aug 15, 2013 at 19:04

5 Answers 5

6

I can see you are trying to use Gmail as your SMTP server. This line is incorrect:

properties.setProperty("smtp.gmail.com", host); 

You are using the host name as the property name, which is incorrect. Because you do not set the mail.smtp.host property JavaMail attempts to connect to 'localhost'. Set these following properties instead:

properties.put("mail.smtp.starttls.enable", "true"); properties.put("mail.smtp.host", "smtp.gmail.com"); properties.put("mail.smtp.user", "username"); // User name properties.put("mail.smtp.password", "password"); // password properties.put("mail.smtp.port", "587"); properties.put("mail.smtp.auth", "true"); 
Sign up to request clarification or add additional context in comments.

3 Comments

what is username ? is it the gmail id ? or something else ??
Thank you so much, This really helpful for me I will add my complete code down
it is user id suppose my mail is [email protected] then userid: liferayasif
0

Change host name to : smtp.gmail.com

String host = "localhost"; 

Change this to:

String host = "smtp.gmail.com" 

Comments

0

You have to try something like this

String host = "smtp.gmail.com"; String from = "user name"; Properties props = System.getProperties(); props.put("mail.smtp.host", host); props.put("mail.smtp.user", from); props.put("mail.smtp.password", "asdfgh"); props.put("mail.smtp.port", "587"); // 587 is the port number of yahoo mail props.put("mail.smtp.auth", "true"); Session session = Session.getDefaultInstance(props, null); MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(from)); InternetAddress[] to_address = new InternetAddress[to.length]; int i = 0; // To get the array of addresses while (to[i] != null) { to_address[i] = new InternetAddress(to[i]); i++; } System.out.println(Message.RecipientType.TO); i = 0; while (to_address[i] != null) { message.addRecipient(Message.RecipientType.TO, to_address[i]); i++; } message.setSubject("sending in a group"); message.setText("Welcome to JavaMail"); // alternately, to send HTML mail: // message.setContent("<p>Welcome to JavaMail</p>", "text/html"); Transport transport = session.getTransport("smtp"); transport.connect("smtp.mail.yahoo.co.in", "user name", "asdfgh"); transport.sendMessage(message, message.getAllRecipients()); transport.close(); 

Rigth now you are trying to connect to localhost

Courtesy this answer

Comments

0

Try adding this to your Properties:

private static final String SMTP_HOST = "smtp.gmail.com"; //Use Gmail's SMTP Host. private static final String SMTP_PORT = ""; //Use Gmail's Port Number for SMTP. properties.setProperty("mail.smtp.host", SMTP_HOST); properties.setProperty("mail.smtp.port", SMTP_PORT); 

Comments

0

suppose my

email: [email protected]

paswd: password

public Session getSession() { Properties props = new Properties(); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.host", "smtp.gmail.com"); props.put("mail.smtp.user", "liferayasif"); // User name props.put("mail.smtp.password", "password"); // password props.put("mail.smtp.port", "587"); props.put("mail.smtp.auth", "true"); props.put("mail.session.mail.pop3.host", "pop.gmail.com"); props.put("mail.session.mail.pop3.password", "password"); props.put("mail.session.mail.pop3.port", "110"); props.put(" mail.session.mail.pop3.user", "USER"); props.put("mail.session.mail.imap.host", "imap.gmail.com"); props.put("mail.session.mail.imap.port", "993"); props.put("mail.session.mail.store.protocol", "imap"); props.put("mail.session.mail.transport.protocol", "smtp"); props.put("mail.session.mail.smtp.host", "smtp.gmail.com"); props.put("mail.session.mail.smtp.password", "password"); props.put("mail.session.mail.smtp.user", "[email protected]"); props.put("mail.session.mail.smtp.port", "465"); props.put("mail.session.mail.smtp.auth", "true"); props.put("mail.session.mail.smtp.starttls.enable", "true"); props.put("mail.session.mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); Session session = Session.getInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("liferayasif", "password"); } }); return session; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.