Java Send Javax.mail message instance explain _java

Source: Internet
Author: User
Tags abstract sessions

Usually we surf the web site, often used to register the site. There are many ways, such as texting verification, email verification. Recently in the SSH framework, made a Simple mail send activation link to the mailbox, and then activate a registered user function. The mailserver I use is the mail server, which refers to the Javax.mail.jar package. JavaMail, as the name suggests, provides a programming interface for developers to work with e-mail. It's a sun-released API for handling emails. It makes it easy to perform some common message transfers.

"Common class Introduction"

Here are some commonly used classes, for you to introduce:

"Authenticator"

This is an abstract class that accesses protected resources through user names and passwords, which are mail servers. After the creation is complete, you must register authenticator with the session. Then, when you need authentication, you will notify authenticator.

"Message"

This is an abstract class used to create messages to send, such as themes, content. You must have a subclass to implement the method.

"Session"

The session class defines a basic mail conversation (sessions), which is the top-level portal class for the Java Mail API. All other classes are valid through this session. The session object obtains information, such as a mail server, user name, password, and other information that is shared throughout the application, using the Java.util.Properties object. This session class represents a message session in JavaMail. Each JavaMail application has at least one session but can have any number of sessions.
Session sendmailsession;
Sendmailsession = session.getinstance (props, null);

"Transport"

The last part of message sending is using the transport class. This class sends messages (usually SMTP) in the language specified by the protocol. It is an abstract class, and it works in a somewhat similar way to the session. Only the static send () method is invoked to use the default version of the class: Transport.send (Message). Alternatively, the reader can obtain a specific instance from a session for his or her own protocol, pass the username and password (if not necessary), send a message, and then close the connection.

"Code Implementation"
Mail Tool class:

Package cn.itcast.shop.utils; 
 
Import java.util.Properties; 
Import Javax.mail.Authenticator; 
Import Javax.mail.Message; 
Import Javax.mail.Message.RecipientType; 
Import javax.mail.MessagingException; 
Import javax.mail.PasswordAuthentication; 
Import javax.mail.Session; 
Import Javax.mail.Transport; 
Import javax.mail.internet.AddressException; 
Import javax.mail.internet.InternetAddress; 
 
Import Javax.mail.internet.MimeMessage; /** * Mail Delivery tool class * @author Zhouzhou * @date 2016-1-1 * @time Morning 08:52:47/public class Mailuitls {public stat IC void SendMail (String to,string code) {/** * 1. Get session * 2. To create an object message for code Mail * 3. Send mail tra 
    Nsport//** * 1. Get Connected Object/Properties Props=new properties (); 
    Props.setproperty ("Mail.host", "localhost"); Session session=session.getdefaultinstance (Props, New Authenticator () {@Override protected passwordauthent 
      Ication getpasswordauthentication () {  TODO auto-generated method stubs return new Passwordauthentication ("Service@azz.com", "111"); 
    } 
       
    }); 
    2. Create mail Send object messages message=new MimeMessage (session); 
      3. Set sender try {message.setfrom (new internetaddress ("service@azz.com")); 
      Set recipient Message.addrecipient (Recipienttype.to, New internetaddress (to)); 
      Title Message.setsubject ("Official activation mail from the Zhou Mall"); Message.setcontent (" 

The

User's business layer calls this class when registering, using the SendMail method.

Package cn.itcast.shop.user.service; 
Import org.springframework.transaction.annotation.Transactional; 
Import Cn.itcast.shop.user.dao.UserDao; 
Import Cn.itcast.shop.user.vo.User; 
Import Cn.itcast.shop.utils.MailUitls; 
 
Import Cn.itcast.shop.utils.UUIDUtils; 
 
  Business Layer @Transactional public class UserService {//installation user Name query user's method private Userdao Userdao; 
  public void Setuserdao (Userdao userdao) {This.userdao = Userdao; 
  Public User Findbyusername (String username) {return userdao.findbyusername (username); //Business layer: Completing user registration public void Save (user user) {//TODO auto-generated method Stub//storing data in a database user.se 
    Tstate (0);//1 has been activated, 0 has not activated String Code=uuidutils.getuuid () +uuidutils.getuuid (); 
    User.setcode (code); 
    Userdao.save (user); 
    send activation message; 
  Mailuitls.sendmail (User.getemail (), code); 
  The//Business layer queries the user's public user Findbycode (String code) {return Userdao.findbycode (code) based on the activation Code; }//Modify user's state method publicvoid Update (User existuser) {userdao.update (existuser); 
  }//user logon method public user login (user user) {return userdao.login (user);  }   
}

Finally, this effect is achieved:

The above is the entire content of this article, I hope to help you learn.

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.