Send mail from java

1

I want to send an email from my Java 8 program to a configurable recipient (gmail, hotmail or corporate) without having to authenticate the sender or make a login, to send the mail. Is this possible in Java 8?

 public static void main(String[] args) {
        try {
            // Get system properties
            Properties props = System.getProperties();

            // Setup mail server
            //props.put("mail.smtp.host", "192.168.1.111");
            props.setProperty("mail.smtp.user", "[email protected]");
            props.setProperty("mail.smtp.auth", "false");

            // Get session
            Session session = Session.getDefaultInstance(props, null);

            // Define message
            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress("[email protected]"));
            message.setSubject("asunto");
            message.addRecipient(Message.RecipientType.TO,new InternetAddress("[email protected]"));
            message.setText("gracias Chuidiang");
            // Envia el mensaje
            Transport.send(message);

        } catch (Throwable e) {
            System.out.println("Fallo sendEmail al enviar Correo: " +e.getMessage());
            e.printStackTrace();
        }
    }
  

com.sun.mail.util.MailConnectException: Could not connect to host, port: localhost, 25; timeout -1;         nested exception is:           java.net.ConnectException: Connection refused: connect           at com.sun.mail.smtp.SMTPTransport.openServer (SMTPTransport.java:2194)           at com.sun.mail.smtp.SMTPTransport.protocolConnect (SMTPTransport.java:726)           at javax.mail.Service.connect (Service.java:366)           at javax.mail.Service.connect (Service.java:246)           at javax.mail.Service.connect (Service.java:195)           at javax.mail.Transport.send0 (Transport.java:254)           at javax.mail.Transport.send (Transport.java:124)           at pe.com.imptec.itickets.test.SendCorreoTest2.main (SendCorreoTest2.java:33)       Caused by: java.net.ConnectException: Connection refused: connect           at java.net.DualStackPlainSocketImpl.connect0 (Native Method)           at java.net.DualStackPlainSocketImpl.socketConnect (DualStackPlainSocketImpl.java:79)           at java.net.AbstractPlainSocketImpl.doConnect (AbstractPlainSocketImpl.java:350)           at java.net.AbstractPlainSocketImpl.connectToAddress (AbstractPlainSocketImpl.java:206)           at java.net.AbstractPlainSocketImpl.connect (AbstractPlainSocketImpl.java:188)           at java.net.PlainSocketImpl.connect (PlainSocketImpl.java:172)           at java.net.SocksSocketImpl.connect (SocksSocketImpl.java:392)           at java.net.Socket.connect (Socket.java:589)           at java.net.Socket.connect (Socket.java:538)           at com.sun.mail.util.SocketFetcher.createSocket (SocketFetcher.java:352)           at com.sun.mail.util.SocketFetcher.getSocket (SocketFetcher.java:238)           at com.sun.mail.smtp.SMTPTransport.openServer (SMTPTransport.java:2160)           ... 7 more

    
asked by Camilo Medina 13.02.2018 в 17:56
source

1 answer

0

Your code is missing the configuration to properly connect to the host with the correct credentials):

final String username = "[email protected]";
final String password = "password";

Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");

Session session = Session.getInstance(props,
  new javax.mail.Authenticator() {
      protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username, password);
      }
  });

try {

      // Define message
      MimeMessage message = new MimeMessage(session);
      message.setFrom(new InternetAddress(username));
      message.setSubject("asunto");
      message.addRecipient(Message.RecipientType.TO,new InternetAddress("[email protected]"));
      message.setText("gracias Chuidiang");
      // Envia el mensaje
      Transport.send(message);
} catch (Exception e) {
}

Eye that the code may still fail, if so make sure that you allow less secure applications to access your account by going to here

    
answered by 13.02.2018 в 23:46