Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to send an email via Java for an assignment. I have to send the email then take a screenshot of the email and send it to my teacher.

What I have tried:

Java
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

public class SendEmail {

   public static void main(String [] args) {    
      // Recipient's email ID needs to be mentioned.
      String to = "***************@gmail.com";//Had actual email here, it is censored

      // Sender's email ID needs to be mentioned
      String from = "*********.**********@********.org";//Had actual email here, it is censored

      // Assuming you are sending email from localhost
      String host = "localhost";//I also tried my own IP address

      // Get system properties
      Properties properties = System.getProperties();

      // Setup mail server
      properties.setProperty("mail.smtp.host", host);

      // Get the default Session object.
      Session session = Session.getDefaultInstance(properties);

      try {
         // Create a default MimeMessage object.
         MimeMessage message = new MimeMessage(session);

         // Set From: header field of the header.
         message.setFrom(new InternetAddress(from));

         // Set To: header field of the header.
         message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));

         // Set Subject: header field
         message.setSubject("Testing...");

         // Now set the actual message
         message.setText("Did it go through?");

         // Send message
         Transport.send(message);
         System.out.println("Sent message successfully....");
      } catch (MessagingException mex) {
         mex.printStackTrace();
      }
   }
}

The code comiles correctly, but I get this.
Caused by: java.net.ConnectException: Connection refused (Connection refused)<br />
	at java.base/java.net.PlainSocketImpl.socketConnect(Native Method)<br />
	at java.base/java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:399)<br />
	at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:242)<br />
	at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:224)<br />
	at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)<br />
	at java.base/java.net.Socket.connect(Socket.java:609)<br />
	at java.base/java.net.Socket.connect(Socket.java:558)<br />
	at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:359)<br />
	at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:238)<br />
	at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2175)<br />
	... 15 more

Am I doing it wrong, do I have the wrong files, or is the BlueJ IDE wrong?
Posted
Comments
Richard MacCutchan 2-Jan-22 3:26am    
You cannot send mail from 'localhost' unless it has a mail server running on it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900