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、拨打投诉电话反映情况,中国教育部投诉电话010-66092315/66093315;...
那曲市教育局关于区外就读学生出藏返校事宜的通告? 加强对返校学生的管理,确保在校期间健康安全 (一)各院(系、部)继续坚持做好学生健康状况监测,加强防疫宣传教育,切实增强学生疫情防控意识和自我防护能力...
126邮箱里面有没有“定时器”之类的功能? 不知你说的“定时器”是指什么?邮箱有定时发信功能啊。。。另外在邮箱应用里有日程管理,创建事项之后也可以定时提醒的。其他的话就没有了。邮箱也有日程功能吗?怎么...
人民网邮箱地址 师徒联手查隐患 确保电路万里通内蒙古 赤峰市 喀喇沁旗农电局 调度通信所 牛志广在倡导“技能至上”,开展“师徒共发展”的活动中,调度通信所实行了师徒结对,老经验变成了新经验,...
有明星用QQ邮箱吗?用什么的比较多本人演员 邮箱只不过是联系的一个方式有那么重要吗?你演过什么呢?看看大家认识不?给你一个〞万紫千红〞你觉得怎样?大红大紫的 意思如何?163邮箱和126有什么区别呢?...
qxjzzxxx@163com找邮箱 "是想问这个邮箱如何登陆吗?1、web使用,通过手机或散逗电好哪脑浏览器访问mail.163.com即可登陆收发邮件2、客户端使用,手机和电脑都有客户端。比如电脑...