- 浏览: 271036 次
- 性别:
- 来自: 天津
文章分类
- 全部博客 (183)
- oracle (4)
- informix (1)
- web开发 (6)
- java (49)
- hibernate (1)
- hadoop (1)
- spring (23)
- 非技术 (8)
- ibatis2 (5)
- Linux (6)
- tomcat (14)
- nginx (7)
- dubbo (3)
- myibatis (7)
- webservice 开发 (2)
- mysql (2)
- svn (2)
- redis (7)
- 分布式技术 (17)
- zookeeper (2)
- kafka (2)
- velocity (1)
- maven (7)
- js (1)
- freemarker (1)
- Thymeleaf (3)
- 代码审计 (1)
- ibatis3 (1)
- rabbitmq (1)
最新评论
1.使用maven下载jar
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.boce</groupId>
<artifactId>boce_web</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>boce_web Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>3.2.18.RELEASE</spring.version>
<slf4j-api.version>1.7.5</slf4j-api.version>
<logback.version>1.0.13</logback.version>
</properties>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc-portlet</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j-api.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>${logback.version}</version>
</dependency>
</dependencies>
<build>
<finalName>boce_web</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>${project.build.sourceEncoding}</encoding>
<sourceEncoding>${project.build.sourceEncoding}</sourceEncoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
2.编写发送邮件代码:
package com.boce.server.tools;
import java.util.Properties;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.util.StringUtils;
public class SendMailTools {
static Logger logger = LoggerFactory.getLogger(SendMailTools.class);
private JavaMailSender javaMailSender;
private SimpleMailMessage simpleMailMessage;
public void setSimpleMailMessage(SimpleMailMessage simpleMailMessage) {
this.simpleMailMessage = simpleMailMessage;
}
public void setJavaMailSender(JavaMailSender javaMailSender) {
this.javaMailSender = javaMailSender;
}
/**
* 使用spring 发送文本email
*
* @return
*/
public boolean SpringSendMailText(MailInfo mailInfo) {
if(!emailValidate(mailInfo)){
return false;
}
simpleMailMessage.setSubject(mailInfo.getSubject());
simpleMailMessage.setText(mailInfo.getText());
javaMailSender.send(simpleMailMessage);
logger.info("发送邮件成功");
return true;
}
/**
* 使用spring 发送文本email
*
* @return
*/
public boolean SpringSendMailHtml(MailInfo mailInfo) {
// 建立邮件消息,发送简单邮件和html邮件的区别
MimeMessage mailMessage = javaMailSender.createMimeMessage();
MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage);
if(!emailAddressValidate(mailInfo) || !emailAddressValidate(mailInfo)){
return false;
}
try {
messageHelper.setTo(mailInfo.getMailto());
messageHelper.setFrom(mailInfo.getMailfrom());
messageHelper.setSubject(mailInfo.getSubject());
messageHelper.setText(mailInfo.getText(), true);
javaMailSender.send(mailMessage);
} catch (MessagingException e) {
StringBuffer sb = new StringBuffer(64);
sb.append("to:").append(mailInfo.getMailto());
sb.append(";from=").append(mailInfo.getMailfrom());
logger.info(sb.toString(), e);
return false;
}
logger.info("发送邮件成功");
return true;
}
/**
* 邮件标题和内容验证
* @param mailInfo
* @return
*/
private boolean emailValidate(MailInfo mailInfo){
if (null == mailInfo) {
logger.error("MailInfo is null");
return false;
}
if (StringUtils.isEmpty(mailInfo.getSubject())) {
logger.error("Subject is null,邮件标题不可以为空");
return false;
}
if (StringUtils.isEmpty(mailInfo.getText())) {
logger.error("Subject is null,邮件内容不可以为空");
return false;
}
return true;
}
/**
* 验证邮件发送地址和接收地址
* @param mailInfo
* @return
*/
private boolean emailAddressValidate(MailInfo mailInfo){
if (null == mailInfo) {
logger.error("MailInfo is null");
return false;
}
if (StringUtils.isEmpty(mailInfo.getMailto())) {
logger.error("mailto is null,收件人不可以为空");
return false;
}
if (StringUtils.isEmpty(mailInfo.getMailfrom())) {
logger.error("mailto is null,发件人不可以为空");
return false;
}
return true;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
/**
* 实现类发送简单email
*
* @param mailInfo
* @return
*/
public boolean sendSinaEmail(MailInfo mailInfo) {
JavaMailSenderImpl mailSend = new JavaMailSenderImpl();
mailSend.setHost(mailInfo.getHost());
mailSend.setPort(mailInfo.getPort());
mailSend.setUsername(mailInfo.getUserName());
mailSend.setPassword(mailInfo.getPassword());
Properties javaMailProperties = new Properties();
javaMailProperties.setProperty("mail.transport.protocol", "smtp");
javaMailProperties.setProperty("mail.smtp.auth", "true");
javaMailProperties.setProperty("mail.debug", "false");
javaMailProperties.setProperty("mail.smtp.timeout", "8000");
mailSend.setJavaMailProperties(javaMailProperties);
SimpleMailMessage simpleMessage = new SimpleMailMessage();
simpleMessage.setTo(mailInfo.getMailto());
simpleMessage.setFrom(mailInfo.getMailfrom());
simpleMessage.setSubject(mailInfo.getSubject());
simpleMessage.setText(mailInfo.getText());
try{
mailSend.send(simpleMessage);
}catch(MailException e){
StringBuffer sb = new StringBuffer(64);
sb.append("to:").append(mailInfo.getMailto());
sb.append(";from=").append(mailInfo.getMailfrom());
sb.append(";port=").append(mailInfo.getPort());
logger.error(sb.toString(), e);
return false;
}
logger.info("发送邮件成功");
return true;
}
/**
* 发送email包含html和text
*
* @param mailInfo
* @return
*/
public boolean sendEmailHtml(MailInfo mailInfo) {
JavaMailSenderImpl mailSend = new JavaMailSenderImpl();
mailSend.setHost(mailInfo.getHost());
mailSend.setPort(mailInfo.getPort());
mailSend.setUsername(mailInfo.getUserName());
mailSend.setPassword(mailInfo.getPassword());
Properties javaMailProperties = new Properties();
javaMailProperties.setProperty("mail.transport.protocol", "smtp");
javaMailProperties.setProperty("mail.smtp.auth", "true");
javaMailProperties.setProperty("mail.debug", "false");
javaMailProperties.setProperty("mail.smtp.timeout", "8000");
mailSend.setJavaMailProperties(javaMailProperties);
// SimpleMailMessage simpleMessage = new SimpleMailMessage();
// simpleMessage.setTo(mailInfo.getMailto());
// simpleMessage.setFrom(mailInfo.getMailfrom());
// simpleMessage.setSubject(mailInfo.getSubject());
// simpleMessage.setText(mailInfo.getText());
MimeMessage mimeMessage = mailSend.createMimeMessage();
MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage);
try{
messageHelper.setTo(mailInfo.getMailto());
messageHelper.setFrom(mailInfo.getMailfrom());
messageHelper.setSubject(mailInfo.getSubject());
messageHelper.setText(mailInfo.getText(), true);
mailSend.send(mimeMessage);
}catch(Exception e){
StringBuffer sb = new StringBuffer(64);
sb.append("to:").append(mailInfo.getMailto());
sb.append(";from=").append(mailInfo.getMailfrom());
sb.append(";port=").append(mailInfo.getPort());
logger.error(sb.toString(), e);
return false;
}
logger.info("发送邮件成功");
return true;
}
public static void main(String[] args) {
SendMailTools tools = new SendMailTools();
//tools.sinaSendMail();
MailInfo mailInfo = new MailInfo();
mailInfo.setHost("smtp.sina.com");
mailInfo.setMailfrom("gj@sina.com");
mailInfo.setMailto("179377968@qq.com");
mailInfo.setUserName("gjp");
mailInfo.setPassword("gjp");
mailInfo.setSubject("tomcat 启动发送邮件2");
mailInfo.setText("<html><header>test</header><body><a href='http://www.baidu.com'>baidu</a></body></html>");
// tools.sendSinaEmail(mailInfo);
tools.sendEmailHtml(mailInfo);
// // 方法二 使用spring3
// ApplicationContext app = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
// SendMailTools sendMailTools = (SendMailTools) app.getBean("sendMailTools");
// // sendMailTools.StringSendMailText();
//
// // sendMailTools.StringSendMailHtml();
}
}
3.实现ServletContextListener 接口
package com.boce.server.listener;
import java.util.Enumeration;
import java.util.Properties;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.boce.server.tools.MailInfo;
import com.boce.server.tools.SendMailTools;
/**
* @author gjp
*
*/
public class ServerStopListener implements ServletContextListener{
static Logger log = LoggerFactory.getLogger(ServerStopListener.class);
@Override
public void contextDestroyed(ServletContextEvent event) {
log.info("====================================stop.............");
Enumeration<String> e = event.getServletContext().getAttributeNames();
while(e.hasMoreElements()){
System.out.println("name:"+e.nextElement());
}
MailInfo mailInfo = init();
mailInfo.setSubject("tomcat 关闭发送邮件");
mailInfo.setText("tomcat停止发送通知邮件邮件 ----stop.....");
sendMail(mailInfo);
}
@Override
public void contextInitialized(ServletContextEvent event) {
log.info("############################start#######################");
MailInfo mailInfo = init();
mailInfo.setSubject("tomcat 启动发送邮件");
mailInfo.setText("<html><header>test</header><body><a href='http://www.baidu.com'>baidu</a></body></html>");
sendMail(mailInfo);
}
//初始化邮件配置
private MailInfo init(){
MailInfo mailInfo = new MailInfo();
mailInfo.setHost("smtp.sina.com");
mailInfo.setMailfrom("gjp@sina.com");
mailInfo.setMailto("179377968@qq.com");
mailInfo.setUserName("gjp");
mailInfo.setPassword("g");
mailInfo.setSubject("tomcat 启动发送邮件");
mailInfo.setText("tomcat启动发送邮件 ----start......");
return mailInfo;
}
//发送邮件
public void sendMail(MailInfo mailInfo){
SendMailTools tools = new SendMailTools();
tools.sendEmailHtml(mailInfo);
}
}
代码在附件中:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.boce</groupId>
<artifactId>boce_web</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>boce_web Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>3.2.18.RELEASE</spring.version>
<slf4j-api.version>1.7.5</slf4j-api.version>
<logback.version>1.0.13</logback.version>
</properties>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc-portlet</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j-api.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>${logback.version}</version>
</dependency>
</dependencies>
<build>
<finalName>boce_web</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>${project.build.sourceEncoding}</encoding>
<sourceEncoding>${project.build.sourceEncoding}</sourceEncoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
2.编写发送邮件代码:
package com.boce.server.tools;
import java.util.Properties;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.util.StringUtils;
public class SendMailTools {
static Logger logger = LoggerFactory.getLogger(SendMailTools.class);
private JavaMailSender javaMailSender;
private SimpleMailMessage simpleMailMessage;
public void setSimpleMailMessage(SimpleMailMessage simpleMailMessage) {
this.simpleMailMessage = simpleMailMessage;
}
public void setJavaMailSender(JavaMailSender javaMailSender) {
this.javaMailSender = javaMailSender;
}
/**
* 使用spring 发送文本email
*
* @return
*/
public boolean SpringSendMailText(MailInfo mailInfo) {
if(!emailValidate(mailInfo)){
return false;
}
simpleMailMessage.setSubject(mailInfo.getSubject());
simpleMailMessage.setText(mailInfo.getText());
javaMailSender.send(simpleMailMessage);
logger.info("发送邮件成功");
return true;
}
/**
* 使用spring 发送文本email
*
* @return
*/
public boolean SpringSendMailHtml(MailInfo mailInfo) {
// 建立邮件消息,发送简单邮件和html邮件的区别
MimeMessage mailMessage = javaMailSender.createMimeMessage();
MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage);
if(!emailAddressValidate(mailInfo) || !emailAddressValidate(mailInfo)){
return false;
}
try {
messageHelper.setTo(mailInfo.getMailto());
messageHelper.setFrom(mailInfo.getMailfrom());
messageHelper.setSubject(mailInfo.getSubject());
messageHelper.setText(mailInfo.getText(), true);
javaMailSender.send(mailMessage);
} catch (MessagingException e) {
StringBuffer sb = new StringBuffer(64);
sb.append("to:").append(mailInfo.getMailto());
sb.append(";from=").append(mailInfo.getMailfrom());
logger.info(sb.toString(), e);
return false;
}
logger.info("发送邮件成功");
return true;
}
/**
* 邮件标题和内容验证
* @param mailInfo
* @return
*/
private boolean emailValidate(MailInfo mailInfo){
if (null == mailInfo) {
logger.error("MailInfo is null");
return false;
}
if (StringUtils.isEmpty(mailInfo.getSubject())) {
logger.error("Subject is null,邮件标题不可以为空");
return false;
}
if (StringUtils.isEmpty(mailInfo.getText())) {
logger.error("Subject is null,邮件内容不可以为空");
return false;
}
return true;
}
/**
* 验证邮件发送地址和接收地址
* @param mailInfo
* @return
*/
private boolean emailAddressValidate(MailInfo mailInfo){
if (null == mailInfo) {
logger.error("MailInfo is null");
return false;
}
if (StringUtils.isEmpty(mailInfo.getMailto())) {
logger.error("mailto is null,收件人不可以为空");
return false;
}
if (StringUtils.isEmpty(mailInfo.getMailfrom())) {
logger.error("mailto is null,发件人不可以为空");
return false;
}
return true;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
/**
* 实现类发送简单email
*
* @param mailInfo
* @return
*/
public boolean sendSinaEmail(MailInfo mailInfo) {
JavaMailSenderImpl mailSend = new JavaMailSenderImpl();
mailSend.setHost(mailInfo.getHost());
mailSend.setPort(mailInfo.getPort());
mailSend.setUsername(mailInfo.getUserName());
mailSend.setPassword(mailInfo.getPassword());
Properties javaMailProperties = new Properties();
javaMailProperties.setProperty("mail.transport.protocol", "smtp");
javaMailProperties.setProperty("mail.smtp.auth", "true");
javaMailProperties.setProperty("mail.debug", "false");
javaMailProperties.setProperty("mail.smtp.timeout", "8000");
mailSend.setJavaMailProperties(javaMailProperties);
SimpleMailMessage simpleMessage = new SimpleMailMessage();
simpleMessage.setTo(mailInfo.getMailto());
simpleMessage.setFrom(mailInfo.getMailfrom());
simpleMessage.setSubject(mailInfo.getSubject());
simpleMessage.setText(mailInfo.getText());
try{
mailSend.send(simpleMessage);
}catch(MailException e){
StringBuffer sb = new StringBuffer(64);
sb.append("to:").append(mailInfo.getMailto());
sb.append(";from=").append(mailInfo.getMailfrom());
sb.append(";port=").append(mailInfo.getPort());
logger.error(sb.toString(), e);
return false;
}
logger.info("发送邮件成功");
return true;
}
/**
* 发送email包含html和text
*
* @param mailInfo
* @return
*/
public boolean sendEmailHtml(MailInfo mailInfo) {
JavaMailSenderImpl mailSend = new JavaMailSenderImpl();
mailSend.setHost(mailInfo.getHost());
mailSend.setPort(mailInfo.getPort());
mailSend.setUsername(mailInfo.getUserName());
mailSend.setPassword(mailInfo.getPassword());
Properties javaMailProperties = new Properties();
javaMailProperties.setProperty("mail.transport.protocol", "smtp");
javaMailProperties.setProperty("mail.smtp.auth", "true");
javaMailProperties.setProperty("mail.debug", "false");
javaMailProperties.setProperty("mail.smtp.timeout", "8000");
mailSend.setJavaMailProperties(javaMailProperties);
// SimpleMailMessage simpleMessage = new SimpleMailMessage();
// simpleMessage.setTo(mailInfo.getMailto());
// simpleMessage.setFrom(mailInfo.getMailfrom());
// simpleMessage.setSubject(mailInfo.getSubject());
// simpleMessage.setText(mailInfo.getText());
MimeMessage mimeMessage = mailSend.createMimeMessage();
MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage);
try{
messageHelper.setTo(mailInfo.getMailto());
messageHelper.setFrom(mailInfo.getMailfrom());
messageHelper.setSubject(mailInfo.getSubject());
messageHelper.setText(mailInfo.getText(), true);
mailSend.send(mimeMessage);
}catch(Exception e){
StringBuffer sb = new StringBuffer(64);
sb.append("to:").append(mailInfo.getMailto());
sb.append(";from=").append(mailInfo.getMailfrom());
sb.append(";port=").append(mailInfo.getPort());
logger.error(sb.toString(), e);
return false;
}
logger.info("发送邮件成功");
return true;
}
public static void main(String[] args) {
SendMailTools tools = new SendMailTools();
//tools.sinaSendMail();
MailInfo mailInfo = new MailInfo();
mailInfo.setHost("smtp.sina.com");
mailInfo.setMailfrom("gj@sina.com");
mailInfo.setMailto("179377968@qq.com");
mailInfo.setUserName("gjp");
mailInfo.setPassword("gjp");
mailInfo.setSubject("tomcat 启动发送邮件2");
mailInfo.setText("<html><header>test</header><body><a href='http://www.baidu.com'>baidu</a></body></html>");
// tools.sendSinaEmail(mailInfo);
tools.sendEmailHtml(mailInfo);
// // 方法二 使用spring3
// ApplicationContext app = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
// SendMailTools sendMailTools = (SendMailTools) app.getBean("sendMailTools");
// // sendMailTools.StringSendMailText();
//
// // sendMailTools.StringSendMailHtml();
}
}
3.实现ServletContextListener 接口
package com.boce.server.listener;
import java.util.Enumeration;
import java.util.Properties;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.boce.server.tools.MailInfo;
import com.boce.server.tools.SendMailTools;
/**
* @author gjp
*
*/
public class ServerStopListener implements ServletContextListener{
static Logger log = LoggerFactory.getLogger(ServerStopListener.class);
@Override
public void contextDestroyed(ServletContextEvent event) {
log.info("====================================stop.............");
Enumeration<String> e = event.getServletContext().getAttributeNames();
while(e.hasMoreElements()){
System.out.println("name:"+e.nextElement());
}
MailInfo mailInfo = init();
mailInfo.setSubject("tomcat 关闭发送邮件");
mailInfo.setText("tomcat停止发送通知邮件邮件 ----stop.....");
sendMail(mailInfo);
}
@Override
public void contextInitialized(ServletContextEvent event) {
log.info("############################start#######################");
MailInfo mailInfo = init();
mailInfo.setSubject("tomcat 启动发送邮件");
mailInfo.setText("<html><header>test</header><body><a href='http://www.baidu.com'>baidu</a></body></html>");
sendMail(mailInfo);
}
//初始化邮件配置
private MailInfo init(){
MailInfo mailInfo = new MailInfo();
mailInfo.setHost("smtp.sina.com");
mailInfo.setMailfrom("gjp@sina.com");
mailInfo.setMailto("179377968@qq.com");
mailInfo.setUserName("gjp");
mailInfo.setPassword("g");
mailInfo.setSubject("tomcat 启动发送邮件");
mailInfo.setText("tomcat启动发送邮件 ----start......");
return mailInfo;
}
//发送邮件
public void sendMail(MailInfo mailInfo){
SendMailTools tools = new SendMailTools();
tools.sendEmailHtml(mailInfo);
}
}
代码在附件中:
- boce_web.rar (23 KB)
- 下载次数: 0
发表评论
-
httpClient 使用http协议上传文件
2018-10-09 15:58 3107<dependency> <grou ... -
隐藏tomcat 隐藏nginx 版本号
2017-11-22 22:32 1178如果tomcat的版本号暴露出来,会有人利用该版本所存在的漏洞 ... -
spring boot 内嵌Tomcat 配置
2017-10-25 11:17 10073Tomcat为Spring Boot的默认容器,下面是几个常用 ... -
spring boot 使用tomcat 运行
2017-10-25 11:14 10011.修改启动类,继承 SpringBootServletIni ... -
tomcat7 中使用jndi连接数据源
2017-09-01 09:25 783在使用eclipse 开发环境中配置tomcat7的jndi, ... -
监控tomcat 启动和停止向管理员发送通知
2017-08-09 16:55 1078当服务上的web容器由于故障停止工作了,怎样通知管理员呢? 我 ... -
tomcat 远程调试代码
2017-05-27 15:14 504Linux 中的tomcat远程调试代码 1,是要编辑cata ... -
resis实现tomcat7 session 共享
2017-05-02 15:08 568resis实现tomcat7 session 共享 1.下载 ... -
tomcat7 优化
2017-04-21 10:59 356tomcat7 优化实例: 1、在bin/catalina. ... -
通过 tomcat 访问文件目录下的文件
2017-03-14 09:11 1421修改tomcat 中的server.xml ,在<ho ... -
中文乱码问题
2016-11-18 14:34 476使用 sping4 框架在tomcat7 运行时发现使用ht ... -
tomcat 性能调优
2015-05-21 14:00 5691.----------------------Li ... -
tomcat 发布应用
2015-05-18 13:21 5651.在tomcat安装目录中有一个conf文件夹,打开 ...
相关推荐
二、Tomcat启动流程 1. **初始化配置**:Tomcat首先读取`conf/server.xml`配置文件,解析各个组件的配置信息,构建组件层次结构。 2. **加载服务**:根据配置,Tomcat创建Service对象,Service由一个或多个...
6. **报警机制**:当Tomcat服务出现异常时,可以发送邮件或短信通知管理员,以便及时处理。 总的来说,"Tomcat崩溃自启动"是一个涉及多方面技能的系统工程,包括服务器管理、脚本编程、日志分析和故障排除等。通过...
- **bin**:包含启动和停止Tomcat的脚本以及一些实用工具。 - **conf**:存放Tomcat的配置文件,如server.xml、web.xml等。 - **lib**:存储Tomcat运行所需的库文件。 - **logs**:日志文件存放的地方,记录Tomcat...
7. **监控和报警**:为了保证系统的稳定,可以添加监控功能,当自动重启失败时,通过邮件、短信或其他方式发送报警通知。 8. **定期更新**:自动重启还可以与软件更新相结合,确保Tomcat服务器始终运行最新版本,...
1. **启动**:Tomcat启动时,会读取配置文件(如server.xml和web.xml),解析并加载所有必要的组件和服务。 2. **请求处理**:当HTTP请求到达时,Connector捕获请求,解析请求头和实体,然后将请求转发给Catalina...
10. **社区支持**:作为开源项目,Tomcat拥有庞大的社区支持,开发者可以在官方论坛、邮件列表和Stack Overflow等平台上找到丰富的文档、教程和解决方案。 综上所述,"Tomcat 6.0 安装版"提供了一种简单易用的方式...
- `bin`:包含启动和停止Tomcat的脚本,以及管理工具。 - `conf`:存放配置文件,如`server.xml`(全局配置)、`web.xml`(默认Servlet和过滤器配置)和`context.xml`(应用上下文配置)。 - `lib`:包含Tomcat...
Catalina还负责管理Web应用程序的生命周期,包括加载、启动、停止和卸载等过程。 2. **Jasper**:Jasper是Tomcat中的JSP编译器,负责将JSP文件转换为Java源代码,然后编译成Servlet。这个过程发生在JSP首次被请求时...
- `bin`:包含启动和停止Tomcat的脚本,以及管理工具。 - `conf`:存储Tomcat的配置文件,如server.xml、web.xml等。 - `lib`:存放Tomcat运行所需的JAR文件和库。 - `logs`:记录Tomcat运行时的日志信息。 - `...
10. **bin**:包含了启动、停止和管理Tomcat的脚本,如`catalina.sh`(Linux/Unix)和`catalina.bat`(Windows)。 为了部署一个新的Web应用,只需将应用的WAR文件(或解压后的目录)放在`webapps`目录下,Tomcat会...
4. **目录结构**:Tomcat的默认目录结构包含了诸如`bin`(包含启动和停止脚本)、`conf`(配置文件)、`webapps`(Web应用的部署位置)和`logs`(日志文件)等重要部分。 5. **部署Web应用**:文档会解释如何通过...
1. `bin`:包含启动和停止Tomcat的脚本,如`catalina.sh`(Unix/Linux)和`catalina.bat`(Windows)。 2. `conf`:存放所有配置文件,如`server.xml`(定义服务器配置)、`web.xml`(全局Web应用配置)和`context....
- `bin`: 包含启动和停止脚本。 - `conf`: 存放配置文件。 - `lib`: 放置Tomcat运行所需的JAR文件。 - `logs`: 记录服务器日志。 - `temp`: 临时文件存储。 - `webapps`: 部署的应用程序存放处。 - `work`: 存放编译...
6. **JNDI(Java Naming and Directory Interface)**: Tomcat支持JNDI,允许应用查找和绑定资源,如数据源、邮件会话等。 7. **Clustering和Session复制**: 用于构建高可用性、负载均衡的Tomcat集群,通过session...
书中首先会介绍Tomcat的安装与配置,包括环境变量设置、服务器启动与停止等基本操作。 2. **Servlet与JSP**:作为Java Web开发的核心,Servlet和JSP是开发者必须掌握的技术。书中的这部分内容会详细讲解Servlet生命...
Tomcat中的每个组件都有一个生命周期,包括初始化、启动、暂停、重新启动、停止和销毁等阶段。通过`Lifecycle`接口和`LifecycleListener`,可以监听和控制这些阶段。 3. **容器概念** Tomcat的核心概念之一是容器...
- **bin**:包含了启动、停止和管理Tomcat的脚本。 - **conf**:存放Tomcat的配置文件,如server.xml、web.xml和context.xml。 - **lib**:存放Tomcat运行时所需的JAR文件,以及应用程序可以使用的共享库。 - **logs...
Tomcat源码中包含对Web应用生命周期的管理,如初始化、启动、停止和卸载等阶段的处理。 9. **部署与配置**: 通过`server.xml`, `web.xml`等配置文件,开发者可以定制Tomcat的行为和Web应用的部署方式。 10. **...
4. **集成Zabbix或Nagios**:这些专业的监控系统不仅可以监控Tomcat,还可以在检测到异常时执行自定义操作,比如发送报警邮件或自动重启。 5. **使用Spring Boot Actuator**:如果你的应用是基于Spring Boot,可以...