无法使用Java Mail发送附件

时间:2012-07-15 11:40:40

标签: android javamail

发生什么事了?以前工作正常,但现在出错。

 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)

1 个答案:

答案 0 :(得分:1)

看起来像连接错误...请检查您的网络状态,防火墙等。