In the specific system management, often there will be a few times to send users some mail business, now refer to the most of the online code, write down my own code.
The contents of the Applicationcontext.xml are as follows:
<?xml version= "1.0" encoding= "UTF-8"?><Beans xmlns= "Http://www.springframework.org/schema/beans"XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"Xmlns:tx= "Http://www.springframework.org/schema/tx"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xmlns:p= "http://www.springframework.org/schema/p"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsdhttp//WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOPhttp//www.springframework.org/schema/aop/spring-aop-3.1.xsdhttp//Www.springframework.org/schema/txhttp//www.springframework.org/schema/tx/spring-tx-3.1.xsd "><bean id= "Testquartz"class= "Com.zhuyun.net.AttachmentEmailUtil" ></bean> <!--Bean Trigger method configuration-<bean name= "Quartzbean"class= "Org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean" > <!--Bean name---<PR Operty name= "TargetObject" ref= "Testquartz"/><!--target Object--<!--Bean Method--<property Name= " Targetmethod "><!--target Method--<value>sendEmail</value> </property> <pro Perty name= "Concurrent" ><!--configured to False does not allow tasks to execute concurrently--<value>false</value> </property> </bean> <!--Bean trigger time configuration, specifying the specific time to execute--<bean id= "q Uartztrigger "class= "Org.springframework.scheduling.quartz.CronTriggerBean" > <!--Trigger Bean configuration--<property NA Me= "Jobdetail" ref= "Quartzbean"/> <!--Trigger Time configuration-<property name= "Cronexpression" > <value>0 0 1 *?</value> </property> </bean> <beanclass= "Org.springframework.scheduling.quartz.SchedulerFactoryBean" lazy-init= "Default" autowire= "Default" > < Property name= "Triggers" > <list> <ref local= "Quartztrigger"/> </list> &l t;/property> <property name= "Autostartup" value= "true"/></bean></beans>
The method is as follows:
Packagecom.zhuyun.net;ImportJava.io.BufferedWriter;ImportJava.io.File;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;ImportJava.io.FileWriter;Importjava.io.IOException;ImportJava.io.ObjectInputStream;ImportJava.io.ObjectOutputStream;ImportJava.io.OutputStream;ImportJava.io.OutputStreamWriter;ImportJava.io.Writer;Importjava.sql.SQLException;ImportJava.text.SimpleDateFormat;Importjava.util.ArrayList;ImportJava.util.Calendar;Importjava.util.Date;Importjava.util.List;Importjava.util.Properties;ImportJavax.activation.DataHandler;ImportJavax.activation.DataSource;ImportJavax.activation.FileDataSource;ImportJavax.mail.BodyPart;ImportJavax.mail.Message;ImportJavax.mail.Multipart;Importjavax.mail.Session;ImportJavax.mail.Transport;Importjavax.mail.internet.InternetAddress;ImportJavax.mail.internet.MimeBodyPart;ImportJavax.mail.internet.MimeMessage;ImportJavax.mail.internet.MimeMultipart; Public classAttachmentemailutil {PrivateString host = "";//SMTP Server PrivateString from = "";//Sender Address PrivateString to = "";//Recipient Address PrivateString affix = "";//Attachment Address PrivateString affixname = "";//Attachment name PrivateString user = "";//User name PrivateString pwd = "";//Password PrivateString subject = "";//message Header Public voidsetaddress (string from, string to, string subject) { This. from =From ; This. to =to ; This. Subject =subject; } Public voidSetaffix (String affix, string affixname) { This. Affix =affix; This. Affixname =Affixname; } Public voidSend (string host, String user, string pwd) { This. Host =host; This. user =user; This. PWD =pwd; Properties Props=NewProperties (); //set the properties of the mail server to send mail (use NetEase's SMTP server here)Props.put ("Mail.smtp.host", host); //need to be authorized, that is, the user name and password verification, so as to pass the verificationProps.put ("Mail.smtp.auth", "true"); Props.put ("Mail.smtp.port", 465); Props.put ("Mail.smtp.ssl.enable",true); //build a session with the props object you just set upSession session =session.getdefaultinstance (props); //with this sentence you can display the process information at the console during the sending of the message for debugging//(You can see the process of sending the message in the console)Session.setdebug (true); //defining a Message object with session parametersMimeMessage message =NewMimeMessage (session); Try { //Load Sender AddressMessage.setfrom (Newinternetaddress (from)); //Load recipient addressMessage.addrecipient (Message.RecipientType.TO,Newinternetaddress (to)); //Load TitleMessage.setsubject (subject); //add various parts of the message to the multipart object, including text content and attachmentsMultipart Multipart =NewMimemultipart (); //set the text content of a messageBodyPart Contentpart =NewMimeBodyPart (); Contentpart.settext ("The second method ..."); Multipart.addbodypart (Contentpart); //Add an attachmentBodyPart Messagebodypart =NewMimeBodyPart (); DataSource Source=NewFiledatasource (affix); //Add the contents of an attachmentMessagebodypart.setdatahandler (NewDataHandler (source)); //Add a caption for an attachment//It is important to use the following BASE64 encoded conversion to ensure that your Chinese attachment header name is not garbled when it is sentSun.misc.BASE64Encoder enc =NewSun.misc.BASE64Encoder (); Messagebodypart.setfilename ("=? GBK? B? " + Enc.encode (affixname.getbytes ()) + "? ="); Multipart.addbodypart (Messagebodypart); //Place the multipart object in the messagemessage.setcontent (multipart); //Save Messagemessage.savechanges (); //Send mailTransport Transport = Session.gettransport ("SMTP"); //connect to the server's mailboxTransport.connect (host, user, PWD); //Send the Mail outtransport.sendmessage (Message, message.getallrecipients ()); Transport.close (); } Catch(Exception e) {e.printstacktrace (); } } Public voidSendEmail () {File file=NewFile ("/root/emailfile/content.csv"); if(!file.exists ()) { Try{file.createnewfile (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }} String pushinfocontent= "Succeeded"; FileWriter writer=NULL; Try { //open a write file, the second parameter in the constructor true indicates that the file is written in append formwriter =NewFileWriter (file,true); Writer.write (Pushinfocontent+ "\ r \ n"); } Catch(IOException e) {e.printstacktrace (); } finally { Try { if(Writer! =NULL) {writer.close (); } } Catch(IOException e) {e.printstacktrace (); }} Attachmentemailutil cn=NewAttachmentemailutil (); //set sender address, recipient address, and message headerCn.setaddress ("Sender Address", "Recipient Address", "one JavaMail message with attachments (title)"); //set the location and title of the attachment to sendCn.setaffix ("Location of Attachment", "File name of attachment"); //set the SMTP server and the account and password for the mailboxCn.send ("smtp.qq.com", "Account", "password"); if(File.exists ()) {file.delete (); } }}
That's probably it.
Spring + Quartz To implement timed send mail function