[英]Could not connect to SMTP host: x.x.x.x, port: 25 in java
I cannot send email in Java, it throw Exception: Could not connect to SMTP host: xxxx, port: 25. 我无法使用Java发送电子邮件,它引发异常:无法连接到SMTP主机:xxxx,端口:25。
I telnet xxxx 25 OK and I sent email successfully in php code with the same host, port, user & password, so I don't think this issue due to network or firewall problems. 我使用telnet xxxx 25,然后以相同主机,端口,用户和密码的php代码成功发送了电子邮件,因此我不认为由于网络或防火墙问题而导致此问题。
In testing, I sent email OK in java with gmail server (smtp.gmail.com :465), but not with host: xxxx port 25. 在测试中,我使用gmail服务器(smtp.gmail.com:465)在Java中以电子邮件发送了OK,但未通过主机:xxxx端口25发送电子邮件。
This is my java code: HOST=xxxx 这是我的Java代码:HOST = xxxx
Properties mailServerProperties = System.getProperties();
mailServerProperties.put("mail.smtp.user", USER);
mailServerProperties.put("mail.smtp.host", HOST);
mailServerProperties.put("mail.smtp.port", 25);
mailServerProperties.put("mail.smtp.debug", "true");
mailServerProperties.put("mail.smtp.auth", "true");
mailServerProperties.put("mail.smtp.starttls.enable", "true");
mailServerProperties.put("mail.smtp.socketFactory.port", 25);
mailServerProperties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
javax.mail.Session getMailSession = javax.mail.Session.getDefaultInstance(mailServerProperties);
MimeMessage generateMailMessage = new MimeMessage(getMailSession);
generateMailMessage.setHeader("Content-Type", "text/plain; charset=UTF-8");
generateMailMessage.setSubject(subject, "UTF-8");
generateMailMessage.setFrom(new InternetAddress(ApiConstant.EMAIL, fromEmail, "UTF-8"));
generateMailMessage.addRecipients(Message.RecipientType.TO, toAddress);
generateMailMessage.addRecipients(Message.RecipientType.CC, ccAddress);
generateMailMessage.addRecipients(Message.RecipientType.BCC, bccAddress);
Multipart multipart = new MimeMultipart();
MimeBodyPart messagePart = new MimeBodyPart();
messagePart.setContent(content, "text/html; charset=UTF-8");
multipart.addBodyPart(messagePart);
generateMailMessage.setContent(multipart);
logger.debug("Create transport");
Transport transport = getMailSession.getTransport("smtp");
transport.connect(HOST, 25, USER, PASS);
transport.sendMessage(generateMailMessage, generateMailMessage.getAllRecipients());
transport.close();
This is my php code, it worked. 这是我的php代码,它起作用了。
$mail = new PHPMailer();
$mail->Host = 'x.x.x.x';
$mail->Port = '25';
$mail->SMTPAuth = true;
$mail->Username = USER;
$mail->Password = PASS;
$mail->From = '.....';
$mail->FromName = '.....';
$mail->setLanguage('vi');
$mail->isHTML(true);
$mail->CharSet = 'UTF-8';
$mail->addAttachment(FILE_NAME);
$mail->addAddress('....');
$mail->Subject ="test";
$mail->Body = "test";
$mail->Body = $body;
Plz help me. 请帮我。
On some networks, IPv6 may be problematic. 在某些网络上,IPv6可能会出现问题。 Try running your app with the VM option
尝试使用VM选项运行您的应用
-Djava.net.preferIPv4Stack=true
ie 即
java -Djava.net.preferIPv4Stack=true -cp . MyClass
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.