0

The following is my code. This executed successfully. But I didn't see any mail in my inbox Anybody please help.

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
public class SendMail {
      public static void main(String args[]) throws Exception {


        String fromAddress = "testmail@gmail.com";   
        String toAddress = "testmail@gmail.com";        
        Properties properties = new Properties();     
        properties.put("mail.smtp.host", "localhost");     
        properties.put("mail.smtp.port", "25");     
        properties.put("mail.transport.protocol", "smtp"); 


        try             
        {         
            properties.put("mail.smtp.starttls.enable", "true");

            Session session = Session.getDefaultInstance(properties, null);

            Message message = new MimeMessage(session);         
            message.setFrom(new InternetAddress(fromAddress));         
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toAddress));          
            message.setSubject("Email from our JAMEs");         
            message.setText("hiiiiii!!");         
            Transport.send(message);          
            System.out.println("Email sent");     
        }     
        catch (MessagingException e)     
        {        
            e.printStackTrace();     
        } 
        }
}
Parvathy
  • 2,275
  • 3
  • 24
  • 39

1 Answers1

0

You'll need to either login and send over one of:

  • POP
  • IMAP
  • Gmail API

or you can use a domain you control, at the moment you are trying to send as gmail.com, and being ignored because it knows your IP is not allowed to send as gmail.com, because of SPF, which limits spam by setting who can send as which domains

SPF (Wikipedia)

jrtapsell
  • 6,719
  • 1
  • 26
  • 49