JMail is a component for email development. Because the required jar is not officially developed, you need to download the required jar package, I use a javamail-1.4.7.
If you are not familiar with the process of sending and receiving emails, please visit another blog: how email works
General steps:
1. Create a Properties object and set the corresponding content for the object.
2. Create a Session Object
3. Create a Message object that encapsulates mail information, such as the sender, recipient, and topic.
4. Create a Transport object to send emails using this object
In general, there are two steps: Construct the mail object (step 1, only other objects are required when building the mail), send the mail (step 2)
Code:
Package com. zyh. demo; import java. util. properties; import javax. mail. address; import javax. mail. message; import javax. mail. messagingException; import javax. mail. session; import javax. mail. transport; import javax. mail. internet. internetAddress; import javax. mail. internet. mimeMessage; public class Demo1 {public static void main (String [] args) throws MessagingException {Properties props = new Properties (); props. SetProperty ("mail. smtp. auth "," true "); // you need to authenticate props to access the smtp server. setProperty ("mail. transport. protocol "," smtp "); // sets the protocol Session = session for accessing the server. getDefaultInstance (props); session. setDebug (true); // enable the debug function Message msg = new MimeMessage (session); msg. setFrom (new InternetAddress ("xxxxx@163.com"); // set the sender, 163 mailbox requires the sender to be consistent with the login user (required), other mailboxes do not understand msg. setText ("Hello world! "); // Set the message content msg. setSubject ("test"); // set the mail subject Transport trans = session. getTransport (); trans. connect ("smtp.163.com", 25, "your email address (@)", "email password"); // connect to the smtp server of the mailbox, 25 is the default port trans. sendMessage (msg, new Address [] {new InternetAddress ("xxxxx@qq.com")}); // send the email trans. close (); // close the connection }}