如何在不转到外部应用程序的情况下直接发送带有附件的电子邮件



我有这个简单的代码和另一个jar库,使我能够在不去其他邮件应用程序的情况下发送电子邮件。

 public class claimrewardemail extends Activity {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.successful);
    final String username = "myname@gmail.com"; 
    final String password = "mypassword"; 

    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                Properties props = new Properties();
                props.put("mail.smtp.auth", "true");
                props.put("mail.smtp.starttls.enable", "true");
                props.put("mail.smtp.host", "smtp.gmail.com");
                props.put("mail.smtp.port", "587");
                Session session = Session.getInstance(props,
                        new javax.mail.Authenticator() {
                            protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
                                return new javax.mail.PasswordAuthentication(
                                        username, password);
                            }
                        });
                // TODO Auto-generated method stub
                Message message = new MimeMessage(session);
                message.setFrom(new InternetAddress("from-email@gmail.com"));
                message.setRecipients(Message.RecipientType.TO,
                        InternetAddress.parse("youremail@gmail.com")); 
                message.setSubject("email");
                message.setText("HI,"
                        + "nn great");
                Transport.send(message);
                System.out.println("Done");

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

上面的代码帮助我直接用硬编码的电子邮件、标题和消息发送邮件,现在我想添加附件部分,我可以在手机中访问我的文件,并添加一个按钮,我可以将附件与此邮件一起发送。有人能帮我吗?

试试这个:

    String filename = "example_filename";
    Multipart _multipart = new MimeMultipart();
    BodyPart messageBodyPart = new MimeBodyPart(); 
    DataSource source = new FileDataSource(filename);
    messageBodyPart.setDataHandler(new DataHandler(source)); 
    messageBodyPart.setFileName(filename); 
    _multipart.addBodyPart(messageBodyPart); 
    message.setContent(_multipart);

编辑:

当您包含aFileChooser库时,您应该在活动布局中创建两个按钮("发送电子邮件"one_answers"选择文件"),然后在活动中创建:

private Button sendEmail;
private Button chooseFileButton;
private String filename;
private static final int REQUEST_CHOOSER = 1234;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    sendEmail = (Button) findViewById(R.id.send_email_button_id);
    sendEmail.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                    Properties props = new Properties();
                    props.put("mail.smtp.auth", "true");
                    props.put("mail.smtp.starttls.enable", "true");
                    props.put("mail.smtp.host", "smtp.gmail.com");
                    props.put("mail.smtp.port", "587");
                    Session session = Session.getInstance(props,
                        new javax.mail.Authenticator() {
                            protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
                                return new javax.mail.PasswordAuthentication(
                                    username, password);
                        }
                    });
                    // TODO Auto-generated method stub
                    Message message = new MimeMessage(session);
                    message.setFrom(new InternetAddress("from-email@gmail.com"));
                    message.setRecipients(Message.RecipientType.TO,
                        InternetAddress.parse("youremail@gmail.com")); 
                    message.setSubject("email");
                    message.setText("HI,"
                        + "nn great");
                    if (!"".equals(filename)) {
                        Multipart _multipart = new MimeMultipart();
                        BodyPart messageBodyPart = new MimeBodyPart(); 
                        DataSource source = new FileDataSource(filename);
                        messageBodyPart.setDataHandler(new DataHandler(source)); 
                        messageBodyPart.setFileName(filename); 
                        _multipart.addBodyPart(messageBodyPart); 
                        message.setContent(_multipart);
                    }
                    Transport.send(message);
                    System.out.println("Done");
        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
    }
}).start();
        }
    });
    chooseFileButton = (Button) findViewById(R.id.choose_file_button_id);
    chooseFileButton.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              // Create the ACTION_GET_CONTENT Intent
              Intent getContentIntent = FileUtils.createGetContentIntent();
              Intent intent = Intent.createChooser(getContentIntent, "Select a file");
              startActivityForResult(intent, REQUEST_CHOOSER);
          }
    });
}

之后添加onActivityResult方法,在那里您可以获得选定的文件名:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
        case REQUEST_CHOOSER:   
            if (resultCode == RESULT_OK) {
                final Uri uri = data.getData();
                // Get the File path from the Uri
                filename = FileUtils.getPath(this, uri);
            }
            break;
    }
}

不要忘记将FileChooserActivity添加到您的AndroidManifest.xml:

<activity
    android:name="com.ipaulpro.afilechooser.FileChooserActivity"
    android:icon="@drawable/ic_launcher"
    android:enabled="true"
    android:exported="true"
    android:label="@string/app_name" >
    <intent-filter>
        <action android:name="android.intent.action.GET_CONTENT" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.OPENABLE" />
        <data android:mimeType="*/*" />
    </intent-filter>
</activity>

之后,您可以通过点击"发送"按钮发送电子邮件;

是的,如果你在代码中添加mail.jar和activation.jar文件,请搜索一个按钮。我想当我们点击本地磁盘中打开的选择文件的新窗口时,你可能知道这不是正常的按钮,选择后,你只需要将其分配给代码中的路径,这样电子邮件就可以随附件发送。

相关内容

  • 没有找到相关文章

最新更新