2

I am working on a final project for one of my classes and this program is meant to send emails to address in the code. I know how most of the code works, just having trouble understanding the password authentication and how to connect to SMTP servers and using specific ports. The problem with the code is it's not sending the email when run, and not giving any error messages. Any help would be much appreciated. Here's the code.

    package application;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage; 


public class SendEmail {  
 public static void main (String [] args) {  

  String host="smtp.gmail.com";  
  final String user="myemail@gmail.com";
  final String password="password";

  String to="targetemail.com";

    //imported code
   Properties props = new Properties(); 
   props.put("mail.smtp.socketfactory.port",  "465");
   props.put("mail.smtp.port",  "465");
   props.put("mail.smtp.host",host);  
   props.put("mail.smtp.auth", "true");  

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

 //imported code
        try {  
         MimeMessage message = new MimeMessage(session);  
         message.setFrom(new InternetAddress(user));  
         message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));  
         message.setSubject("Dwight from the future");  
         message.setText("At 8:00, someone poisons the coffee. Do NOT drink 
    it.");  


         Transport.send(message);  

         System.out.println("message sent!");  

         } 
        catch (MessagingException mex) 
        {
            System.out.println("Error: unable to send message....");
            mex.printStackTrace();
        }
     }  
   }  
Jai
  • 8,165
  • 2
  • 21
  • 52
Rinzler
  • 23
  • 1
  • 3
  • please check https: security.. because almost all the smtp server using ssl protocol – Onic Team Nov 27 '17 at 04:32
  • Here is the complete working example http://javabycode.com/spring-framework-tutorial/spring-boot-tutorial/spring-boot-freemarker-email-template.html – David Pham Nov 27 '17 at 04:41
  • @VedPrakash He's using port 465, i.e., smtps. – Robert Nov 27 '17 at 04:41
  • In your code, `to` is not an email address, but a domain. I find it strange that you wouldn't see an error message, though. – Robert Nov 27 '17 at 04:43
  • Fix any of these [common JavaMail mistakes](https://javaee.github.io/javamail/FAQ#commonmistakes) in your code and see these [Gmail instructions in the JavaMail FAQ](https://javaee.github.io/javamail/FAQ#gmail). – Bill Shannon Nov 28 '17 at 20:21

2 Answers2

1

I think the port value should be as below

 props.put("mail.smtp.port", "587");

example configuration

 props.put("mail.smtp.host", "smtp.gmail.com"); 
 props.put("mail.smtp.auth", "true"); 
 props.put("mail.smtp.port", "587"); 
 props.put("mail.smtp.starttls.enable", "true"); 
 props.put("mail.smtp.ssl.trust", "*");
Praveen Kumar
  • 1,515
  • 1
  • 21
  • 39
  • Thank you! Do you hava an idea of what the props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.ssl.trust", "*"); code does? @praveen – Rinzler Nov 27 '17 at 04:40
  • https://stackoverflow.com/questions/18333594/is-starttls-enabled-true-is-safe-for-mail-sending-from-java-code this link should answer your question – Praveen Kumar Nov 27 '17 at 04:44
  • https://stackoverflow.com/questions/4894954/how-to-ignore-server-cert-error-in-javamail this is the meaning for mail.smtp.ssl.trust – Praveen Kumar Nov 27 '17 at 04:45
0

Please try the below code (modified the port value).

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;


public class Main {

    public static void main(String[] args) {

        String host="smtp.gmail.com";
        final String user="email@gmail.com";
        final String password="*********";

        String to="username@gmail.com";

        //imported code
        Properties props = new Properties();
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.user", user);
        props.put("mail.smtp.password", password);
        props.put("mail.smtp.port", "587");
        props.put("mail.smtp.auth", "true");


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

        //imported code
        try {
            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress(user));
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
            message.setSubject("Dwight from the future");
            message.setText("At 8:00, someone poisons the coffee. Do NOT drinkit.");


                    Transport.send(message);

            System.out.println("message sent!");

        }
        catch (MessagingException mex)
        {
            System.out.println("Error: unable to send message....");
            mex.printStackTrace();
        }

    }
}
Karan
  • 695
  • 1
  • 6
  • 16