Hello
I want to sent mail and i am using this code

public boolean sendEmail(String from, String to, String subject, String themessage){
        boolean success=false;
        // Assuming you are sending email from localhost
        String host = "localhost";

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

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

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

        System.err.println("Gone thereee");
        try{
           // Create a default MimeMessage object.
           MimeMessage message = new MimeMessage(mailSession);
           System.err.println("From: "+from);
           // Set From: header field of the header.
           message.setFrom(new InternetAddress(from));
           System.err.println("Message from: "+message.getSender());
           // Set Subject: header field
           message.setSubject(subject);
           // Now set the actual message
           message.setText(themessage);
           System.err.println("To: "+to);
           // Set To: header field of the header.
           message.addRecipient(Message.RecipientType.TO, 
                      new InternetAddress(to));
           System.err.println("Message sender: "+message.getSender());
           //Send message
           Transport.send(message);
           success=true;;
        }catch (MessagingException mex) {
           //mex.printStackTrace();
            System.err.println("Problem in sending: "+mex.getMessage());
        }
        return success;
}

But when i try to send the mail i have this error

From: admin@restaurantlolos.gr
Message from: null
To: info@restaurantlolos.gr
Message sender: null
Problem in sending: Invalid Addresses

Why the message from and message sender are null? They should not have value?
Thank you very much

Member Avatar
LastMitch

Why the message from and message sender are null? They should not have value?

Did you configure this properly:

properties.setProperty("mail.smtp.host", host);

If you are going to send email from localhost meaning on your own computer then you need to configure it correctly on your SMTP setting.

instead of using setproperties() method you can use put method also.
also configure your smtp host.

or use following

properties.put("mail.smtps.host","smtp.gmail.com");
properties.put("mail.smtps.auth","true");