Today, I suddenly wanted to add the email sending function to my current project and found some information on the Internet. I was puzzled about the results. The sender address frequently reported errors during the mail sending process, no verification code exists. Just think about the actual situation. It is impossible for me to send the code by writing any sender at ordinary times. Later, I found some information about Java verification on the Internet. Unfortunately, the version may be upgraded too quickly and these methods cannot be used in javamail 1.4, so I went to the official API documentation and found this method.
public static Session getInstance(java.util.Properties props, Authenticator authenticator)
-
Get a new session object.
Every time you create a session, we should enter the verification information here. Let's take a look at the second parameter of this method.
Public abstract classAuthenticatorExtends java. Lang. Object
This class is an abstract class, but the inheritance class of this class is not provided in the API documentation. OK. Let's inherit it by ourselves.
Static class smtpauth extends javax. Mail. authenticator {
Private string user, password;
Public void getuserinfo (string getuser, string GetPassword ){
User = getuser;
Password = GetPassword;
}
Protected javax. Mail. passwordauthentication getpasswordauthentication (){
Return new javax. Mail. passwordauthentication (user, password );
}
}
For my convenience, I set smtpauth to a static internal class. Let's look at this method of authenticator.
protected PasswordAuthentication |
getPasswordAuthentication() Called when password authentication is needed. |
This method is called when password verification is required.Passwordauthentication class.Find out his Constructor
Passwordauthentication(Java. Lang. String username, java. Lang. String password)
Initialize a new passwordauthentication
So we can design the username and password members .. The verification details are clear! Below is a simple design
Import java. util. date;
Import java. util. properties;
Import javax. Mail. message;
Import javax. Mail. messagingexception;
Import javax. Mail. Session;
Import javax. Mail. Transport;
Import javax. Mail. Internet. addressexception;
Import javax. Mail. Internet. internetaddress;
Import javax. Mail. Internet. mimemessage;
Public class mail {
Static class smtpauth extends javax. Mail. authenticator {
Private string user, password;
Public void getuserinfo (string getuser, string GetPassword ){
User = getuser;
Password = GetPassword;
}
Protected javax. Mail. passwordauthentication getpasswordauthentication (){
Return new javax. Mail. passwordauthentication (user, password );
}
}
/**
* @ Param ARGs
* @ Throws messagingexception
* @ Throws addressexception
*/
@ Suppresswarnings ("static-access ")
Public static void main (string [] ARGs) throws addressexception, messagingexception {
// Todo automatically generates method stubs
Properties props = new properties ();
Session sendmailsession;
Transport transport;
Smtpauth SA = new smtpauth ();
SA. getuserinfo ("tw", "1234 ");
Sendmailsession = session. getdefaultinstance (props, SA );
Transport = sendmailsession. gettransport ("SMTP ");
Transport. Connect ("134.98.83.32", "tw", "1234 ");
Props. Put ("mail. SMTP. Host", "134.98.83.32 ");
Props. Put ("mail. SMTP. Auth", "true ");
Message newmessage = new mimemessage (sendmailsession );
Newmessage. setfrom (New internetaddress ("tw@zjdw.com "));
Newmessage. setrecipient (message. recipienttype. To, new internetaddress ("tw@zjdw.com "));
Newmessage. setsubject ("Hello World ");
Newmessage. setsentdate (new date ());
Newmessage. settext ("this is a test example ");
Transport. Send (newmessage );
}
}
OK. Verification Successful!