springboot+maven sending mail
Nonsense not much to say directly on the code
1. pom File Import jar Package
<!--Mail-- <dependency> <groupId>javax.mail</groupId> <artifactId> mail</artifactid> <version>1.4.7</version> </dependency>
2. Mail method I use 163 mailbox send
/** * Project file Root path * @return */public static String RootPath () { return System.getproperty ("User.dir");}}
//Send mail Public StaticBoolean SendEmail () {FinalProperties props =NewProperties (); //log in to the mailbox server is required to verifyProps.put ("Mail.smtp.auth", "true"); Props.put ("Mail.smtp.host", "smtp.163.com"); Props.put ("Mail.smtp.port", 25); //set up ProtocolsProps.put ("Mail.transport.protocol", "SMTP"); //Sender's account numberProps.put ("Mail.user", "[email protected]"); //The password you need to provide when accessing the SMTP service is very important.Land mailbox password is required to 163 mailbox settings SMTP password Props.put ("Mail.password", "Mima"); //Building authorization information for SMTP authenticationAuthenticator Authenticator =NewAuthenticator () {@Overrideprotectedpasswordauthentication getpasswordauthentication () {//user name, passwordString userName = Props.getproperty ("Mail.user"); String Password= Props.getproperty ("Mail.password"); return Newpasswordauthentication (userName, password); } }; //Create a mail session using environment properties and authorization informationSession mailsession =session.getinstance (props, authenticator);//Mailsession.setdebug (TRUE); //Create a mail messageMimeMessage message =Newmimemessage (mailsession); Try{String Nick= ""; Nick= Javax.mail.internet.MimeUtility.encodeText ("title");//Set SenderInternetAddress from =NewInternetAddress (Nick + "<" +Props.getproperty ("Mail.user") + ">"); Message.setfrom (from); Address[] a=NewAddress[1]; //e-mail address of the receiving party replyA[0] =NewInternetAddress ("[Email protected]"); Message.setreplyto (a); //Set RecipientInternetAddress to =NewInternetAddress ("[Email protected]"); Message.setrecipient (MimeMessage.RecipientType.TO, to); //set the message headerMessage.setsubject ("Mailtitle"); //Add Attachment Section//message Content Section 1---text contentMimeBodyPart body0 =NewMimeBodyPart ();//the text part of the messageBody0.setcontent ("<p> la la la </p>", "Text/html;charset=utf-8"); //message Content Section 2---Annex 1MimeBodyPart body1 =NewMimeBodyPart ();//Annex 1Body1.setdatahandler (NewDataHandler (NewFiledatasource (Ulegalzcutil.rootpath ()+File.separator+ "PDF" + file.separator + "Templateol" + ". pdf")) );//./represents the project root directoryBody1.setfilename (Mimeutility.encodetext ("Lala. pdf") );//Chinese attachment name, solve garbled characters//assemble the top 3 parts together and set them to MSGMimemultipart mm =NewMimemultipart (); Mm.addbodypart (BODY0); Mm.addbodypart (BODY1); Message.setcontent (mm);//set the content body of a message//message.setcontent ("The question when I use postman to upload pictures, have never lived. Show this, what's the problem? "," Text/html;charset=utf-8 "); //Send mailtransport.send (message); } Catch(Exception e) {String err=E.getmessage (); //the message content is processed here, the format is fixedSystem.out.println ("= =:" +err); return false; } return true; }
Note The password is not the login password for the mailbox
Java springboot+maven sending mail