title: Java mail with domain smtp?
body: <p>I am trying to send email via my domain email. I tried with 2 different ports 465 and 587. However, in both forms this exception occurs.</p>
  
  <p>When I run this code with google smtp 465 port, it works perfectly. But because of daily limit of google, I need to send email via domain email address.</p>
  
  <p>Thanks in advance.</p>
  
  <p>Code:</p>
  
  <pre><code>public static void sendMailTo() throws Exception {
  
      String mailAddress = "mail@mydomain.com";
      String mailPassword = "password";
  
      String messageContext = "Content";
  
      try {
  
          String to = "ahmet****@gmail.com";
  
          Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
          final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
  
          // Get a Properties object
          Properties props = System.getProperties();
          props.setProperty("mail.smtps.host", "smtp.mydomain.com");
          props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
          props.setProperty("mail.smtp.socketFactory.fallback", "false");
          props.setProperty("mail.smtp.port", "587");
          props.setProperty("mail.smtp.socketFactory.port", "587");
          props.setProperty("mail.smtps.auth", "true");
  
          props.put("mail.smtps.quitwait", "false");
  
          Session session = Session.getInstance(props, null);
  
          // -- Create a new message --
          final MimeMessage msg = new MimeMessage(session);
  
          // -- Set the FROM and TO fields --
          msg.setFrom(new InternetAddress(mailAddress));
          msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));
  
          DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
          Date date = new Date();
  
          msg.setSubject("Yeni Kullanici! (" + dateFormat.format(date) + ")");
          msg.setText(messageContext, "utf-8");
          msg.setSentDate(new Date());
  
          SMTPTransport t = (SMTPTransport) session.getTransport("smtps");
  
          t.connect("smtp.mydomain.com", mailAddress, mailPassword);
          t.sendMessage(msg, msg.getAllRecipients());
          t.close();
  
      } catch (Exception e) {
          e.printStackTrace();
      }
  
  }
  </code></pre>
  
  <p>Exception :</p>
  
  <pre><code>com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.mydomain.com, 587; timeout -1;
  
    nested exception is:
      java.net.UnknownHostException: smtp.mydomain.com
      at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2100)
      at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:699)
      at javax.mail.Service.connect(Service.java:345)
      at javax.mail.Service.connect(Service.java:226)
      at verego.MailSender.sendMailToTeacher(MailSender.java:86)
      at verego.MailSender.main(MailSender.java:37)
  
  Caused by: java.net.UnknownHostException: smtp.mydomain.com
      at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:184)
      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:331)
      at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:217)
      at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2066)
      ... 5 more
  </code></pre>