如何在jmeter中使用SMTP服务器发送电子邮件

时间:2019-02-01 12:06:28

标签: jmeter smtp jmeter-plugins jmeter-3.2 jmeter-4.0

我曾尝试使用SMTP Sampler发送电子邮件,但无法发送。出现以下错误并显示500个响应代码。

Response code: 500
Response message: Could not connect to SMTP host: smtp.gmail.com, port: 587

enter image description here

1 个答案:

答案 0 :(得分:0)

  1. 首先使用telnet client或同等功能检查从计算机到587主机的端口smtp.gmail.com的连接,您应该看到类似以下内容的

    Connected to gmail-smtp-msa.l.google.com.
    Escape character is '^]'.
    220 smtp.gmail.com ESMTP g20sm1259543lfh.33 - gsmtp
    

    enter image description here

    如果您将无法看到以上输出-可能是操作系统或路由器firewall阻止了对Gmail SMTP端口的访问,因此您需要与网络管理员联系以获得访问权限

  2. 您的组织可能正在使用corporate proxy访问Internet。在这种情况下-您将无法使用SMTP Sampler,因为它不支持代理配置especially if the authentication is required。在这种情况下,您将需要切换到JSR223 Sampler并编写使用Groovy语言发送电子邮件的逻辑。

    • Download simple-java-mail-5.1.3.jar并将其拖放到JMeter安装的“ lib”文件夹中
    • Download emailaddress-rfc2822-1.1.2.jar并将其拖放到JMeter安装的“ lib”文件夹中
    • 重新启动JMeter以拾取.jars
    • 将JSR223采样器添加到您的测试计划中
    • 将以下代码放入“脚本”区域:

      import org.simplejavamail.email.Email
      import org.simplejavamail.email.EmailBuilder
      import org.simplejavamail.mailer.Mailer
      import org.simplejavamail.mailer.MailerBuilder
      import org.simplejavamail.mailer.config.TransportStrategy
      
      Mailer mailer = MailerBuilder
              .withTransportStrategy(TransportStrategy.SMTP_TLS)
              .withSMTPServer("smtp.gmail.com", 587)
              .withSMTPServerUsername("Your SMTP Username")
              .withSMTPServerPassword("Your SMTP Password")
              .withProxyHost("replace with your proxy host")
              .withProxyPort(1234) // replace with your proxy port
              .withProxyUsername("your proxy username if needed")
              .withProxyPassword("your proxy password if needed")
              .buildMailer()
      
      Email email = EmailBuilder.startingBlank()
              .from("SMTP FROM address (in the majority of cases the same as SMTP Username)")
              .to("Recipient")
              .withSubject("test script")
              .withPlainText("test message")
              .buildEmail()
      
      mailer.sendMail(email)