Here is a little example:

/*
 * File: SimpleMailServlet.java
 */

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import sun.net.smtp.SmtpClient;
public class SimpleMailServlet
  extends HttpServlet
{
  public static final String DEFAULT_FROM_MAIL = "mailuser";
  public static final String DEFAULT_MAIL_SERVER = "mailserver";

  private String fromMail;
  private String mailServer;

  /**
   * Initialisierung des Servlets
   * @param config ServletConfiguration
   */
  public void init(ServletConfig config)
    throws ServletException
  {
    super.init(config);
    fromMail = config.getInitParameter("Mail");
    if( fromMail == null )
      fromMail = DEFAULT_FROM_MAIL;
    mailServer = config.getInitParameter("MailServer");
    if( mailServer == null )
      mailServer = DEFAULT_MAIL_SERVER;
  }

  public void service(HttpServletRequest req, HttpServletResponse resp)
    throws IOException
  {
    PrintStream out;
    SmtpClient sendmail;

    try
    {
      String to = req.getParameter("mail");
      String subject = req.getParameter("subject");
      if( subject == null )
        subject = "Ihre Anfrage";

      sendmail = new SmtpClient(mailServer);
      sendmail.from(fromMail);
      sendmail.to(to);

      out = sendmail.startMessage();
      out.println("From: " + fromMail );
      out.println("To: " + to );
      out.println("Subject: " + subject );
      out.println( "\nThis Email was written by the SimpleMailServlet at " +
            (new Date()) + "\n" );

      out.println( "Remote Host: " +  req.getRemoteHost() );
      out.println( "Remote Addr: " +  req.getRemoteAddr() );
      out.println( "Remote User: " +  req.getRemoteUser() );

      out.println( "\nWith greetings from your SimpleMailServlet.");
      out.print("\r\n");
      out.flush();
      out.close();
      sendmail.closeServer();
    }
    catch ( IOException e )
    {
        log( e.toString() );
    }

    resp.setContentType("text/html");
    PrintWriter pw = new PrintWriter( resp.getOutputStream() );

    pw.println( "<HTML><BODY>" );
    pw.println( "<B><I>Thanks</I></B>" );
    pw.println( "<BR><BR><B>...for sending an email.</B>" );
    pw.println( "<BR><BR><B>You will get an message soon.</B>" );
    pw.println( "<BR><BR><BR><BR><B>fromMail:   " + fromMail + "</B>" );
    pw.println( "<BR><BR><B>mailServer: " + mailServer + " </B>" );
    pw.println( "</BODY></HTML>" );
    pw.flush();
  }
}


<HTML>
<HEAD>
<TITLE>SimpleMailServlet</TITLE>
</HEAD>

<BODY>

<H1>Put in something:</H1>

<FORM ACTION="http://localhost:8080/servlet/Mail" METHOD="POST">
<TABLE>
<TR>
  <TD>email:</TD>
  <TD><INPUT TYPE="Text" NAME="mail" SIZE="40" MAXLENGTH="80"></TD>
</TR>
<TR>
  <TD>subject:</TD>
  <TD><INPUT TYPE="Text" NAME="subject" SIZE="40" MAXLENGTH="80"></TD>
</TR>
<TR>
  <TD><INPUT TYPE=SUBMIT Name="aktion" VALUE="Send"></TD>
</TR>
</TABLE>
</FORM>

After putting down the button you will get an email.
</BODY>
</HTML>




> Hi all,  I'm looking for an example on how to mail from a servlet, i
> downloaded the JavaMail API, but i'd like to see an example somewhere to
> help me out.  Right now i'm mailing out with an ASP activex control, but
> i'm going to be posting to a servlet so i'd like to send the mail from
> the servlet.
>
>
> Thanks,
>
> Giscard Girard
>
> ______________________________________________________
> Get Your Private, Free Email at http://www.hotmail.com
>
> ___________________________________________________________________________
> To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
> of the message "signoff SERVLET-INTEREST".
>
> Archives: http://archives.java.sun.com/archives/servlet-interest.html
> Resources: http://java.sun.com/products/servlet/external-resources.html
> LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
>

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to