Hi, I am trying to send a simple email via a servlet. I create a button 'email' and when pressed I want to send a simple email. Could you please check the code to see if everything is all right? Have I understood the whole matter correctly?
 
Theo
 
 
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.net.*;
import sun.net.smtp.SmtpClient;
 
public class NewEmail extends HttpServlet {
 
  public void init(ServletConfig config) throws ServletException
    {super.init(config);}
 
  protected void doGet(HttpServletRequest req, HttpServletResponse res)
      throws ServletException, IOException
  {
   res.setContentType("text/html");
    res.setHeader("pragma", "no-cache");
    PrintWriter out = res.getWriter();
    out.println("<HTML><HEAD><TITLE>Loan page</TITLE></HEAD>");
    out.println("</UL><HR><FORM METHOD=POST>");
    out.println("<INPUT TYPE=SUBMIT NAME=email VALUE=Email>");
    out.println("</FORM><HR></BODY></HTML>");
    out.close();
  }
 
  protected void doPost(HttpServletRequest req, HttpServletResponse res)
      throws ServletException, IOException
  {
      // Email
      PrintStream OutMail;
      SmtpClient  sendmail;
      try
      { sendmail = new SmtpClient("newemail");
        sendmail.from( "myname@mycompany.com" );
        sendmail.to( "myfriend@hiscompany.com" );
        OutMail = sendmail.startMessage();
        OutMail.println("From: name1@com" );
        OutMail.println("To: name2@org" );
        OutMail.println("Subject: something" );
        OutMail.flush();
        OutMail.close();
        sendmail.closeServer();
      }
      catch ( IOException E ){}
 
    res.setContentType("text/html");
    res.setHeader("pragma", "no-cache");
    PrintWriter out = res.getWriter();
    out.println("<HTML><HEAD><TITLE>Answer</TITLE></HEAD><BODY>");
    out.println("An email has already been sent to you!");
    out.println("</BODY></HTML>");
    out.close();
 
}
}

Reply via email to