title: Sending email using JSP
body: <p>This problem is driving me nuts. I have the following code:</p>
  
  <pre><code>&lt;html&gt;
  &lt;body&gt;
  &lt;%@ page import="java.util.*" %&gt;
  &lt;%@ page import="javax.mail.*" %&gt;
  &lt;%@ page import="javax.mail.internet.*" %&gt;
  &lt;%@ page import="javax.activation.*" %&gt;
  &lt;%
  String host = "exchsrv2";
  String to = "alan@domain.com";
  String from = "apeince@domain.com";
  String subject = "test";
  String messageText = "body test";
  
  Properties props = System.getProperties();
  props.put("mail.host", host);
  props.put("mail.transport.protocol", "smtp");
  props.put("mail.smtp.port", "25");
  Session mailSession = Session.getDefaultInstance(props, null);
  
  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 + ".");
  %&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Okay, the problem is, I get the following error:</p>
  
  <pre><code>javax.servlet.ServletException: javax.mail.MessagingException: Could not connect to SMTP host: exchsrv2, port: 25;
  nested exception is:
  java.net.SocketException: Permission denied: connect
  org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:911)
  org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:840)
  org.apache.jsp.alan_jsp._jspService(alan_jsp.java:113)
  org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
  javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
  org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:433)
  org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:389)
  org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)
  javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
  </code></pre>
  
  <p>I know Exchange server is there. I can telnet to it just fine. My Exchange server is setup to not require authentication. I got a program that works fine in C#/.NET, and it works fine, so I know the problem isn't in the Exchange server. What am I doing wrong here?</p>