Email sending program V1.1

Source: Internet
Author: User

This is a servlet application ..

The first is the Web. xml file.

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <web-app version = "2.5" xmlns = "http://java.sun.com/xml/ns/javaee" <br/> xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" <br/> xsi: schemalocation = "http://java.sun.com/xml/ns/javaee <br/> http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <br/> <servlet-Name> sendmailservlet </servlet-Name> <br/> <Servlet -Class> COM. jadyer. servlet. sendmailservlet </servlet-class> <br/> </servlet> <br/> <servlet-mapping> <br/> <servlet-Name> sendmailservlet </servlet-Name> <br/> <URL-pattern>/servlet/sendmail </url-pattern> <br/> </servlet-mapping> <br/> <welcome-file-List> <br/> <welcome-File> Sendmail. JSP </welcome-File> <br/> </welcome-file-List> <br/> </Web-app>

The sendmail. jsp page is used to enter the content of the sent mail.

<% @ Page Language = "Java" contenttype = "text/html; charset = UTF-8 "%> <br/> <center> <br/> <H2> <font color =" blue "> email sender V1.1 </font> </H2> </P> <p> <form action = "<% = request. getcontextpath () %>/servlet/sendmail "method =" Post "> <br/> <Table border =" 22 "> <br/> <tr> <br/> <TD> recipient </TD> <br/> <TD> <input type = "text" name = "to" size = "30"> </TD> <br/> </tr> <br/> <tr> <br/> <TD> sender </TD> <br/> <TD> <input type = "text" name = "from" Size = "30"> </TD> <br/> </tr> <br/> <TD> subject </TD> <br/> <TD> <input type = "text" name = "subject" size = "30"> </TD> <br/> </tr> <br/> <tr> <br/> <TD valign = "TOP"> body </TD> <br/> <TD> <textarea Cols = "50" rows = "15" name = "Body "> </textarea> </TD> <br/> </tr> <br/> <TD> & nbsp; </TD> <br/> <TD> <input type = "Submit" value = "Submit"> </TD> <br/> </tr> <br/> </table> <br/> </form> <br/> </center>

Next is the sendresult. jsp page used to display the mail sending result.

<% @ Page Language = "Java" contenttype = "text/html; charset = UTF-8 "%> </P> <p> <font color =" red ">$ {requestscope ['sendmail. message ']} </font>

The core servlet of the email sending program

Package COM. jadyer. servlet; </P> <p> Import Java. io. ioexception; </P> <p> Import javax. servlet. servletexception; <br/> Import javax. servlet. HTTP. httpservlet; <br/> Import javax. servlet. HTTP. httpservletrequest; <br/> Import javax. servlet. HTTP. httpservletresponse; </P> <p> Import Org. apache. commons. mail. emailexception; <br/> Import Org. apache. commons. mail. simpleemail; </P> <p>/** <br/> * Email sending program V1.1 <br/> * @ see --- ---------------------------------------- <Br/> * @ see the running environment of this program is javase6.0 and javaee5.0 <br/> * @ See example <br/> * @ see apache-commons-email is used here components and mail. jar package <br/> * @ see is a Web-based email sending Program <br/> * @ see the running of this program, no need to install your own email server <br/> * @ see ----------------------------------------- <br/> */<br/> public class sendmailservlet extends httpservlet {<br /> Private Static final long serialversionuid = response; </P> <p> Public void dogst (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {<br/> dopost (request, response); <br/>}</P> <p> Public void dopost (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {<br/> request. setcharacterencoding ("UTF-8"); </P> <p> // Generate simpleemail-type object email <br/> // simpleemail is a class in the Commons-eamil component, encapsulate the behavior of sending emails without attachments <br/> simpleemail email = new simpleemail (); </P> <p> // set the SMTP server name. Here is the free email server address of Netease Yeah <br/> email. sethostname ("smtp.yeah.net "); </P> <p> // set the user name and password required for sending emails using this server <br/> // This user must be a valid user on this free email server <br/> // to put it bluntly, here I use my mailbox jadyer@yeah.net <br/> email. setauthentication ("jadyer", "mypassword"); </P> <p> try {<br/> // sets the file encoding method of the email content <br/> email. setcharset ("UTF-8"); </P> <p> // set recipient <br/> email. addto (request. getparameter ("to"); <br/> // set the sender <br/> email. setfrom (request. getparameter ("from ") ); <Br/> // set the email subject <br/> email. setsubject (request. getparameter ("subject"); <br/> // set the email body <br/> email. setmsg (request. getparameter ("body"); </P> <p> // call the send () method of the email object to send the email. <br/> email. send (); </P> <p> // Note: during the test, do not send spam because it may be returned by the recipient's email server. <br/> request. setattribute ("Sendmail. message "," Congratulations! Email sent successfully! "); <Br/>}catch (emailexception e) {<br/> E. printstacktrace (); <br/> request. setattribute ("Sendmail. message "," Sorry! Failed to send email! "); <Br/>}< br/> request. getrequestdispatcher ("/sendresult. JSP "). forward (request, response); <br/>}< br/>}

During the test: Enter the details of the email you want to send on the sendmail. jsp page.

Recipient: Enter the recipient's email address.

Sender: Enter the jadyer@yeah.net, that is, my email address. This is restricted by line 44th of sendmailservlet. java.

Subject and body: Try to Enter Text of some meaning. Otherwise, the email is easily processed by the recipient's email server as spam, leading to sending failure.

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.