Java uses JXL to automatically export data Excle,quartz automatically send messages

Source: Internet
Author: User

=============java Background Code =====================

Package COM.QGC.SERVICE.AUTOSENDMSG.AUTOSENDMSG

Import Java.io.File;
Import Java.text.SimpleDateFormat;
Import Java.util.Date;
Import Java.util.HashMap;
Import java.util.List;
Import Java.util.Map;
Import java.util.Properties;

Import Javax.activation.DataHandler;
Import Javax.activation.DataSource;
Import Javax.activation.FileDataSource;
Import Javax.mail.BodyPart;
Import Javax.mail.Message;
Import Javax.mail.Multipart;
Import javax.mail.Session;
Import Javax.mail.Transport;
Import javax.mail.internet.InternetAddress;
Import Javax.mail.internet.MimeBodyPart;
Import Javax.mail.internet.MimeMessage;
Import Javax.mail.internet.MimeMultipart;

Import Com.gzbugu.common.service.impl.BaseService;
Import Com.gzbugu.common.util.JxlsUtils;
Import Com.gzbugu.dzz.domain.DzzUserinfo;

/**
* Automatically export data and send mail to users
* @author http://cnblogs.com/qgc88
*
*/
public class Autosendmsg {
Private String host = ""; SMTP server
Private String from = ""; Sender Address
Private String to = ""; Recipient address
Private String affix = ""; Attachment Address
Private String Affixname = ""; Attachment Name
Private String user = ""; User name
Private String pwd = ""; Password
Private String subject = ""; Message header
Private String count= "";//File contents

public void SendEmail () {
try {


String hql = "from Dzzuserinfo o where enabled = 1";
list<dzzuserinfo> list = COMMONDAO.GETLISTBYHQL (HQL);
SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd hhmmss");

String Bathpath = This.getclass (). getClassLoader (). GetResource ("")
. GetPath ();
Bathpath=bathpath.substring (1,bathpath.length ());

Bathpath=bathpath.substring (1,bathpath.lastindexof ("Web-inf")) + "Dzz" + "/neworderdata/";
File File=new file (Bathpath);
if (!file.exists ()) {
File.mkdirs ();
}

String Flilename=sdf.format (New Date ()) + ". xls";
String TempPath =bathpath+flilename;
String template =bathpath+ "Neworder_template.xls";

Export using Jxls template mode
MAP params = new HashMap ();
Params.put ("list", list);
Jxlsutils.createexcel (template, params, temppath);
System.out.println (TempPath);
Set sender address, recipient address, and message header
Setaddress ("Sender mailbox @qq.com", "Recipient mailbox @qq.com", flilename+ ", college students latest information! ");
Set the location and title of the attachment to send
Setaffix (Temppath,flilename);

Count= "College students the latest information, please find the details of the annex, thank you! ";

Set the SMTP server and the account and password for the mailbox
Send ("Sender mailbox @qq.com", "Sender's email password");
} catch (Exception e) {
E.printstacktrace ();
}

}



public void Send (String user,string pwd) {
This.user = user;
This.pwd = pwd;

This.host = "SMTP."
+ from.substring (from.indexof (' @ ') + 1, from.length ()); Send on the Internet

Properties Props = new properties ();

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 verification (must have this)
Props.put ("Mail.smtp.auth", "true");

Build a session with the props object you just set up
Session 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 parameters
MimeMessage message = new MimeMessage (session);
try{
Load Sender Address
Message.setfrom (New InternetAddress (from));
Load recipient Address
Message.addrecipient (Message.recipienttype.to,new internetaddress (to));
Load title
Message.setsubject (subject);

Add various parts of the message to the multipart object, including text content and attachments
Multipart Multipart = new Mimemultipart ();


Set the text content of a message
BodyPart Contentpart = new MimeBodyPart ();
Contentpart.settext (count);
Multipart.addbodypart (Contentpart);
Add an attachment
BodyPart messagebodypart= new MimeBodyPart ();
DataSource Source = new Filedatasource (affix);
Add the contents of an attachment
Messagebodypart.setdatahandler (new DataHandler (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 sent
Sun.misc.BASE64Encoder enc = new Sun.misc.BASE64Encoder ();
Messagebodypart.setfilename ("=? GBK? B? " +enc.encode (Affixname.getbytes ()) + "? =");
Multipart.addbodypart (Messagebodypart);


Place the multipart object in the message
Message.setcontent (multipart);
Save Message
Message.savechanges ();
Send mail
Transport Transport = Session.gettransport ("SMTP");
Connect to the server's mailbox
Transport.connect (host, user, PWD);
Send the Mail out
Transport.sendmessage (Message, message.getallrecipients ());
Transport.close ();
}catch (Exception e) {
E.printstacktrace ();
}
}


public void setaddress (String from,string to,string subject) {
This.from = from;
This.to = to;
This.subject = subject;
}

public void Setaffix (String affix,string affixname) {
This.affix = affix;
This.affixname = Affixname;
}

}

===============application.xml Timing Configuration =================================

<?xml version= "1.0" encoding= "UTF-8"?>
<beans xmlns= "Http://www.springframework.org/schema/beans"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:tx= "Http://www.springframework.org/schema/tx"
xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xmlns:context= "Http://www.springframework.org/schema/context"
xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.0.xsd
Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "
Default-autowire= "ByName" default-lazy-init= "true" >
<!--a Normal object--
<bean id= "autosendmsg" class= "COM.QGC.SERVICE.AUTOSENDMSG.AUTOSENDMSG"/>

<bean id= "Autosendmsgtaskinit" class= " Org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean ">
<!--object Set to timed call--
<property name= "TargetObject" >
<ref local= "Autosendmsg"/>
</property>
<!--set one of the methods in the object to work in a timed way--
<property name= "Targetmethod" >
<value>sendEmail</value>
</property>
</bean>
<bean id= "Autosendmsgtrigger" class= "Org.springframework.scheduling.quartz.CronTriggerBean" >
<!--to add timer objects to timers--
<property name= "Jobdetail" >
<ref local= "Autosendmsgtaskinit"/>
</property>
<!--set time call regulation--
<property name= "Cronexpression" >
<!--0 15 10? * * 23 points per day 140 minutes---
<value>0 40 23? * * </value>
</property>
</bean>

<bean id= "Autosendmsgscheduler" class= "Org.springframework.scheduling.quartz.SchedulerFactoryBean" >
<!--add timers to the manager--
<property name= "Triggers" >
<list>
<ref bean= "Autosendmsgtrigger"/>
</list>
</property>
</bean>


</beans>

===================neworder_template.xls Export Template ==================================

Java uses JXL to automatically export data Excle,quartz automatically send 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.