In the project to use the regular mail to send, collects the feeling on the net to use the spring and the quartz realization is relatively simple.
A base class for sending messages:
Package com.spring.mail;
Import Org.springframework.mail.MailSender;
Import Org.springframework.mail.javamail.JavaMailSender;
Public abstract class Basemailsender {
protected String to;
protected String from;
protected String subject;
protected MailSender Sender;
Use MailSender when sending a message as a simple message;
protected Javamailsender Sender;
Use Javamailsender when sending a message for Mimi
public void Setto (String to) {
This.to = to;
}
public void Setfrom (String from) {
This.from = from;
}
public void Setsubject (String subject) {
This.subject = subject;
}
public void Setjavamailsender (MailSender sender) {//(Javamailsender sender) {
This.sender = sender;
}
}
class to send message calls:
Package com.spring.mail;
Import javax.mail.MessagingException;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;
Import Org.springframework.mail.SimpleMailMessage;
public class Simplehtmlmailsender extends basemailsender{
public void SendMessage () throws Messagingexception {
String textstr = "Helo!!!!!!!!!!!"; /Send the message body of the message
Simplemailmessage msg = new Simplemailmessage ();
Msg.setto (to);
Msg.setfrom (from);
Msg.setsubject (subject);
Msg.settext (TEXTSTR);
Sender.send (msg);
}
public void DoIt () throws Exception {
Classpathxmlapplicationcontext ctx1 = new Classpathxmlapplicationcontext (
New string[] {"Applicationcontext.xml"});
Simplehtmlmailsender sender = (simplehtmlmailsender) ctx1.getbean ("Messagesender");
Sender.sendmessage ();
SYSTEM.OUT.PRINTLN ("Send Success");
}
}
Configuration file ApplicationContext:
<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE beans Public "-//spring//dtd bean//en" "Http://www.springframework.org/dtd/spring-beans.dtd" >
<beans>
<bean id= "Sender" class= "Org.springframework.mail.javamail.JavaMailSenderImpl" >
<property name= "Host" >
<value>smtp.163.com</value>
</property>
<property name= "username" >
<value>tomcat0506</value>
</property>
<property name= "Password" >
<value>wes</value>
</property>
<property name= "Javamailproperties" >
<props>
<prop key= "Mail.smtp.auth" >true</prop>
</props>
</property>
</bean>
<bean id= "Messagesender" class= "Com.spring.mail.SimpleHtmlMailSender" >
<property name= "Javamailsender" >
<ref bean= "Sender"/>
</property>
<property name= "to" >
<value>wes830506@126.com</value>
</property>
<property name= "from" >
<value>tomcat0506@163.com</value>
</property>
<property name= "Subject" >
<value>test</value>
</property>
</bean>
<bean id= "Methodinvokingjobdetail" class= " Org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean ">
<!--<property name= "Jobclass" >
<value>com.spring.mail.SimpleHtmlMailSender</value>
</property>-->
<property name= "TargetObject" ><ref bean= "Messagesender"/></property>
<property name= "Targetmethod" ><value>doIt</value></property>
</bean>
<bean id= "Simpletrigger" class= "Org.springframework.scheduling.quartz.SimpleTriggerBean"
<property name= "Jobdetail" ><ref bean= "Methodinvokingjobdetail"/></PROPERTY>
<property name= "Startdelay"
<!--3 seconds;
<value>3000</value>
</property>
<property name= " Repeatinterval "
<!--repeat every 6 seconds;
<value> 6000</value>
</property>
</bean>
<bean id= "Crontrigger" class= "Org.springframework.scheduling.quartz.CronTriggerBean" >
<property name= "Jobdetail" > <ref bean= "Methodinvokingjobdetail"/></property>
<property name= "cronexpression" > <value>0 0 6,12,20 * *?</value> </property>
</bean>
<bean class= "Org.springframework.scheduling.quartz.SchedulerFactoryBean" >
<property name= "Triggers" >
<list>
<ref local= "Simpletrigger"/>
</list>
</property>
</bean>
</beans>
two Mimi Mail sent
Javax.mail.MessagingException;
Javax.mail.internet.MimeMessage;
Java EE the classes that support the need to send MIME information
Example:
Base class for sending messages
Basemailsender. Java:
Import Org.springframework.mail.javamail.JavaMailSender;
Public abstract class Basemailsender {
protected String to;
protected String from;
protected String subject;
Protected Javamailsender sender;//The class that is used when sending Mimi messages
public void Setto (String to) {
This.to = to;
}
public void Setfrom (String from) {
This.from = from;
}
public void Setsubject (String subject) {
This.subject = subject;
}
public void Setjavamailsender (Javamailsender sender) {
This.sender = sender;
}
}
class to send mail:
Package com.spring.mail;
Import javax.mail.MessagingException;
Import Javax.mail.internet.MimeMessage;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;
Import Org.springframework.mail.SimpleMailMessage;
Import Org.springframework.mail.javamail.MimeMessageHelper;
public class Simplehtmlmailsender extends basemailsender{
public void SendMessage () throws messagingexception {
//string htmlhead = "
//string htmlBody = "";
//string htmlend = "";
// stringbuffer mailmessage = new StringBuffer ();
//mailmessage.append (HtmlHead);
//mailmessage.append (htmlBody);
//mailmessage.append (htmlend);
//Log off this sentence when the message sent is a simple message;
string textstr = "
//string textstr = "Helo could you to this email?"; /Send message principal
//simplemailmessage msg = new Simplemailmessage ();
&NBSP;&NBSP
//When sending a message as a simple message
mimemessage msg = Sender.createmimemessage ();
mimemessagehelper helper = new Mimemessagehelper (msg, True, "GB2312");
helper.setto (to);
helper.setfrom (from);
helper.setsubject (subject);
helper.settext (Textstr, true);
/*
msg.setto (to);
msg.setfrom (from);
msg.setsubject (subject);
msg.settext (TEXTSTR);
*/
sender.send (msg);
.}
public void DoIt () throws exception{//static void Main (string[] args) throws Exception {//
ApplicationContext CTX = new Filesystemxmlapplicationcontext (new string[] {"Springexample-creditaccount.xml"});
Classpathxmlapplicationcontext CTX = new Classpathxmlapplicationcontext (
New string[] {"Applicationcontext.xml"});
Simplehtmlmailsender sender = (simplehtmlmailsender) ctx
. Getbean ("Messagesender");
Sender.sendmessage ();
SYSTEM.OUT.PRINTLN ("Send Success");
}
}
Config file ditto!
A few classes to use when writing a mail program:
org.springframework.mail.javamail.javamailsender;
//This class inherits the
//mailsender interface in spring--defines two simple sender methods for sending simple message messages with the following parameter types: Simplemailmessage ([])
//javamailsender class inherits the above method and supports Mimi messages, you can send Mimi messages
Org.springframework.context.support.ClassPathXmlApplicationContext;
The class to use when reading the configuration file inside the //spring
org.springframework.mail.javamail.mimemessagehelper;
//the auxiliary class to be used when sending Mimi messages;