JavaxException Unknown SMTP host Exception: mail.gmail.com





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I am trying to send mail using Java code . The code is working fine when running on my personal PC . But on office network the exception of unknown SMTP host is appearing. Also my office pc is not able to ping smtp.gmail.com. PC firewall is closed as well.
Is there any other way to establish the connection? I am also providing my code below for reference.



mport javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
import javax.mail.Authenticator;

public class otp {
String d_email = "email@gmail.com",
d_password = "password",
d_uname="uname",//your email password
d_host = "mail.outlook.com",
d_port = "587",
m_to = "target@gmail.com", // Target email address
m_subject = "Testing Mail programs",
m_text = "Hey, this is a test email.";

public otp() {
Properties props = new Properties();
props.put("mail.smtp.user", d_email);
props.put("mail.smtp.host", d_host);
props.put("mail.smtp.port", d_port);
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.debug", "true");
props.put("mail.smtp.socketFactory.port", d_port);
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
try {
Authenticator auth = new SMTPAuthenticator();
Session session = Session.getInstance(props,auth);
session.setDebug(true);
MimeMessage msg = new MimeMessage(session);
msg.setText(m_text);
msg.setSubject(m_subject);
System.out.println(1);
msg.setFrom(new InternetAddress(d_email));

msg.addRecipient(Message.RecipientType.TO, new InternetAddress(m_to));

System.out.println(3);
Transport transport = session.getTransport("smtp");
transport.connect(d_host, Integer.valueOf(d_port),d_uname , d_password);
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();

Transport.send(msg);
System.out.println("Message Sent succesfully");
} catch (Exception mex) {
mex.printStackTrace();
}
}

public static void main(String args) {
otp blah = new otp();
}

private class SMTPAuthenticator extends javax.mail.Authenticator {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(d_email, d_password);
}
}
}









