Top 10 Java/jsp Learning Series (javamail API sends an email to [servlet])

Source: Internet
Author: User
Tags smtpclient
A: condition must download Sun's javamail API package, address: http://java.sun.com/products/javamail/

I use version 1.2 here and add the relevant package (JAR file) to classpath.

II. This program is very simple, and we don't need to consider a lot of ground layer things, because the API has helped us do these things well. Below is a simple e-mail servlet: (for people familiar with it, i'm afraid it's a simple servlet)

Import java. Io .*;

Import javax. servlet .*;

Import javax. servlet. http .*;

Import sun.net. SMTP .*;

Public class sendmailservlet extends httpservlet {

Public static string mail_from = "from ";

Public static string mail_to = "";

Public static string mail_subject = "subject ";

Public static string mail_body = "body ";

Public static string mail_host = "mailhost ";

Public void doget (httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception

{

Resp. setcontenttype ("text/html; charset = gb2312 ");

Printwriter out = resp. getwriter ();

Out. println ("<form method = post action =/" "+ Req. getrequesturi () +"/"> ");

Out. println ("<Table> ");

Out. println ("<tr> <TD> send mail server: </TD> ");

Out. println ("<TD> <input type = text name =" + mail_host + "size = 30> </TD> </tr> ");

Out. println ("<tr> <TD> from: </TD> ");

Out. println ("<TD> <input type = text name =" + mail_from + "size = 30> </TD> </tr> ");

Out. println ("<tr> <TD> to: </TD> ");

Out. println ("<TD> <input type = text name =" + mail_to + "size = 30> </TD> </tr> ");

Out. println ("<tr> <TD> subject: </TD> ");

Out. println ("<TD> <input type = text name =" + mail_subject + "size = 30> </TD> </tr> ");

Out. println ("<tr> <TD> text: </TD> ");

Out. println ("<TD> <textarea name =" + mail_body + "Cols = 40 rows = 10> </textarea> </TD> </tr> ");

Out. println ("</table> <br> ");

Out. println ("<input type = submit value =/" Send/"> ");

Out. println ("<input type = reset value =/" Reset/"> ");

Out. println ("</form> ");

Out. Flush ();

}

Public void dopost (httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception

{

Resp. setcontenttype ("text/html; charset = gb2312 ");

Printwriter out = new printwriter (resp. getoutputstream ());

String from = Req. getparameter (mail_from );

String to = Req. getparameter (mail_to );

String subject = Req. getparameter (mail_subject );

String body = Req. getparameter (mail_body );

String mailhost = Req. getparameter (mail_host );

Try

{

Smtpclient Mailer = new smtpclient (mailhost );

Mailer. From (from );

Mailer. To ();

Printstream PS = mailer. startmessage ();

PS. println ("from:" + from );

PS. println ("to:" + );

PS. println ("Subject:" + subject );

PS. println (body );

Mailer. closeserver ();

Out. println ("success! ");

}

Catch (exception ex)

{

Out. println ("an error about:" + ex. getmessage ());

}

Out. Flush ();

}

Public void Init (servletconfig CFG) throws servletexception

{

Super. INIT (CFG );

}

Public void destroy ()

{

Super. Destroy ();

}

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.