Java Web development 4: Implementation of email sending and SMS sending, java SMS sending

Source: Internet
Author: User

Java Web development 4: Implementation of email sending and SMS sending, java SMS sending

Email sending and text message sending are common in user registration and authentication and system message prompting modules, but the implementation code is too cumbersome. After JSPGen is used, the encapsulation of emails and text messages makes complex activities much easier.

In the SMS sending module, two methods are supported: third-party sending and local platform sending, here we mainly demonstrate the configuration and implementation of third-party sending methods (you do not need to configure the sending method when using the local platform, and rewrite the corresponding implementation class according to the SMS sending interface file ).

I. basic configuration

Jspgen-config.xml
In the basic configuration file of the Framework, locate the mail node and sms node, and configure as follows:

<! -- Mail service --> <mail status = "true"> <smtp> <! -- Sending type (smtp mx) --> <type> SMTP </type> <server> smtp.163.com </server> <port> 25 </port> <email> jspgen@163.com </email> <name> JSPGen official website </name> <username> jspgen </username> <password> jspgen </password> <! -- Content format (text html url) --> <format> html </format> <! -- Content signature --> <sign> JSPGen-makes Java (Web) software development more agile! Www.JSPGen.com </sign> </smtp> </mail> <! -- Sms Service (http) --> <sms status = "true"> <! -- Submit address and method (get post) --> <url> Sending address </url> <method> GET </method> <charset> UTF-8 </charset> <username> <key> name </key> <value> jspgen </value> </username> <password> <key> password </key> <value> jspgen </value> </password> <! -- Whether batch Sending is supported --> <mobile batch = "false"> <key> mobile </key> <gap>; </gap> </mobile> <content> <key> msg </key> </content> <time> <key> dtime </key> <! -- Time format. If it is null, it is a Long timestamp. --> <format> yyyy-MM-dd HH: mm: ss </format> </time> <! -- Return data --> <return> <! -- Return format (text xml json) --> <format> text </format> <! -- Return keyword, which is useful when the format is xml/json --> <key> status </key> <! -- Return success id --> <value> 0 </value> </return> <! -- Content signature --> <sign> [JSPGen] </sign> </sms>

 

Ii. Code Implementation

1. SendAction. java

Package demo. action; import grapes. dates; import grapes. files; import grapes. grapes; import grapes. tools. browser; import java. io. file; import java.net. malformedURLException; import java.net. URL; import java. util. arrayList; import java. util. list; import java. util. map; import jspgen. action. action;/*** Action class: Mail and SMS ** @ author JSPGen * @ copyright (c) JSPGen.com * @ created March 2013 * @ email jspgen@163.com * @ Address www.jspgen.com */public class SendAction extends Action {/*** default method */@ Override public String execute () {return "mail ";} /*** submit an email */public String mail () {boolean isflag = false; Long time = Dates. getTimeMillis (); String to = getParameter ("to"); // receives the form value (form name: to) isflag = sendMail (to, getParameter ("subject "), getParameter ("content"), time); // if (! Isflag) {addActionError ("failed to send! "); Return ERROR;} addActionMessage (" sent successfully! "); Return SUCCESS;}/*** send sms submission */public String sms () {boolean isflag = false; Long time = Dates. getTimeMillis (); String to = getParameter ("to"); // receives the form value (form name: to) isflag = sendSMS (to, getParameter ("content "), time); // if (! Isflag) {addActionError ("failed to send! "); Return ERROR;} addActionMessage (" sent successfully! "); Return SUCCESS ;}}

 

2、mail.html portal page template

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

 

Iii. Test

Enter http: // localhost: 8080/jspgensdf/send. gen in the browser.

 

Note:163 the newly registered account cannot be sent. It seems that it takes two months to use it. You can build an email service environment and test it.

 

 

 


Java Web development email sending and user verification

Each user record defines whether there are only two conditions for marking a and. By default, whether a user's initial a mark is used. After the user clicks "Verification Successful", change "a = No" to "a = yes ".

How does java web implement email sending or communication in its own system?

If it is for common sending and receiving messages, you can simply check the database.
If you want to do mail, you need to have a mail server, and then use the SMTP and POP3 protocols for sending and receiving operations.
For instant messaging, You can dive into the socket program on the page, or perform real-time Dynamic Refresh, ajax

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.