- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试编写一个测试,它将使用 JavaMail SMTP 或 SMTPS 通过 SSL 和身份验证将消息发送到 Greenmail我正在运行的服务器。
我已经写了一个快速的小子,它应该完全符合我的要求:
import java.util.Properties;
import javax.mail.Session;
import javax.mail.Transport;
import com.icegreen.greenmail.user.GreenMailUser;
import com.icegreen.greenmail.util.GreenMail;
public class MailTest {
public static void main(String[] args) throws Exception {
GreenMail greenmail = new GreenMail();
try {
greenmail.start();
String email = "foo@bar";
String userid = "user";
String password = "pa$$word";
GreenMailUser setUser = greenmail.setUser(email, userid, password);
setUser.create();
GreenMailUser user = greenmail.getManagers().getUserManager().getUser(userid);
System.out.println("User created:" + user.getEmail() + ":" + user.getLogin() + ":" + user.getPassword());
int portSmtps = greenmail.getSmtps().getPort();
System.out.println("Smtps started on port:" + portSmtps);
Properties props = new Properties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", "localhost");
props.put("mail.smtp.socketFactory.port", portSmtps);
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
Session session = Session.getInstance(props);
Transport transport = session.getTransport("smtp");
transport.connect("localhost", portSmtps, userid, password);
System.out.println("Transport is connected: " + transport.isConnected());
} finally {
greenmail.stop();
}
}
}
我的问题是我遇到了几个异常:
Exception in thread "Thread-7" java.lang.RuntimeException: javax.net.ssl.SSLException: Connection has been shutdown: javax.net.ssl.SSLHandshakeException: Received fatal alert: certificate_unknown
at com.icegreen.greenmail.smtp.SmtpHandler.run(Unknown Source)
Caused by: javax.net.ssl.SSLException: Connection has been shutdown: javax.net.ssl.SSLHandshakeException: Received fatal alert: certificate_unknown
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.checkEOF(SSLSocketImpl.java:1293)
at com.sun.net.ssl.internal.ssl.AppInputStream.read(AppInputStream.java:65)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:264)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:306)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158)
at java.io.InputStreamReader.read(InputStreamReader.java:167)
at java.io.BufferedReader.fill(BufferedReader.java:136)
at java.io.BufferedReader.readLine(BufferedReader.java:299)
at java.io.BufferedReader.readLine(BufferedReader.java:362)
at com.icegreen.greenmail.smtp.SmtpConnection.readLine(Unknown Source)
at com.icegreen.greenmail.smtp.SmtpHandler.handleCommand(Unknown Source)
... 1 more
Caused by: javax.net.ssl.SSLHandshakeException: Received fatal alert: certificate_unknown
at com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Alerts.java:174)
at com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Alerts.java:136)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.recvAlert(SSLSocketImpl.java:1720)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:954)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1138)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.writeRecord(SSLSocketImpl.java:632)
at com.sun.net.ssl.internal.ssl.AppOutputStream.write(AppOutputStream.java:59)
at sun.nio.cs.StreamEncoder.writeBytes(StreamEncoder.java:202)
at sun.nio.cs.StreamEncoder.implFlushBuffer(StreamEncoder.java:272)
at sun.nio.cs.StreamEncoder.implFlush(StreamEncoder.java:276)
at sun.nio.cs.StreamEncoder.flush(StreamEncoder.java:122)
at java.io.OutputStreamWriter.flush(OutputStreamWriter.java:212)
at java.io.BufferedWriter.flush(BufferedWriter.java:236)
at java.io.PrintWriter.flush(PrintWriter.java:276)
at com.icegreen.greenmail.util.InternetPrintWriter.println(Unknown Source)
at com.icegreen.greenmail.util.InternetPrintWriter.println(Unknown Source)
at com.icegreen.greenmail.smtp.SmtpConnection.println(Unknown Source)
at com.icegreen.greenmail.smtp.SmtpHandler.sendGreetings(Unknown Source)
... 1 more
Exception in thread "main" javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 3465;
nested exception is:
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1934)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:638)
at javax.mail.Service.connect(Service.java:295)
at MailTest.main(MailTest.java:35)
Caused by: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Alerts.java:174)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1649)
at com.sun.net.ssl.internal.ssl.Handshaker.fatalSE(Handshaker.java:241)
at com.sun.net.ssl.internal.ssl.Handshaker.fatalSE(Handshaker.java:235)
at com.sun.net.ssl.internal.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1206)
at com.sun.net.ssl.internal.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:136)
at com.sun.net.ssl.internal.ssl.Handshaker.processLoop(Handshaker.java:593)
at com.sun.net.ssl.internal.ssl.Handshaker.process_record(Handshaker.java:529)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:893)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1138)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1165)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1149)
at com.sun.mail.util.SocketFetcher.configureSSLSocket(SocketFetcher.java:507)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:238)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1900)
... 3 more
Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:323)
at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:217)
at sun.security.validator.Validator.validate(Validator.java:218)
at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:126)
at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:209)
at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:249)
at com.sun.net.ssl.internal.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1185)
... 13 more
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:174)
at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:238)
at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:318)
... 19 more
我试过在这里查看其他一些答案,但我一定遗漏了一些东西:
抱歉发了这么长的帖子,提前感谢您的任何建议。
最佳答案
您应该像这样启动您的 GreenMail 服务器:
Security.setProperty("ssl.SocketFactory.provider", DummySSLSocketFactory.class.getName());
GreenMail mailServer = new GreenMail(ServerSetupTest.SMTPS);
mailServer.start();
关于java - 将 Javamail 和 Greenmail 用于 SMTPS/SSL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6552829/
在发布之前,我在互联网上阅读了很多内容和建议的问题,但找不到任何适合我的内容。 有关使用 gmail 的 JavaMail 常见问题解答 Stackoverflow 问题 JavaMail 教程 Go
我正在尝试通过 java 邮件发送电子邮件。它给出了以下异常javax.mail.internet.ParseException: 在 Content-Type string 中,预期的 MIME 类
我想从我的 Gmail 帐户的特定文件夹中获取最近未读的具有特定主题的电子邮件。我正在使用 JavaMail API,如下所示,但它返回 0 个结果。但是,如果我只单独使用 subjectTerm,我
我正在开发一个基于 Spring 的网络应用程序,当用户输入他的电子邮件和密码进行注册时我想验证它们并向他发送电子邮件。我已经使用 java mail api并正确设置gmail smtp。 这是我的
我试图获取我的 Gmail 的所有邮件,但只能获取 250 封。根据 https://javaee.github.io/javamail/FAQ#gmailsettings ,我已经将邮件数量限制设置
我有几封主题为报告 #123:来自测试的“Email_test_856” 的电子邮件。我尝试使用 JavaMail API 根据关键字 Email_test 查找邮件,但它不会过滤掉任何邮件。 这是我
我有一种方法可以发送 HTML 电子邮件和嵌入的图片,以引用我的计算机中的图像,例如 c:/image.gif 该方法效果很好,但我不知道如何添加位于远程计算机中的图像,我知道您的 IP 地址和图像所
这是我的代码,它应该会收到未读消息。然而,它正在返回我收件箱中的最后一条消息。有什么建议吗? import javax.mail.*; import javax.mail.internet.*; im
Javax邮件版本使用1.6.2 手动设置 JavaMailSender 我尝试使用 mail.smtp.timeout 和 mail.smtps.timeout 超时。 而且,我尝试使用字符串和整数
我有以下问题:我的电子邮件客户端使用 Javamail,可以获取电子邮件的所有部分,如发送日期、主题、发件人、收件人等,但我不知道如何获取邮件正文. 我已经尝试过 getContent() 函数,但它
我编写了一个使用 JavaMail API 发送 gmail 的程序。以下是我的代码: import java.util.*; import javax.mail.*; import javax.ac
所以我的问题是关于JavaMail 的。我想向不同的电子邮件地址发送电子邮件,但我希望发件人类似于“noreply”。但是,我似乎无法这样做,因为 JavaMail 需要发送者和接收者都有实际的电子邮
我正在尝试从 Java 应用程序发送电子邮件,但每当我运行在线找到的代码时 here 当我运行代码时,我收到此错误: Exception in thread "main" java.lang.NoCl
所以我遇到了 Javamail 的问题,如果我在邮件中发送附件,正文就会消失。当我不随邮件发送附件时,我只能看到正文。 我的 GMailSender.java: public GMailSender(
我有一个关于 Java Mail 及其如何与流配合使用的问题。在 Java Mail 1.4.1 中,有一个 MimeMessage constructor that accepts a stream
我窃取了这段代码来测试使用 java 发送电子邮件的情况。显然,Javamail 是必需的。由于某种原因,我无法实现 javax.mail。我下载了最新的javamail并将它们放入jdk和jre l
我正在使用 JavaMail 开发一个邮件客户端,它运行良好。现在我希望能够处理传入消息的已读回执。如果传入的邮件 header 包含“Disposition-Notification-To”,我想处
Javamail 在 JUnit 测试和 GlassFish 3.1.2 中工作正常,但在 Tomcat 7.0.41 中不起作用:所有“From” header 均为 NULL!我究竟做错了什么?当
我尝试使用 javamail 连接到电子邮件 - 在非 Android 应用程序中,此代码工作正常: public void perform(View v){ Toast.makeText(g
我必须向多个收件人发送电子邮件。我不希望标题显示完整的收件人列表(即我没有使用“Message.RecipientType.CC”)。如果我使用“Message.RecipientType.BCC”,
我是一名优秀的程序员,十分优秀!