139邮箱java版_用LoadRunner测试139邮箱的脚本,用JavaVUser

用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

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值