2011-10-22 62 views
0

我是編程新手。我不明白什麼是錯的。該代碼總是返回FALSE,但是該字母被髮送。注意「結果」變量。可能是我沒有正確描述它。提前致謝!結果總是假的

public class Send extends javax.mail.Authenticator 
{ 
    private String mailhost = "smtp.gmail.com"; 
    private String user; 
    private String password; 
    private Session session; 
    protected boolean result; 
    public String sss = ""; 
    public static final String LOG_TAG = "LOG"; 

    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.socketFactory.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 boolean sendMail (final String subject, 
           final String body, 
           final String sender, 
           final String recipients, 
           final String FileName) 
    { 
     Thread SendThread = new Thread() 
     { 
      public void run() 
      {     
       try 
       { 
        MimeMessage message = new MimeMessage(session); 
        DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain")); 

        message.setSender(new InternetAddress(sender)); 
        message.setSubject(subject);   
        message.setDataHandler(handler); 
        MimeBodyPart attachmentPart = new MimeBodyPart();  

        FileDataSource fileDataSource = new FileDataSource(FileName) 
        { 
         @Override 
         public String getContentType() 
         { 
          return "application/octet-stream"; 
         } 
        }; 
        attachmentPart.setDataHandler(new DataHandler(fileDataSource)); 

        String Fname = new File (FileName).getName(); 

        attachmentPart.setFileName(Fname); 

        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); 
        result = true; 
        Log.d(LOG_TAG, "SENDED"); 
       } 
       catch(Exception e) 
       { 
        result = false; 
        Log.d(LOG_TAG, "FAILED"); 
       } 
      } 
     }; 
     SendThread.start();   
     return result; 
    } 
    ...... 

回答

4
SendThread.start();   
    return result; 

你(可能)返回的result線程完成之前的值。如果你需要發送異步,你不能立即返回結果 - 它還沒有可用。如果您在發送完成時需要通知某人/某些東西,則需要在該線程中進行編碼。

+0

墊,非常感謝你! – XXX