首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何解决windows8中的java 7 FTP传输?

如何解决windows8中的java 7 FTP传输?
EN

Stack Overflow用户
提问于 2013-07-29 21:18:58
回答 1查看 255关注 0票数 0

我使用的是Windows8 x86和jdk_7,下面的代码编译得很好,没有错误。当我运行它的时候,它给了我这样的异常:

代码语言:javascript
复制
javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;
   nested exception is:
java.net.SocketException: Permission denied: connect

现在,作为一种变通方法,我尝试添加以下行:

代码语言:javascript
复制
System.setProperty("java.net.preferIPv4Stack", "true");

这并没有解决问题,在互联网上研究了几个小时后,我注意到问题出在jdk_7和防火墙问题上。我甚至试过这个命令

代码语言:javascript
复制
netsh advfirewall set global StatefulFTP disable

这也不能解决问题。

代码语言:javascript
复制
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

public class SendFileEmail
{
  public static void main(String [] args)
  {
     System.setProperty("java.net.preferIPv4Stack", "true");
  // Recipient's email ID needs to be mentioned.
     String to = "tmadile@gmail.com";

  // Sender's email ID needs to be mentioned
     String from = "hareitse@gmail.com";

  // Assuming you are sending email from localhost
     String host = "localhost";

  // Get system properties
     Properties properties = System.getProperties();

  // Setup mail server
     properties.setProperty("mail.smtp.host", host);

  // Get the default Session object.
     Session session = Session.getDefaultInstance(properties);

     try{
     // Create a default MimeMessage object.
        MimeMessage message = new MimeMessage(session);

     // Set From: header field of the header.
        message.setFrom(new InternetAddress(from));

     // Set To: header field of the header.
        message.addRecipient(Message.RecipientType.TO,
                              new InternetAddress(to));

     // Set Subject: header field
        message.setSubject("This is the Subject Line!");

     // Create the message part 
        BodyPart messageBodyPart = new MimeBodyPart();

     // Fill the message
        messageBodyPart.setText("This is message body");

     // Create a multipar message
        Multipart multipart = new MimeMultipart();

     // Set text message part
        multipart.addBodyPart(messageBodyPart);

     // Part two is attachment
        messageBodyPart = new MimeBodyPart();
        String filename = "shh.jpg";
        DataSource source = new FileDataSource(filename);
        messageBodyPart.setDataHandler(new DataHandler(source));
        messageBodyPart.setFileName(filename);
        multipart.addBodyPart(messageBodyPart);

     // Send the complete message parts
        message.setContent(multipart );

     // Send message
        Transport.send(message);
        System.out.println("Sent message successfully....");
     }
        catch (MessagingException mex) {
           mex.printStackTrace();
        }
  }
}
EN

回答 1

Stack Overflow用户

发布于 2013-07-29 21:34:46

代码语言:javascript
复制
// Assuming you are sending email from localhost
String host = "localhost";
// Setup mail server
properties.setProperty("mail.smtp.host", host);

从我阅读您的代码和您的错误的方式来看,您似乎更像是试图从本地主机作为主机发送邮件,而实际上您正在尝试发送到位于端口25的本地主机的邮件服务器。如果您没有在本地主机上运行任何SMTP服务器,您将收到上述错误。您提供的代码不会为您启动任何SMTP服务器。您可能应该将host = "localhost“替换为您的smtp主机(可能还有其他设置,具体取决于您的smtp提供商需要什么)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17925170

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档