1. Scenario: When the system needs to send the user-related messages, and the user is not on the line, you can take the way to send mail, so that users understand the latest system situation or send verification code and other verification scenarios
2. The experimental environment mainly uses Mail.jar and Activation.jar two packs
3. Use a simple email
Public voidSendMail ()throwsaddressexception, messagingexception{/** 1. Get session*/Properties Properties=NewProperties (); Properties.setproperty ("Mail.host", "smtp.163.com"); Properties.setproperty ("Mail.smtp.auth", "true"); //Account Information TestAuthenticator auth =NewAuthenticator () {@Overrideprotectedpasswordauthentication getpasswordauthentication () {return NewPasswordauthentication ("zhou_s1", "XXX");//here is the account name and password } }; Session Session=session.getinstance (Properties,auth); /** 2. Create messages Message*/MimeMessage Message=NewMimeMessage (session); Message.setfrom (NewInternetAddress ("[email protected]");//Set SenderMessage.setrecipients (recipienttype.to, "[email protected]");//Set Recipient//message.setrecipients (recipienttype.cc, "[email protected]");//set up cc//message.setrecipients (RECIPIENTTYPE.BCC, "[email protected]");//whom to send toMessage.setsubject ("This is a spam--javamail from the Zhou song test."); Message.setcontent ("TEST test test!!!", "Text/html;charset=utf-8"); /** 3. Send*/transport.send (message); }
4. An email with an attachment
Public voidSendmailmore ()throwsaddressexception, messagingexception, ioexception{/** 1. Get session*/Properties Properties=NewProperties (); Properties.setproperty ("Mail.host", "smtp.163.com"); Properties.setproperty ("Mail.smtp.auth", "true"); //Account Information TestAuthenticator auth =NewAuthenticator () {@Overrideprotectedpasswordauthentication getpasswordauthentication () {return NewPasswordauthentication ("zhou_s1", "XXX"); } }; Session Session=session.getinstance (Properties,auth); /** 2. Create messages Message*/MimeMessage Message=NewMimeMessage (session); Message.setfrom (NewInternetAddress ("[email protected]");//Set SenderMessage.setrecipients (recipienttype.to, "[email protected]");//Set RecipientMessage.setsubject ("This is a message with an attachment---test using--javamail"); /******************************************************/ /** Annex Main process attachment contains multiple components referred to as multiple parts **/Mimemultipart List=NewMimemultipart (); //1. Main content of the message MimeBodyPartMimeBodyPart part1 =NewMimeBodyPart (); //1.1. ContentPart1.setcontent ("A test message with attachments", "Text/html;charset=utf-8")); //1.2 Add a principal to a messageList.addbodypart (part1); //2.1. Contents of Email AttachmentsMimeBodyPart Part2 =NewMimeBodyPart (); //2.2. AccessoriesFile File =NewFile ("G:/javatest/merge.mp3"); Part2.attachfile (file); Part2.setfilename (Mimeutility.encodetext ("Merge.mp3")); //2.3 Add a principal to a messageList.addbodypart (part2); //3. Finally add multiple parts to the messagemessage.setcontent (list); //Sendtransport.send (message); }
Test finished!
How Java Mail sends messages