小编Sur*_*rez的帖子

TLS上的Java Mail

我试图通过TLS连接从我的程序发送电子邮件.这是我的代码

    final String username = "XXXXXX";
    final String password = "XXXXX"; 
    Properties props = new Properties();
    props.put("mail.transport.protocol", "smtp");
    props.put("mail.smtp.starttls.enable","true");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.host", "mail.xxxx.com");
    props.put("mail.smtp.port", "587");

    Session session = Session.getInstance(props,
            new javax.mail.Authenticator() {
              protected PasswordAuthentication getPasswordAuthentication() {
                  return new PasswordAuthentication(username, password);
              }
            });

    Message message = new MimeMessage(session);
    message.setFrom(new InternetAddress("from@xxxx.com"));
    message.setRecipients(Message.RecipientType.TO,
        InternetAddress.parse(to_address));
    message.setSubject("Test Mail"); 
    message.setText("TestMail ");
    Transport.send(message)
Run Code Online (Sandbox Code Playgroud)

我的电子邮件网关具有启用了SSL的传入邮件设置,并在端口587上启用了TLS传出.我能够在Outlook中配置此设置并且它正常工作.但通过我的java程序,它说"拒绝连接".帮助感谢!

最后工作:

我使用InstallCert程序导入certicate以生成jssecacerts文件,然后将文件添加到my/jre/lib/security/path.这是我的工作代码

    properties.put("mail.transport.protocol", "smtp");
    properties.put("mail.smtp.host", "XXXXXX");  
    properties.put("mail.smtp.port", "465"); 
    properties.put("mail.smtp.ssl.enable", true);
    properties.put("mail.smtp.socketFactory.port", "465");
    properties.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
    properties.put("mail.smtp.socketFactory.fallback", "false"); 
    properties.put("mail.smtp.quitwait", "false"); 
    properties.put("mail.smtp.auth", "true"); 
Run Code Online (Sandbox Code Playgroud)

java email ssl

7
推荐指数
1
解决办法
1万
查看次数

在Apache HttpClient中设置自定义文件名

我使用Apache HttpClient通过MultipartEntity上传文件,我需要上传不同文件名的文件..下面是我的代码...

FileBody uploadFilePart = new FileBody(binaryFile);
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("bin", uploadFilePart);
reqEntity.addPart("comment", comment);
httpPost.setEntity(reqEntity);

HttpResponse response = httpclient.execute(httpPost);
HttpEntity resEntity = response.getEntity();

System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
if (resEntity != null) {
    System.out.println("Response content length: " +
                       resEntity.getContentLength());
}
EntityUtils.consume(resEntity);
Run Code Online (Sandbox Code Playgroud)

帮助感谢!

谢谢,Surez

java httpclient apache-httpcomponents

2
推荐指数
1
解决办法
1838
查看次数

标签 统计

java ×2

apache-httpcomponents ×1

email ×1

httpclient ×1

ssl ×1