JavaMail Learning (send with attachments) and javamail attachment
I used to think that JavaMail was a very big tool, so I found JavaMail on the Internet. In fact, I didn't think it was so tall. I just learned the mail part, then I will paste my own code. The specific implementation code is explained in detail.
The first is to send a common email.
// Send the common mail public static void sendTextmail () throws MessagingException {// step 1. obtain the Session (note: This is javax. mail class)/** public static Session getInstance (java. util. properties props, Authenticator authenticator) ** you must specify two key values for props. One is to specify the server host name, and the other is to specify whether authentication is required! When this parameter is set, you must verify that true * authenticator is an interface that indicates the authenticator, that is, the client's identity login. We need to implement this interface by ourselves. To implement this interface, we need to use the account and password */Properties props = new Properties (); props. setProperty ("mail. host "," smtp.sohu.com "); props. setProperty ("mail. smtp. auth "," true "); Authenticator auth = new Authenticator () {@ Override protected PasswordAuthentication getPasswordAuthentication () {return new PasswordAuthentication (" lishun1005 "," login ");}}; session session = Session. getInstance (props, auth); // Step 2: Create the MimeMessage object MimeMessage msg = new MimeMessage (session); msg. setFrom (new InternetAddress ("lishun1005@sohu.com"); // sets the sender msg. setRecipients (RecipientType. TO, "lishun1005@sohu.com"); // send, can be sent TO multiple people, equivalent TO sending msg. setRecipients (RecipientType. BCC, "lishun1005@sohu.com"); // send (send to only one person) msg. setRecipients (RecipientType. CC, "lishun1005@sohu.com"); // CC: msg. setSubject ("send to Wang Nima"); // set the mail title; msg. setContent ("common mail", "text/plan; charset = UTF-8"); // sets the encoding method for the body and body. // sends the mail Transport. send (msg );}
The second is sending with attachments.
// Send the public static void sendTextAndFilemail () throws MessagingException, IOException {Properties props = new Properties (); props. setProperty ("mail. host "," smtp.sohu.com "); props. setProperty ("mail. smtp. auth "," true "); Authenticator auth = new Authenticator () {@ Override protected PasswordAuthentication getPasswordAuthentication () {return new PasswordAuthentication (" lishun1005 "," leason841553 484 ") ;}}; Session session = Session. getInstance (props, auth); MimeMessage msg = new MimeMessage (session); msg. setFrom (new InternetAddress ("lishun1005@sohu.com"); // sets the sender msg. setRecipients (RecipientType. TO, "lishun1005@sohu.com"); msg. setSubject ("send to Wang Nima");/** when sending an email containing attachments, the email body is in the form of multiple parts! 1. Create a multi-part content! MimeMultipart * MimeMultipart is a set used to load multiple main parts! 2. We need to create two main parts: Text Content and attachment content. * The main part is MimeBodyPart. 3. Set MimeMultipart to MimeMessage! This section describes the learning materials found on the Internet. If you are interested, you can try again. */MimeMultipart list = new MimeMultipart (); // create multi-part content // create MimeBodyPart part1 = new MimeBodyPart (); // set the text content part1.setContent ("emails with attachments", "text/html; charset = UTF-8 "); // Add the text to the list in the set. addBodyPart (part1); // create MimeBodyPart part2 = new MimeBodyPart (); part2.attachFile (new File ("D:/Wang Nima .jpg ")); // set the attachment content part2.setFileName (MimeUtility. encodeText (" Wang Nima .jpg "); // set the name of the displayed file, in which encodeText is used to handle Chinese garbled list. addBodyPart (part2); msg. setContent (list); // set it to the mail as the mail content. // Send the email Transport. send (msg );}