The effect is the same as that of the SMTP protocol in the previous two days. However, it is much more convenient and easier to use javamail.
To run this program, two Java packages are required: mail. jar and activation. Jar. These two packages are easy to run, so I will not talk about them here.
The core classes of javamail APIs used in this program include Session, message, address, verification program, and transmission.
The functions are as follows:
Session
The session class defines a basic mail session. This session allows other tasks to run smoothly. The Session object uses the java. util. properties object to obtain information such as the email server, user name, password, and other information that can be shared throughout the application.
Message class
Create the message to be sent.
Address class
Once a session and message are created and the content is filled with the message, you need to use the Address class to mark the address for your mail.
Authenticator class
Like the java.net class, the javamail API can use the authenticator class to access protected resources through the user name and password.
Transport class
The last step to send a message is to use the transport class. This class uses a language specific to the Protocol (usually SMTP) to send messages.
Javamail. Java
-------------------------------------------------------
Package mail;
Import java. util .*;
Import javax. Mail. address;
Import javax. Mail. authenticator;
Import javax. Mail. passwordauthentication;
Import javax. Mail. Session;
Import javax. Mail. Transport;
Import javax. Mail. Internet. internetaddress;
Import javax. Mail. Internet .*;
Import javax. Mail .*;
Public class javamail {
String Server = "smtp.126.com"; // SMTP Server
String to = "wasingmon@eyou.com"; // recipient address
String from = "wasingmon@126.com"; // sender address
String user = "wasingmon"; // Login Server Username
String Password = ""; // Password
String subject = "2222222 ";
String content = "1111111 ";
Boolean auth = true;
Boolean DEBUG = true;
Public static void main (string [] ARGs ){
Javamail sendmessage = new javamail ();
Properties props = new properties ();
Props. Put ("mail. SMTP. Host", sendmessage. Server );
Props. Put ("mail. SMTP. Auth", String. valueof (sendmessage. Auth); // authentication required
Props. Put ("mail. dubug", String. valueof (sendmessage. Debug); // display debugging information for debugging convenience
Popupauthenticator auth = new popupauthenticator (sendmessage. User, sendmessage. Password );
Try {
Session session = session. getinstance (props, auth); // create a session
Session. setdebug (true );
Mimemessage message = new mimemessage (session); // create a message
Address addressto = new internetaddress (sendmessage. To, "wxm1"); // create an address object
Address addressfrom = new internetaddress (sendmessage. From, "wxm2 ");
Message. setcontent (sendmessage. content, "text/plain"); // you can specify the content of a message.
Message. setsubject (sendmessage. Subject );
Message. setfrom (addressfrom );
Message. addrecipient (message. recipienttype. To, addressto );
Message. savechanges ();
Transport transport = session. gettransport ("SMTP"); // transfer object
Transport. Connect (sendmessage. server, 25, sendmessage. User, sendmessage. Password );
Transport. Send (Message );
Transport. Close ();
System. Out. println ("succ! ");
} Catch (exception e ){
E. printstacktrace ();
System. Out. println ("failed to send! ");
}
}
}
Class popupauthenticator extends authenticator {
Private string username, password;
Public popupauthenticator (string username, string password ){
This. Password = password;
This. Username = username;
}
Protected passwordauthentication getpasswordauthentication (){
Return new passwordauthentication (username, password );
}
}