2013-10-10 73 views
0

我只想发送一个图像附件与gmail的意图。以下代码完美适用于所有4.0+设备,但在我的motorola razr 2.3上失败。为什么???我如何让它在4.0设备下工作?如何在android 2.3设备上附件发送电子邮件?

我有以下代码:

public class SimpleEmailAttachmentActivity extends Activity { 
private static final String sFileName = "myfile.png"; 
private Bitmap bitmap; 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_simple_email_attachment); 

    final Button button = (Button) findViewById(R.id.email_snapshot_button); 
    button.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View paramView) { 
      View rootView = button.getRootView(); 
      if(rootView != null) { 
       rootView.setDrawingCacheEnabled(true); 
       Bitmap drawingCache = rootView.getDrawingCache(); 
       bitmap = Bitmap.createBitmap(drawingCache); 
       rootView.setDrawingCacheEnabled(false); 
      } 

      saveToInternalStorage(bitmap); 

      File file = new File(getFilesDir() + File.separator + sFileName); 
      Uri uri = Uri.fromFile(file); 
      Intent intent = new Intent(Intent.ACTION_SEND); 
      intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"[email protected]"}); 
      intent.putExtra(Intent.EXTRA_SUBJECT, "email subject here"); 
      intent.putExtra(Intent.EXTRA_TEXT, "email body here"); 
      intent.putExtra(Intent.EXTRA_STREAM, uri); 
      intent.setType("plain/text"); 

      startActivity(intent);    
     } 
     }); 
    } 

private void saveToInternalStorage(Bitmap bitmap){ 
    FileOutputStream fos; 
    try { 
     fos = openFileOutput(sFileName, Context.MODE_WORLD_READABLE); 
     bitmap.compress(Bitmap.CompressFormat.PNG, 90, fos); 
     fos.close(); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 
} 

回答

0

用这个例子带有附件的电子邮件

public class MailImageFile extends javax.mail.Authenticator { 

      public MailImageFile(){} 

    public void Mail(String user, String pass) { 
     Properties props = new Properties(); 
     props.put("mail.smtp.host", "smtp.gmail.com"); 
     props.put("mail.smtp.socketFactory.port", "465"); 
     props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); 
     props.put("mail.smtp.auth", "true"); 
     props.put("mail.smtp.port", "465"); 

     Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator()   { 
      protected PasswordAuthentication getPasswordAuthentication() { 
       return new PasswordAuthentication("[email protected]", "pqr123%"); 
      } 
      }); 
     try { 
      Message message = new MimeMessage(session); 
      message.setFrom(new InternetAddress("[email protected]")); 
      message.setRecipients(Message.RecipientType.TO,  InternetAddress.parse("[email protected]")); 
    message.setContent(_multipart); 
      message.setSubject("Testing Subject"); 
      message.setContent("Hi...", "text/html; charset=utf-8"); 

      Transport.send(message); 

     } catch (MessagingException e) { 
      throw new RuntimeException(e); 
    } 

//得到这个解决方案的形式在这里

private Multipart _multipart; 
_multipart = new MimeMultipart(); 

public void addAttachment(String filename,String subject) throws Exception { 
    BodyPart messageBodyPart = new MimeBodyPart(); 
    DataSource source = new FileDataSource(filename); 
    messageBodyPart.setDataHandler(new DataHandler(source)); 
    messageBodyPart.setFileName(filename); 
    _multipart.addBodyPart(messageBodyPart); 

    BodyPart messageBodyPart2 = new MimeBodyPart(); 
    messageBodyPart2.setText(subject); 

    _multipart.addBodyPart(messageBodyPart2); 
} 


    }