从Android发送电子邮件时附加图像

时间:2014-01-28 12:44:15

标签: android image email attachment

我设法在我的Android应用中发送了一封电子邮件;但是,我不知道如何在我的电子邮件中附加图像。 我该怎么办?

 public MailSender(String user, String password, final Context context) {   
    this.user = user;   
    this.password = password;   
    this.context = context;

    this.bckMng = BackgroundManager.getInstance(context);


    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, new javax.mail.Authenticator() {        
         protected PasswordAuthentication getPasswordAuthentication() {   
             String userName = context.getResources().getString(R.string.source_username);
             String password = context.getResources().getString(R.string.source_password);
             return new PasswordAuthentication(userName, password);
             }
        }); 
}   

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

在this.bckmng.getImages()中,我可以获得图片......

3 个答案:

答案 0 :(得分:0)

使用以下方法发送图像。

            Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
    emailIntent.setType("png/image");
    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] {
            "//email-id" });
    emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
    emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, message);
    Uri uri = Uri.fromFile(new File(Environment
            .getExternalStorageDirectory(), "/saved_images/MyImage.png")); // Image path
    emailIntent.putExtra(Intent.EXTRA_STREAM, uri);
    emailIntent.setType("text/plain");
    startActivity(emailIntent);

答案 1 :(得分:0)

这是我现在发送消息的方式:

protected String doInBackground(String... params) {

    String immediatlyMessage = params[0];
    try {      
        Message message = new MimeMessage(session);    
        message.setFrom(new InternetAddress(context.getResources().getString(R.string.source_username))); 

        EmailAddressDataSource eads = new EmailAddressDataSource(context);
        eads.open();
        List<EmailAddress> emailsList = eads.getAllEmails();
        eads.close();
        String emailAdress = "";
        for(EmailAddress address: emailsList){
            emailAdress += address.getAddress();
            emailAdress+=",";
        }
        //message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(emailAdress)); 
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("yoyoyo@yoyo.com")); 
        message.setSubject("Hey");          
        message.setContent(immediatlyMessage, "text/html; charset=utf-8");


    Transport.send(message);                  
    }
    catch (MessagingException e) {        
        e.printStackTrace();
    }        
    catch (Exception e) {            
        e.printStackTrace();
    }    
    return null;    
}    

我在这里使用意图吗?用哪种方法?

答案 2 :(得分:0)

试试这个,但它可能不适用于模拟器,但它适用于真正的手机

Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);

    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, emailaddress);
    emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, message);
    emailIntent.setType("image/png");

    ArrayList<Uri> uris = new ArrayList<Uri>();

    uris.add(Uri.parse("android.resource://" + getPackageName() + "/" + R.drawable.file1));
    uris.add(Uri.parse("android.resource://" + getPackageName() + "/" + R.drawable.file2));

    emailIntent.putExtra(Intent.EXTRA_STREAM, uris));

    startActivity(emailIntent);