Hi experts, I am having a problem with sending mail from servlet
I have written this code :

import javax.mail.Session;
   import javax.mail.Message;
   import javax.mail.Transport;
   import javax.mail.MessagingException;
   import javax.mail.internet.MimeMessage;
   import javax.mail.internet.InternetAddress;
   import java.util.Properties;

 
 public class EmailSenderDemo {
   public static void main(String[] args)  {
    
        String from = "ali@mlabs.com";
        String to = "ali@mlabs.com";
        String subject = "Hi There...";
        String text = "How are you?";
        String host = "smtp.mlabs.com";
    
        Properties properties = new Properties();
        properties.put("mail.smtp.host", host);
        properties.put("mail.smtp.port", "25");
        properties.put("mail.smtp.debug", "true");
        Session session = Session.getDefaultInstance(properties, null);
        try {
                   Message message = new MimeMessage(session);
                   message.setFrom(new InternetAddress(from));     message.setRecipient(Message.RecipientType.TO, new       InternetAddress(to));
                 message.setSubject(subject);
                message.setText(text);
          
               Transport.send(message);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
}

but I got this error:

javax.mail.MessagingException: Unknown SMTP host: smtp.mlabs.com;
nested exception is:
java.net.UnknownHostException: smtp.mlabs.com
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1280)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:370)
at javax.mail.Service.connect(Service.java:275)
at javax.mail.Service.connect(Service.java:156)
at javax.mail.Service.connect(Service.java:105)
at javax.mail.Transport.send0(Transport.java:168)
at javax.mail.Transport.send(Transport.java:98)
at EmailSenderDemo.main(EmailSenderDemo.java:53)
Caused by: java.net.UnknownHostException: smtp.mlabs.com
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:177)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:519)
at java.net.Socket.connect(Socket.java:469)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:232)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:189)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1250)
... 7 more

please help!

Use mail.host key instead of mail.smtp.host.

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

            Authenticator auth = new SMTPAuthenticator(login, password);

            Session session = Session.getInstance(props, auth);

            MimeMessage msg = new MimeMessage(session);
            msg.setText(message);
            msg.setSubject(subject);
            msg.setFrom(new InternetAddress(from));
            msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
            Transport.send(msg);
           ......
This article has been dead for over six months. Start a new discussion instead.