[英]send email phpmailer and javax.mail
我有两个类似的代码部分,分别用java和php编写。 由于证书错误,PHP没有发送电子邮件-
Connection failed. Error #2: stream_socket_enable_crypto():
Peer certificate CN=*.hosting.com' did not match
expected CN=smtp.anotherhosting.com'
但是java代码发送电子邮件没有任何问题,我不明白为什么。 (我到处都有问题- 如何使用Java跳过ssl检查?
这是代码:
的PHP:
<?php
require './PHPMailer.php';
require './SMTP.php';
use PHPMailer\PHPMailer\PHPMailer;
$mail = new PHPMailer(true);
try {
$mail->SMTPDebug = 4;
$mail->isSMTP();
$mail->Host = 'smtp.anotherhosting.com';
$mail->SMTPAuth = true;
$mail->Username = 'username@anotherhosting.com';
$mail->Password = 'password';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
//Recipients
$mail->setFrom('from@company.com');
$mail->addAddress('myemail@company.com');
$mail->isHTML(true);
$mail->Subject = 'Here is the subject12';
$mail->Body = 'This is the HTML message bo22dy <b>in bold!</b>';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
try {
$mail->smtpClose();
} catch (Exception $e) {
echo $e->getTraceAsString();
}
和Java:
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
public class Main {
public static void main(String[] args) {
final String username = "username@anotherhosting.com";
final String password = "password";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.anotherhosting.com");
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
session.setDebug(true);
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("from@company.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("myemail@company.com"));
message.setSubject("Testing Subject");
message.setText("Dear Mail Crawler,"
+ "\n\n No spam to my email, please!");
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
我的任务是使用php代码实现电子邮件发送功能。 从我当前的角度来看,它由于smtp从一台主机重定向到另一台主机而失败。 phpmailer最有可能获得了host1,接收到host2的重定向,从host2获得证书并将此证书与host1进行比较。 同时,java客户端可以做的很好。 如果有人知道如何解决此类问题,请告诉我。
另外,当php代码尝试调用stream_socket_enable_crypto时,它在402行失败。
这是日志文件:java:
DEBUG: setDebug: JavaMail version 1.4ea
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.anotherhosting.com", port 587, isSSL false
220 mailpod.hosting.com ESMTP
DEBUG SMTP: connected to host "smtp.anotherhosting.com", port: 587
EHLO degr [most probably my computer name]
250-mailpod.hosting.com
250-STARTTLS
250-PIPELINING
250-8BITMIME
250-SIZE 65000000
250 AUTH LOGIN PLAIN CRAM-MD5
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "SIZE", arg "65000000"
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN CRAM-MD5"
STARTTLS
220 ready for tls
EHLO degr
250-mailpod.hosting.com
250-PIPELINING
250-8BITMIME
250-SIZE 65000000
250 AUTH LOGIN PLAIN CRAM-MD5
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "SIZE", arg "65000000"
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN CRAM-MD5"
DEBUG SMTP: Attempt to authenticate
AUTH LOGIN
334 [auth hash here]
[auth hash here]
334 [auth hash here]
[auth hash here]
235 ok, go ahead (#2.0.0)
DEBUG SMTP: use8bit false
MAIL FROM:<from@company.com>
250 ok
RCPT TO:<myemail@company.com>
250 ok
DEBUG SMTP: Verified Addresses
DEBUG SMTP: myemail@company.com
DATA
354 go ahead
From: from@company.com
To: myemail@company.com
Message-ID: <1338668845.01537892151523.JavaMail.myemail@company.com>
Subject: Testing Subject
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Dear Mail Crawler,
No spam to my email, please!
.
250 ok 1537892155 qp 173024
QUIT
221 mailpod.hosting.com
Done
的PHP:
2018-09-25 16:26:35 Connection: opening to smtp.anotherhosting.com:587, timeout=300, options=array()
2018-09-25 16:26:35 Connection: opened
2018-09-25 16:26:35 SMTP INBOUND: "220 mailpod.hosting.com ESMTP"
2018-09-25 16:26:35 SERVER -> CLIENT: 220 mailpod.hosting.com ESMTP
2018-09-25 16:26:37 CLIENT -> SERVER: EHLO localhost
2018-09-25 16:26:38 SMTP INBOUND: "250-mailpod.hosting.com"
2018-09-25 16:26:38 SMTP INBOUND: "250-STARTTLS"
2018-09-25 16:26:38 SMTP INBOUND: "250-PIPELINING"
2018-09-25 16:26:38 SMTP INBOUND: "250-8BITMIME"
2018-09-25 16:26:38 SMTP INBOUND: "250-SIZE 65000000"
2018-09-25 16:26:38 SMTP INBOUND: "250 AUTH LOGIN PLAIN CRAM-MD5"
2018-09-25 16:26:38 SERVER -> CLIENT: 250-mailpod.hosting.com250-STARTTLS250-PIPELINING250-8BITMIME250-SIZE 65000000250 AUTH LOGIN PLAIN CRAM-MD5
2018-09-25 16:26:38 CLIENT -> SERVER: STARTTLS
2018-09-25 16:26:38 SMTP INBOUND: "220 ready for tls"
2018-09-25 16:26:38 SERVER -> CLIENT: 220 ready for tls
2018-09-25 16:26:38 Connection failed. Error #2: stream_socket_enable_crypto(): Peer certificate CN=*.hosting.com' did not match expected CN=smtp.anotherhosting.com' [C:\project\SMTP.php line 402]
SMTP Error: Could not connect to SMTP host.
2018-09-25 16:26:39 CLIENT -> SERVER: QUIT
2018-09-25 16:26:39
2018-09-25 16:26:39
2018-09-25 16:26:39
2018-09-25 16:26:39 Connection: closed
SMTP Error: Could not connect to SMTP host.
Message could not be sent. Mailer Error: SMTP Error: Could not connect to SMTP host.
由https://www.networksolutions.com/提供的PS smpt服务
对。 因此,您的Java代码有一个错误,该错误使中间人攻击成功。 在PHP中,通过完全按照TLS的设计来成功防止这种情况。
发生的是您的ISP防火墙处的TCP重定向,这对两个客户端都是不可见的。 您可以禁用证书检查(如故障排除指南中所述 ),但实际上不应该这样做。 显式连接到正确的名称( mailpod.hosting.com
),或者使用不干扰您流量的托管服务提供商。
这可能是您的临时解决方案。
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
您可以通过PHPMailer 5.2.10中引入的SMTPOptions属性允许不安全的连接(可以通过对早期版本中的SMTP类进行子类化来实现此目的), 尽管不 建议这样做 , 因为这样 做会破坏很多使用安全 传输 的点 。
有关SMTPOptions
更多信息,请SMTPOptions
Wiki。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.