以下是我发送电子邮件的代码:
Properties props = new Properties();
props.put("mail.smtp.host", host);
props.put("mail.debug", "false");
final Session session = Session.getInstance(props);
final Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
msg.setRecipients(Message.RecipientType.TO, toAddress);
msg.setSubject(subject);
msg.setSentDate(new Date());
Multipart multipart = new MimeMultipart("related");
BodyPart mbp1 = new MimeBodyPart();
mbp1.setContent(body, "text/html");
multipart.addBodyPart(mbp1);
Transport.send(msg);
Run Code Online (Sandbox Code Playgroud)
错误堆栈跟踪:
javax.mail.NoSuchProviderException: smtp
at javax.mail.Session.getService(Session.java:764)
at javax.mail.Session.getTransport(Session.java:689)
at javax.mail.Session.getTransport(Session.java:632)
at javax.mail.Session.getTransport(Session.java:612)
at javax.mail.Session.getTransport(Session.java:667)
at javax.mail.Transport.send0(Transport.java:154)
at javax.mail.Transport.send(Transport.java:80)
Run Code Online (Sandbox Code Playgroud)
注意:
- 如果作为桌面应用程序执行,相同的代码.但是在tomcat上部署时抛出异常.
- 最新的mail.jar和smtp.jar被添加到库中.
- SMTP主机地址也是正确的.
如果有人可以指点我会有所帮助.
我经历了许多类似的话题,但没有运气!!
我想使用 PEM 文件生成公钥和私钥。以下是我使用的代码:
String pemFileNme = "C:\\Users\\amitmm\\Desktop\\clean\\key.pem";
File pubKeyFile = new File(pemFileNme);
File privKeyFile = new File(pemFileNme);
// read public key DER file
DataInputStream dis = new DataInputStream(new
FileInputStream(pubKeyFile));
byte[] pubKeyBytes = new byte[(int)pubKeyFile.length()];
dis.readFully(pubKeyBytes);
dis.close();
// read private key DER file
dis = new DataInputStream(new FileInputStream(privKeyFile));
byte[] privKeyBytes = new byte[(int)privKeyFile.length()];
dis.read(privKeyBytes);
dis.close();
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
// decode public key
X509EncodedKeySpec pubSpec = new X509EncodedKeySpec(pubKeyBytes);
RSAPublicKey pubKey = (RSAPublicKey)
keyFactory.generatePublic(pubSpec);
// decode private key
PKCS8EncodedKeySpec privSpec = …
Run Code Online (Sandbox Code Playgroud)