今天老大提出一个需求,说如果用例执行错误后就发一个邮件通知给他。看到这个需求后,我就觉得这个比较麻烦,其实是因为我以前没有写过发邮件的程序,所以对未知的东西总有点怕怕的,我这个老大人很好,说你尽量快点实现就可以了,不会强迫我什么时候一定要完成这个功能什么的,这么好的老大很少见了哦。好了废话少说了,看我的发邮件的程序吧,第一:要将程序中需要的mail.jar和activation.jar放到工程的lib目录下,然后引进工程里面去。
第二:写程序、
package com.carryquote.autotest.util.mail;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
/**
* @author suse
*
*/
public class MailUtil {
/**
* @param args
*/
private String host= "smtp.163.com";//主机;
private String user= "******@163.com";//用户名;
private String password= "********";//密码;
private String from= "***************@163.com";//发件人;
private String to= "*****@carryquote.com";//收件人;
private String subjetc= "Hahaha";//标题;
private String content= "testteststtst";//内容;
//设置内容
public void setContent(String content) {
this.content = content;
}
//设置收件人
public void setTo(String to) {
this.to = to;
}
/**
* 发送email
* @throws MessagingException
* @throws Exception
*/
public void sendMail() throws MessagingException,Exception{
Properties props = new Properties();
props.put( "mail.smtp.host", host);//指定SMTP服务器
props.put( "mail.smtp.auth", "true");//指定是否需要SMTP验证
Session mailSession = Session.getDefaultInstance(props);
Message message=new MimeMessage(mailSession);
message.setFrom(new InternetAddress(from));//发件人
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));//收件人
message.setSubject(subjetc);//邮件主题
message.setText(content);//邮件内容
message.saveChanges();
Transport transport = mailSession.getTransport( "smtp");
transport.connect(host, user, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
new MailUtil().sendMail();
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}