Java implementation Registration Mailbox activation verification

Source: Internet
Author: User

Mail delivery Servelet Implementation

Package com.xbs.register.main;

Import java.io.IOException;
Import Java.util.Date;
Import java.util.Properties;

Import javax.mail.Address;
Import Javax.mail.Authenticator;
Import Javax.mail.Message.RecipientType;
Import javax.mail.MessagingException;
Import Javax.mail.Transport;
Import javax.mail.internet.InternetAddress;
Import Javax.mail.internet.MimeMessage;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
/**
*
*/
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
Import javax.servlet.http.HttpSession;

/**
* @author James
* The servlet implementation of sending mail
*/
public class Registerservlet extends HttpServlet {

protected void Doget (HttpServletRequest req, HttpServletResponse resp)
Throws Servletexception, IOException {
TODO auto-generated Method Stub
This.dopost (req, resp);
}

protected void DoPost (HttpServletRequest req, HttpServletResponse resp)
Throws Servletexception, IOException {

Resp.setcontenttype ("text/html");

Coding
Req.setcharacterencoding ("Utf-8");
Resp.setcharacterencoding ("Utf-8");

String Tomail = req.getparameter ("email");
String registername = Req.getparameter ("UserName");

String userName = "[email protected]";
String password = "wjw6993469";

String Registerid = "" + math.random () * Math.random ();
String url = "http://localhost:8080/register/MailBackServlet?registerId="
+ registerid+ "&registername=" +registername;//click on the link in the mailbox to return to your website.

HttpSession HttpSession = Req.getsession ();
Httpsession.setattribute (Registerid, registername);
Number of seconds
Httpsession.setmaxinactiveinterval (1000);
Httpsession.setattribute ("2", registername);

/*
* SYSTEM.OUT.PRINTLN ("------" +registerid); String str = (string)
* Httpsession.getattribute (Registerid);
* SYSTEM.OUT.PRINTLN ("----------------" +str);
* SYSTEM.OUT.PRINTLN (Registername);
*/

Set up information such as authentication, server, etc.
Properties Props = new properties ();
Props.setproperty ("Mail.smtp.host", "smtp.qq.com");
Props.setproperty ("Mail.smtp.auth", "true");
Props.setproperty ("Mail.store.protocol", "SMTP");
Authenticator Authenticator = new Myauthenticator (userName, password);

Javax.mail.Session Session = javax.mail.Session.getDefaultInstance (
props, authenticator);
Session.setdebug (TRUE);

try {
Address from = new InternetAddress (userName);
Address to = new InternetAddress (tomail);

MimeMessage msg = new MimeMessage (session);
Msg.setfrom (from);
Msg.setsubject ("Honest website Registration");
Msg.setsentdate (New Date ());
Msg.setcontent ("<a href=" + URL + "' > click" + URL + "Complete registration </a>",
"Text/html;charset=utf-8");
Msg.setrecipient (recipienttype.to, to);
/*
* Transport Transport = session.gettransport ("SMTP");
* Transport.connect ("smtp.163.com", userName, password);
* Transport.sendmessage (Msg,msg.getallrecipients ());
* Transport.close ();
*/
Transport.send (msg);
} catch (Messagingexception e) {
E.printstacktrace ();
}

Req.getrequestdispatcher ("/sendmailsuccess.jsp"). Forward (req, resp);
}
}

/**
* Authorized user name and password class for the mailbox that sent the message
*/
Package com.xbs.register.main;

Import Javax.mail.Authenticator;
Import javax.mail.PasswordAuthentication;

/**
* @author James
*
*/
public class Myauthenticator extends Authenticator {
Private String UserName;
private String password;

Public Myauthenticator (String userName, string password) {
This.username = UserName;
This.password = password;
}

@Override
Protected Passwordauthentication getpasswordauthentication () {
return new Passwordauthentication (userName, password);
}
}

Simple Sign-up page

register.jsp

<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%>

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
<title> user registration using a mailbox for verification </title>
<body>
<br/>
<form action= "${pagecontext.request.contextpath}/registerservlet" method= "POST" >
<font size= "color=" "Red" > User registration </font><br/>
Email: <input type= "text" name= "email"/><br/>
Nickname: <input type= "text" name= "UserName"/><br/>
<input type= "Submit" value= "Submit"/><br/>
</form>
</body>

The required JAR report. Mail.jar and Servlet.jar

http://www.2cto.com/kf/201312/268157.html Example

Java implementation Registration Mailbox activation verification

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.