How Java Mail sends messages

Source: Internet
Author: User
Tags auth

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

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.