cmaedu.com
cmaedu.com可以选择使用log4j,它是一款开源的日志记录工具,提供发送日志邮件功能
cmaedu.comLog4j发送日志邮件的作用:
项目错误信息能及时(实时)反映给项目维护人员以及相关负责人。
优点:
cmaedu.com 1.快速响应;
2.共同监督;
cmaedu.com 3.邮件正文直接显示了错误信息,拷贝信息比登陆服务器再查找要方便;
cmaedu.com 4.在日志信息继续写入文件的前提下,多了另外一种获取信息的渠道。
补充:Log4j可以实现输出到控制台,文件,回滚文件,发送日志邮件,数据库,自定义标签。
发送邮件的一个重要的类是SMTPAppender,之前用的是 log4j-1.2.8,在1.2.8的版本中,SMTPAppender没有SMTPPassword 和SMTPUsername 属性。这两个属性分别是登录SMTP服务器发送认证的用户名和密码。
依赖的jar包:
log4j-1.2.15.jar(版本低于log4j-1.2.14.jar不支持SMTP认证)
mail.jar
activation.jar
cmaedu.com在log4j.properties文件中配置:
cmaedu.com### send error through email.
#log4j的邮件发送appender,如果有必要你可以写自己的appender
cmaedu.comlog4j.appender.MAIL=org.apache.log4j.net.SMTPAppender
#发送邮件的门槛,仅当等于或高于ERROR(比如FATAL)时,邮件才被发送
cmaedu.comlog4j.appender.MAIL.Threshold=ERROR
cmaedu.com#缓存文件大小,日志达到10k时发送Email
log4j.appender.MAIL.BufferSize=10
cmaedu.com#发送邮件的邮箱帐号
cmaedu.comlog4j.appender.MAIL.From=xxx@163.com
cmaedu.com#SMTP邮件发送服务器地址
cmaedu.comlog4j.appender.MAIL.SMTPHost=smtp.163.com
cmaedu.com#SMTP发送认证的帐号名
log4j.appender.MAIL.SMTPUsername=xxx@163.com
cmaedu.com#SMTP发送认证帐号的密码
cmaedu.comlog4j.appender.MAIL.SMTPPassword=xxx
#是否打印调试信息,如果选true,则会输出和SMTP之间的握手等详细信息
cmaedu.comlog4j.appender.MAIL.SMTPDebug=false
cmaedu.com#邮件主题
cmaedu.comlog4j.appender.MAIL.Subject=Log4JErrorMessage
#发送到什么邮箱,如果要发送给多个邮箱,则用逗号分隔;
#如果需要发副本给某人,则加入下列行
cmaedu.com#log4j.appender.MAIL.Bcc=xxx@xxx.xxx
cmaedu.comlog4j.appender.MAIL.To=xxx@xxx.com
log4j.appender.MAIL.layout=org.apache.log4j.PatternLayout
log4j.appender.MAIL.layout.ConversionPattern=[framework]%d - %c -%-4r[%t]%-5p %c %x -%m%n
在java代码中,可是用logger.info("message");方法将message代表的消息发送到指定邮箱中
cmaedu.com
cmaedu.com实现java发送邮件的过程大体有以下几步:
准备一个properties文件,该文件中存放SMTP服务器地址等参数。
利用properties创建一个Session对象
利用Session创建Message对象,然后设置邮件主题和正文
cmaedu.com利用Transport对象发送邮件
cmaedu.com需要的jar有2个:activation.jar和mail.jar发送附件,需要用到Multipart对象。
cmaedu.comimport java.io.File;
cmaedu.comimport java.io.IOException;
cmaedu.comimport java.io.InputStream;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
cmaedu.comimport javax.mail.BodyPart;
import javax.mail.Message;
cmaedu.comimport javax.mail.MessagingException;
import javax.mail.Multipart;
cmaedu.comimport javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
cmaedu.comimport javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
public class JavaMailWithAttachment {
cmaedu.com private MimeMessage message;
cmaedu.com private Session session;
private Transport transport;
private String mailHost = "";
cmaedu.com private String sender_username = "";
cmaedu.com private String sender_password = "";
private Properties properties = new Properties();
cmaedu.com * 初始化方法
public JavaMailWithAttachment(boolean debug) {
cmaedu.com InputStream in = JavaMailWithAttachment.class.getResourceAsStream("MailServer.properties");
try {
cmaedu.com properties.load(in);
this.mailHost = properties.getProperty("mail.smtp.host");
cmaedu.com this.sender_username = properties.getProperty("mail.sender.username");
this.sender_password = properties.getProperty("mail.sender.password");
cmaedu.com } catch (IOException e) {
cmaedu.com e.printStackTrace();
cmaedu.com session = Session.getInstance(properties);
cmaedu.com session.setDebug(debug);// 开启后有调试信息
cmaedu.com message = new MimeMessage(session);
cmaedu.com * 发送邮件
* @param subject
* 邮件主题
cmaedu.com * @param sendHtml
* 邮件内容
cmaedu.com * @param receiveUser
* 收件人地址
cmaedu.com * @param attachment
* 附件
cmaedu.com public void doSendHtmlEmail(String subject, String sendHtml, String receiveUser, File attachment) {
try {
cmaedu.com // 发件人
InternetAddress from = new InternetAddress(sender_username);
message.setFrom(from);
// 收件人
cmaedu.com InternetAddress to = new InternetAddress(receiveUser);
message.setRecipient(Message.RecipientType.TO, to);
cmaedu.com // 邮件主题
cmaedu.com message.setSubject(subject);
cmaedu.com // 向multipart对象中添加邮件的各个部分内容,包括文本内容和附件
cmaedu.com Multipart multipart = new MimeMultipart();
cmaedu.com // 添加邮件正文
cmaedu.com BodyPart contentPart = new MimeBodyPart();
cmaedu.com contentPart.setContent(sendHtml, "text/html;charset=UTF-8");
multipart.addBodyPart(contentPart);
// 添加附件的内容
cmaedu.com if (attachment != null) {
cmaedu.com BodyPart attachmentBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(attachment);
cmaedu.com attachmentBodyPart.setDataHandler(new DataHandler(source));
cmaedu.com // 网上流传的解决文件名乱码的方法,其实用MimeUtility.encodeWord就可以很方便的搞定
cmaedu.com // 这里很重要,通过下面的Base64编码的转换可以保证你的中文附件标题名在发送时不会变成乱码
//sun.misc.BASE64Encoder enc = new sun.misc.BASE64Encoder();
//messageBodyPart.setFileName("=?GBK?B?" + enc.encode(attachment.getName().getBytes()) + "?=");
cmaedu.com //MimeUtility.encodeWord可以避免文件名乱码
attachmentBodyPart.setFileName(MimeUtility.encodeWord(attachment.getName()));
cmaedu.com multipart.addBodyPart(attachmentBodyPart);
cmaedu.com // 将multipart对象放到message中
cmaedu.com message.setContent(multipart);
cmaedu.com // 保存邮件
cmaedu.com message.saveChanges();
cmaedu.com transport = session.getTransport("smtp");
cmaedu.com // smtp验证,就是你用来发邮件的邮箱用户名密码
cmaedu.com transport.connect(mailHost, sender_username, sender_password);
cmaedu.com // 发送
transport.sendMessage(message, message.getAllRecipients());
System.out.println("send success!");
cmaedu.com } catch (Exception e) {
cmaedu.com e.printStackTrace();
} finally {
cmaedu.com if (transport != null) {
cmaedu.com try {
cmaedu.com transport.close();
cmaedu.com } catch (MessagingException e) {
cmaedu.com e.printStackTrace();
public static void main(String[] args) {
cmaedu.com JavaMailWithAttachment se = new JavaMailWithAttachment(true);
File affix = new File("c:\\测试-test.txt");
se.doSendHtmlEmail("邮件主题", "邮件内容", "xxx@XXX.com", affix);//
cmaedu.com
cmaedu.compublic boolean mainto()
cmaedu.com boolean flag = true;
//建立邮件会话
Properties pro = new Properties();
cmaedu.com pro.put("mail.smtp.host","smtp.qq.com");//存储发送邮件的服务器
cmaedu.com pro.put("mail.smtp.auth","true"); //通过服务器验证
cmaedu.com Session s =Session.getInstance(pro); //根据属性新建一个邮件会话
cmaedu.com //s.setDebug(true);
cmaedu.com //由邮件会话新建一个消息对象
cmaedu.com MimeMessage message = new MimeMessage(s);
cmaedu.com //设置邮件
InternetAddress fromAddr = null;
InternetAddress toAddr = null;
cmaedu.com try
cmaedu.com fromAddr = new InternetAddress(451144426+"@qq.com"); //邮件发送地址
message.setFrom(fromAddr); //设置发送地址
cmaedu.com toAddr = new InternetAddress("12345367@qq.com"); //邮件接收地址
message.setRecipient(Message.RecipientType.TO, toAddr); //设置接收地址
message.setSubject(title); //设置邮件标题
message.setText(content); //设置邮件正文
cmaedu.com message.setSentDate(new Date()); //设置邮件日期
message.saveChanges(); //保存邮件更改信息
Transport transport = s.getTransport("smtp");
transport.connect("smtp.qq.com", "451144426", "密码"); //服务器地址,邮箱账号,邮箱密码
transport.sendMessage(message, message.getAllRecipients()); //发送邮件
cmaedu.com transport.close();//关闭
cmaedu.com catch (Exception e)
e.printStackTrace();
flag = false;//发送失败
return flag;
这是一个javaMail的邮件发送代码,需要一个mail.jar
发出去的邮件为什么总被认为是垃圾邮件 1、对方已经设置了你的邮箱是垃圾邮件来源。(如果一直是同一个邮箱拒收的话可能性很大)2、可能是因为你的邮件发了过多,系统评测你流量过大,那么你这边的服务器也会认为...
乐山市成都理工大学工程技术学院的邮信地址、要详细的啦,还要速度的、 学院地址:四川省乐山市市中区肖坝路222号理工大学 邮政编码:614007 另个学院地址:四川省乐山市市中区15号信箱 某部门...
《英雄联盟》战队名称有哪些? 如下:1、ヾ辉煌ヾ贵族2、『情夕』战队3、『灭魂』战队4、精达々精英5、夜月殇魂战队6、莫求图战队7、123战队8、交友不喝酒战队9、月明黑夜战队10、君惜少年战队11、...
杜维明提出的三个意义世界不包括 杜维明提出的三个意义世界不包括(B)。A.关注并研究中华文化的外国人士 B.来华旅游的外国人士 C.大陆和港澳台的华人 D.散居世界各地的华人 杜维明,男,1940年生...
为什么我用OUTLOOK无法正常接收HOTMAIL的邮件? Foxmail6.0beta2版本支持Hotmail不需要任何单独的设置,直接新建账户,按照要求填写好所有选项即可。Foxmail能够自动的...
QQ邮箱怎么样升级? 楼主您好 办法只有一个,每天不断地发送邮件!电脑每天登陆一次得5分(多次也一样),每发一封邮件得5分,每天最多20分!手机登陆QQ邮箱是电脑的一倍,也就是每天最多可得40分。要想...