0

I am trying to send an email from one account to another using javamail. But the code is unable to execute Transport.send(msg); line. What could be the possible reason? Below is the following jsp code.

<%

        String host = "localhost";
        String to = request.getParameter("to");
        String from = request.getParameter("from");
        String subject = request.getParameter("subject");
        String messageText = request.getParameter("body");
        boolean sessionDebug = false;

        Properties props = System.getProperties();
        props.put("mail.host", host);
        props.put("mail.transport.protocol", "smtp");

        Session mailSession = Session.getDefaultInstance(props, null);

        mailSession.setDebug(sessionDebug);

        try { 
        Message msg = new MimeMessage(mailSession);

        msg.setFrom(new InternetAddress(from));
        InternetAddress[] address = {new InternetAddress(to)};
        msg.setRecipients(Message.RecipientType.TO, address);
        msg.setSubject(subject);
        msg.setSentDate(new Date());
        msg.setText(messageText);

        Transport.send(msg);

        out.println("Mail was sent to " + to);
        out.println(" from " + from);
        out.println(" using host " + host + ".");
        } catch (MessagingException mex) {mex.printStackTrace();}
    %>
user207421
  • 305,947
  • 44
  • 307
  • 483
John Durn
  • 45
  • 3
  • 9

2 Answers2

2

These are the properties that I usually use

properties.put("mail.transport.protocol", "smtp");
properties.put("mail.smtp.host", host);
properties.put("mail.user", from);
properties.put("mail.smtp.port", smtpPort);
properties.put("mail.smtp.localhost", "myHost");

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

You will notice that I set mail.smtp.localhost which needs to be set if the machines hostname is not set up properly e.g. for a VM etc

Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
0

Try using property mail.smtp_port instead of mail.smtp.port or:

mail.smtp.port=
mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
mail.smtp.socketFactory.fallback=false
Kelvin Schoofs
  • 8,323
  • 1
  • 12
  • 31