share|improve this question































    0















    I am trying to send mail using Java code . The code is working fine when running on my personal PC . But on office network the exception of unknown SMTP host is appearing. Also my office pc is not able to ping smtp.gmail.com. PC firewall is closed as well.
    Is there any other way to establish the connection? I am also providing my code below for reference.



    mport javax.mail.*;
    import javax.mail.internet.*;
    import java.util.*;
    import javax.mail.Authenticator;

    public class otp {
    String d_email = "email@gmail.com",
    d_password = "password",
    d_uname="uname",//your email password
    d_host = "mail.outlook.com",
    d_port = "587",
    m_to = "target@gmail.com", // Target email address
    m_subject = "Testing Mail programs",
    m_text = "Hey, this is a test email.";

    public otp() {
    Properties props = new Properties();
    props.put("mail.smtp.user", d_email);
    props.put("mail.smtp.host", d_host);
    props.put("mail.smtp.port", d_port);
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.debug", "true");
    props.put("mail.smtp.socketFactory.port", d_port);
    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.socketFactory.fallback", "false");
    try {
    Authenticator auth = new SMTPAuthenticator();
    Session session = Session.getInstance(props,auth);
    session.setDebug(true);
    MimeMessage msg = new MimeMessage(session);
    msg.setText(m_text);
    msg.setSubject(m_subject);
    System.out.println(1);
    msg.setFrom(new InternetAddress(d_email));

    msg.addRecipient(Message.RecipientType.TO, new InternetAddress(m_to));

    System.out.println(3);
    Transport transport = session.getTransport("smtp");
    transport.connect(d_host, Integer.valueOf(d_port),d_uname , d_password);
    transport.sendMessage(msg, msg.getAllRecipients());
    transport.close();

    Transport.send(msg);
    System.out.println("Message Sent succesfully");
    } catch (Exception mex) {
    mex.printStackTrace();
    }
    }

    public static void main(String args) {
    otp blah = new otp();
    }

    private class SMTPAuthenticator extends javax.mail.Authenticator {
    public PasswordAuthentication getPasswordAuthentication() {
    return new PasswordAuthentication(d_email, d_password);
    }
    }
    }









    share|improve this question



























      0












      0








      0








      I am trying to send mail using Java code . The code is working fine when running on my personal PC . But on office network the exception of unknown SMTP host is appearing. Also my office pc is not able to ping smtp.gmail.com. PC firewall is closed as well.
      Is there any other way to establish the connection? I am also providing my code below for reference.



      mport javax.mail.*;
      import javax.mail.internet.*;
      import java.util.*;
      import javax.mail.Authenticator;

      public class otp {
      String d_email = "email@gmail.com",
      d_password = "password",
      d_uname="uname",//your email password
      d_host = "mail.outlook.com",
      d_port = "587",
      m_to = "target@gmail.com", // Target email address
      m_subject = "Testing Mail programs",
      m_text = "Hey, this is a test email.";

      public otp() {
      Properties props = new Properties();
      props.put("mail.smtp.user", d_email);
      props.put("mail.smtp.host", d_host);
      props.put("mail.smtp.port", d_port);
      props.put("mail.smtp.starttls.enable", "true");
      props.put("mail.smtp.debug", "true");
      props.put("mail.smtp.socketFactory.port", d_port);
      props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
      props.put("mail.smtp.socketFactory.fallback", "false");
      try {
      Authenticator auth = new SMTPAuthenticator();
      Session session = Session.getInstance(props,auth);
      session.setDebug(true);
      MimeMessage msg = new MimeMessage(session);
      msg.setText(m_text);
      msg.setSubject(m_subject);
      System.out.println(1);
      msg.setFrom(new InternetAddress(d_email));

      msg.addRecipient(Message.RecipientType.TO, new InternetAddress(m_to));

      System.out.println(3);
      Transport transport = session.getTransport("smtp");
      transport.connect(d_host, Integer.valueOf(d_port),d_uname , d_password);
      transport.sendMessage(msg, msg.getAllRecipients());
      transport.close();

      Transport.send(msg);
      System.out.println("Message Sent succesfully");
      } catch (Exception mex) {
      mex.printStackTrace();
      }
      }

      public static void main(String args) {
      otp blah = new otp();
      }

      private class SMTPAuthenticator extends javax.mail.Authenticator {
      public PasswordAuthentication getPasswordAuthentication() {
      return new PasswordAuthentication(d_email, d_password);
      }
      }
      }









      share|improve this question
















      I am trying to send mail using Java code . The code is working fine when running on my personal PC . But on office network the exception of unknown SMTP host is appearing. Also my office pc is not able to ping smtp.gmail.com. PC firewall is closed as well.
      Is there any other way to establish the connection? I am also providing my code below for reference.



      mport javax.mail.*;
      import javax.mail.internet.*;
      import java.util.*;
      import javax.mail.Authenticator;

      public class otp {
      String d_email = "email@gmail.com",
      d_password = "password",
      d_uname="uname",//your email password
      d_host = "mail.outlook.com",
      d_port = "587",
      m_to = "target@gmail.com", // Target email address
      m_subject = "Testing Mail programs",
      m_text = "Hey, this is a test email.";

      public otp() {
      Properties props = new Properties();
      props.put("mail.smtp.user", d_email);
      props.put("mail.smtp.host", d_host);
      props.put("mail.smtp.port", d_port);
      props.put("mail.smtp.starttls.enable", "true");
      props.put("mail.smtp.debug", "true");
      props.put("mail.smtp.socketFactory.port", d_port);
      props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
      props.put("mail.smtp.socketFactory.fallback", "false");
      try {
      Authenticator auth = new SMTPAuthenticator();
      Session session = Session.getInstance(props,auth);
      session.setDebug(true);
      MimeMessage msg = new MimeMessage(session);
      msg.setText(m_text);
      msg.setSubject(m_subject);
      System.out.println(1);
      msg.setFrom(new InternetAddress(d_email));

      msg.addRecipient(Message.RecipientType.TO, new InternetAddress(m_to));

      System.out.println(3);
      Transport transport = session.getTransport("smtp");
      transport.connect(d_host, Integer.valueOf(d_port),d_uname , d_password);
      transport.sendMessage(msg, msg.getAllRecipients());
      transport.close();

      Transport.send(msg);
      System.out.println("Message Sent succesfully");
      } catch (Exception mex) {
      mex.printStackTrace();
      }
      }

      public static void main(String args) {
      otp blah = new otp();
      }

      private class SMTPAuthenticator extends javax.mail.Authenticator {
      public PasswordAuthentication getPasswordAuthentication() {
      return new PasswordAuthentication(d_email, d_password);
      }
      }
      }






      java jsp ide






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 3 at 9:58







      Kritika

















      asked Jan 3 at 7:26









      KritikaKritika

      11




      11
























          2 Answers
          2






          active

          oldest

          votes


















          0














          I guess that you are behind the internal firewall/proxy of your office, it is very common to put the whole internal network of a company behind a central firewall or to check Outgoing/Incoming requests of the networks traffic a Dynamic Proxy Server.in that case, you can check it in the proxy settings of your pc.
          internet explorer(or any browser)-> settings -> internet option -> Connections Tab -> LAN Settings.



          for the granular analyses,please attach your code.






          share|improve this answer
























          • please check the code attached below. Thanks :)

            – Kritika
            Jan 3 at 9:59











          • i tried to disable the proxy still iasm facing the same error message of Unknown SMTP host: smtp.gmail.com;

            – Kritika
            Jan 3 at 10:08











          • normally you cannot easily disable a dynamic-proxy on your local pc,basically such a system configuration is managed by system admin

            – Mohammad Alavi
            Jan 4 at 13:06



















          0














          you are using a gmail account but you have provided a outlook host , try the following template:



          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 SendEmailUsingGMailSMTP {
          public static void main(String args) {
          // Recipient's email ID needs to be mentioned.
          String to = "xyz@gmail.com";//change accordingly

          // Sender's email ID needs to be mentioned
          String from = "abc@gmail.com";//change accordingly
          final String username = "abc";//change accordingly
          final String password = "*****";//change accordingly

          // >> gmail host
          String host = "smtp.gmail.com";

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

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

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

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

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

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

          // Now set the actual message
          message.setText("Hello, this is sample for to check send "
          + "email using JavaMailAPI ");

          // Send message
          Transport.send(message);

          System.out.println("Sent message successfully....");

          } catch (MessagingException e) {
          throw new RuntimeException(e);
          }enter code here
          }
          }





          share|improve this answer
























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54018019%2fjavaxexception-unknown-smtp-host-exception-mail-gmail-com%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            I guess that you are behind the internal firewall/proxy of your office, it is very common to put the whole internal network of a company behind a central firewall or to check Outgoing/Incoming requests of the networks traffic a Dynamic Proxy Server.in that case, you can check it in the proxy settings of your pc.
            internet explorer(or any browser)-> settings -> internet option -> Connections Tab -> LAN Settings.



            for the granular analyses,please attach your code.






            share|improve this answer
























            • please check the code attached below. Thanks :)

              – Kritika
              Jan 3 at 9:59











            • i tried to disable the proxy still iasm facing the same error message of Unknown SMTP host: smtp.gmail.com;

              – Kritika
              Jan 3 at 10:08











            • normally you cannot easily disable a dynamic-proxy on your local pc,basically such a system configuration is managed by system admin

              – Mohammad Alavi
              Jan 4 at 13:06
















            0














            I guess that you are behind the internal firewall/proxy of your office, it is very common to put the whole internal network of a company behind a central firewall or to check Outgoing/Incoming requests of the networks traffic a Dynamic Proxy Server.in that case, you can check it in the proxy settings of your pc.
            internet explorer(or any browser)-> settings -> internet option -> Connections Tab -> LAN Settings.



            for the granular analyses,please attach your code.






            share|improve this answer
























            • please check the code attached below. Thanks :)

              – Kritika
              Jan 3 at 9:59











            • i tried to disable the proxy still iasm facing the same error message of Unknown SMTP host: smtp.gmail.com;

              – Kritika
              Jan 3 at 10:08











            • normally you cannot easily disable a dynamic-proxy on your local pc,basically such a system configuration is managed by system admin

              – Mohammad Alavi
              Jan 4 at 13:06














            0












            0








            0







            I guess that you are behind the internal firewall/proxy of your office, it is very common to put the whole internal network of a company behind a central firewall or to check Outgoing/Incoming requests of the networks traffic a Dynamic Proxy Server.in that case, you can check it in the proxy settings of your pc.
            internet explorer(or any browser)-> settings -> internet option -> Connections Tab -> LAN Settings.



            for the granular analyses,please attach your code.






            share|improve this answer













            I guess that you are behind the internal firewall/proxy of your office, it is very common to put the whole internal network of a company behind a central firewall or to check Outgoing/Incoming requests of the networks traffic a Dynamic Proxy Server.in that case, you can check it in the proxy settings of your pc.
            internet explorer(or any browser)-> settings -> internet option -> Connections Tab -> LAN Settings.



            for the granular analyses,please attach your code.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jan 3 at 7:57









            Mohammad AlaviMohammad Alavi

            1




            1













            • please check the code attached below. Thanks :)

              – Kritika
              Jan 3 at 9:59











            • i tried to disable the proxy still iasm facing the same error message of Unknown SMTP host: smtp.gmail.com;

              – Kritika
              Jan 3 at 10:08











            • normally you cannot easily disable a dynamic-proxy on your local pc,basically such a system configuration is managed by system admin

              – Mohammad Alavi
              Jan 4 at 13:06



















            • please check the code attached below. Thanks :)

              – Kritika
              Jan 3 at 9:59











            • i tried to disable the proxy still iasm facing the same error message of Unknown SMTP host: smtp.gmail.com;

              – Kritika
              Jan 3 at 10:08











            • normally you cannot easily disable a dynamic-proxy on your local pc,basically such a system configuration is managed by system admin

              – Mohammad Alavi
              Jan 4 at 13:06

















            please check the code attached below. Thanks :)

            – Kritika
            Jan 3 at 9:59





            please check the code attached below. Thanks :)

            – Kritika
            Jan 3 at 9:59













            i tried to disable the proxy still iasm facing the same error message of Unknown SMTP host: smtp.gmail.com;

            – Kritika
            Jan 3 at 10:08





            i tried to disable the proxy still iasm facing the same error message of Unknown SMTP host: smtp.gmail.com;

            – Kritika
            Jan 3 at 10:08













            normally you cannot easily disable a dynamic-proxy on your local pc,basically such a system configuration is managed by system admin

            – Mohammad Alavi
            Jan 4 at 13:06





            normally you cannot easily disable a dynamic-proxy on your local pc,basically such a system configuration is managed by system admin

            – Mohammad Alavi
            Jan 4 at 13:06













            0














            you are using a gmail account but you have provided a outlook host , try the following template:



            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 SendEmailUsingGMailSMTP {
            public static void main(String args) {
            // Recipient's email ID needs to be mentioned.
            String to = "xyz@gmail.com";//change accordingly

            // Sender's email ID needs to be mentioned
            String from = "abc@gmail.com";//change accordingly
            final String username = "abc";//change accordingly
            final String password = "*****";//change accordingly

            // >> gmail host
            String host = "smtp.gmail.com";

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

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

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

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

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

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

            // Now set the actual message
            message.setText("Hello, this is sample for to check send "
            + "email using JavaMailAPI ");

            // Send message
            Transport.send(message);

            System.out.println("Sent message successfully....");

            } catch (MessagingException e) {
            throw new RuntimeException(e);
            }enter code here
            }
            }





            share|improve this answer




























              0














              you are using a gmail account but you have provided a outlook host , try the following template:



              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 SendEmailUsingGMailSMTP {
              public static void main(String args) {
              // Recipient's email ID needs to be mentioned.
              String to = "xyz@gmail.com";//change accordingly

              // Sender's email ID needs to be mentioned
              String from = "abc@gmail.com";//change accordingly
              final String username = "abc";//change accordingly
              final String password = "*****";//change accordingly

              // >> gmail host
              String host = "smtp.gmail.com";

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

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

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

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

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

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

              // Now set the actual message
              message.setText("Hello, this is sample for to check send "
              + "email using JavaMailAPI ");

              // Send message
              Transport.send(message);

              System.out.println("Sent message successfully....");

              } catch (MessagingException e) {
              throw new RuntimeException(e);
              }enter code here
              }
              }





              share|improve this answer


























                0












                0








                0







                you are using a gmail account but you have provided a outlook host , try the following template:



                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 SendEmailUsingGMailSMTP {
                public static void main(String args) {
                // Recipient's email ID needs to be mentioned.
                String to = "xyz@gmail.com";//change accordingly

                // Sender's email ID needs to be mentioned
                String from = "abc@gmail.com";//change accordingly
                final String username = "abc";//change accordingly
                final String password = "*****";//change accordingly

                // >> gmail host
                String host = "smtp.gmail.com";

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

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

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

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

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

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

                // Now set the actual message
                message.setText("Hello, this is sample for to check send "
                + "email using JavaMailAPI ");

                // Send message
                Transport.send(message);

                System.out.println("Sent message successfully....");

                } catch (MessagingException e) {
                throw new RuntimeException(e);
                }enter code here
                }
                }





                share|improve this answer













                you are using a gmail account but you have provided a outlook host , try the following template:



                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 SendEmailUsingGMailSMTP {
                public static void main(String args) {
                // Recipient's email ID needs to be mentioned.
                String to = "xyz@gmail.com";//change accordingly

                // Sender's email ID needs to be mentioned
                String from = "abc@gmail.com";//change accordingly
                final String username = "abc";//change accordingly
                final String password = "*****";//change accordingly

                // >> gmail host
                String host = "smtp.gmail.com";

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

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

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

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

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

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

                // Now set the actual message
                message.setText("Hello, this is sample for to check send "
                + "email using JavaMailAPI ");

                // Send message
                Transport.send(message);

                System.out.println("Sent message successfully....");

                } catch (MessagingException e) {
                throw new RuntimeException(e);
                }enter code here
                }
                }






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 4 at 10:18









                Mohammad AlaviMohammad Alavi

                1




                1






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54018019%2fjavaxexception-unknown-smtp-host-exception-mail-gmail-com%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    MongoDB - Not Authorized To Execute Command

                    How to fix TextFormField cause rebuild widget in Flutter

                    in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith