用LoadRunner测试139邮箱的脚本,用JavaVUser编写java脚本调用mail包发送邮件,包括附件:
import java.util.Date;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
import com.sun.mail.smtp.*;
public class Actions
{
public int init() {
return 0;
}//end of init
public int action() {
boolean ssl = false;
Properties props = System.getProperties();
//设置SMTP主机地址
props.put("mail.smtp.host", " 123.125.50.133");
//设置SMTP端口号,465是ssl,25是非ssl端口
props.put("mail.smtp.port",ssl ? "465" : "25");
//设置SMTP用户名
props.put("mail.smtp.user","xxxx@139.com");
Session session=Session.getInstance(props,null);
String mailer = "send from VU java SMTP";
//设置收件人
String sendTo = "xxxx@139.com";
//设置抄送人
String sendCC = "";
//设置发件人
String sendFrom = "xxxx@139.com";
try{
SMTPTransport t = (SMTPTransport)session.getTransport(ssl ? "smtps" : "smtp");
//与SMTP主机相连
t.connect("123.125.50.133","xxxx@139.com","xxx");
MimeMessage msg = new MimeMessage(session);
Attachment attachment = new Attachment();
attachment.setFile(f);
//设置邮件各个字段
String subject = "smtp测试";
String personalName = "LR性能测试用户";
String body = "这里有很多内容\r\n这里有很多内容\r\n这里有很多内容\r\n这里有很多内容\r\n这里有很多内容";
String charset = "utf-8";
String name="SendJob.java";
String filePath="C:/Users/Administrator/Desktop/mailsender";
InternetAddress[] to = new InternetAddress[1];
InternetAddress from = new InternetAddress(sendFrom,personalName,charset);
to[0] =new InternetAddress(sendTo,personalName,charset);
attachment.setName(name);
attachments.add(attachment);
msg.setFrom(from);
msg.setRecipients(Message.RecipientType.TO,to);
msg.setSubject(subject,charset);
msg.setText(body,charset);
msg.setSentDate(new Date());
//发送邮件
t.sendMessage(msg, msg.getAllRecipients());
t.close();
}
catch (Exception e){
e.printStackTrace();
}
finally{
}
return 0;
}//end of action
/**
* 添加附件
*
* @param name
* @param path
* @throws FileNotFoundException
*/
public void addAttachment(String name, String filePath)
throws FileNotFoundException {
File f = new File(filePath);
if (!f.exists()) {
throw new FileNotFoundException("File [" + filePath + "] not found");
}
}
/**
* 添加附件
*
* @param filePath
* @throws FileNotFoundException
*/
public int end() {
return 0;
}//end of end
}