【问题标题】:How to Send email using a SMTP sever in jmeter如何在 jmeter 中使用 SMTP 服务器发送电子邮件
【发布时间】:2019-02-01 12:06:28
【问题描述】:
我曾尝试使用 SMTP Sampler 发送电子邮件,但无法发送。收到以下错误,响应代码为 500。
Response code: 500
Response message: Could not connect to SMTP host: smtp.gmail.com, port: 587
【问题讨论】:
标签:
jmeter
smtp
jmeter-plugins
jmeter-3.2
jmeter-4.0
【解决方案1】:
-
首先,使用telnet client 或等效方法检查您的机器与smtp.gmail.com 主机的端口587 的连接性,您应该会看到如下内容:
Connected to gmail-smtp-msa.l.google.com.
Escape character is '^]'.
220 smtp.gmail.com ESMTP g20sm1259543lfh.33 - gsmtp
如果您无法看到上述输出 - 可能是操作系统或路由器 firewall 阻止了对 Gmail SMTP 端口的访问,因此您需要联系您的网络管理员以获得访问权限
-
您的组织可能使用corporate proxy 访问互联网。如果是这种情况 - 您将无法使用 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)