2012-07-15 64 views
0

发生了什么事?以前工作好,但现在给出了一个错误。无法使用Java Mail发送附件

public Send(String user, String password) 
    { 
     this.user = user; 
     this.password = password; 
     Properties props = new Properties(); 
     props.setProperty("mail.transport.protocol", "smtp"); 
     props.setProperty("mail.host", mailhost); 
     props.put("mail.smtp.auth", "true"); 
     props.put("mail.smtp.port", "465"); 
     props.put("mail.smtp.socketFactory.port", "465"); 
     props.put("mail.smtp.so cketFactory.class","javax.net.ssl.SSLSocketFactory"); 
     props.put("mail.smtp.socketFactory.fallback", "false"); 
     props.setProperty("mail.smtp.quitwait", "false"); 
     session = Session.getDefaultInstance(props, this); 
    } 

    protected PasswordAuthentication getPasswordAuthentication() 
    { 
     return new PasswordAuthentication(user, password); 
    } 

public void sendMail (final String recipients, final String FileName) 
    { 
     Thread SendThread = new Thread() 
     { 
      public void run() 
      {     
       try 
       {    
        //String SSS = FileName.replace("+", "");     
        //String Fname = new File (SSS).getName(); 

        String Fname = new File (FileName).getName(); 
        String subject = Fname.substring(0, 9);  
        Log.e("!!!!!!!!!!!!!!!!!!!!!! ", Fname); 

        String RusFname = new String (Fname.getBytes()); 
        String sender = "_X_X_X_"; 
        MimeMessage message = new MimeMessage(session); 
        message.setSender(new InternetAddress(sender)); 
        message.setSubject(subject);    
        MimeBodyPart attachmentPart = new MimeBodyPart();      
        FileDataSource fileDataSource = new FileDataSource(FileName) 
        { 
         @Override 
         public String getContentType() 
         { 
          return "application/octet-stream"; 
         } 
        }; 
        attachmentPart.setDataHandler(new DataHandler(fileDataSource));        
        attachmentPart.setFileName(MimeUtility.encodeText(RusFname));    
        Multipart multipart = new MimeMultipart(); 
        multipart.addBodyPart(attachmentPart);    
        message.setContent(multipart);   
        if (recipients.indexOf(',') > 0) 
         message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients)); 
        else 
         message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients)); 
        Transport.send(message); 
        File f = new File(FileName); 
        Log.e("!!!!!!!!!!!!!!!!!!!!!! ", "Sended"); 
        f.delete();     
       } 
       catch (MessagingException ex) 
       { 
        ex.printStackTrace(); 
       } catch (UnsupportedEncodingException e) 
       { 
        e.printStackTrace(); 
       } 
      } 
     }; 
     SendThread.start(); 
    } 
07-15 15:32:31.849: WARN/System.err(10287): javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465, response: -1 
07-15 15:32:31.849: WARN/System.err(10287):  at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1379) 
07-15 15:32:31.849: WARN/System.err(10287):  at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:412) 
07-15 15:32:31.849: WARN/System.err(10287):  at javax.mail.Service.connect(Service.java:310) 
07-15 15:32:31.854: WARN/System.err(10287):  at javax.mail.Service.connect(Service.java:169) 
+0

我不知道如果这是你的意图或没有,但你真的需要使用Java发送电子邮件邮件,而不是Intent.ACTION_SEND?最后一个使用起来更容易和更快速。 – yugidroid 2012-07-15 12:20:44

回答

1

它看起来像连接错误...请检查你的网络状况,防火墙等