1

我有一个邮件发送应用程序,它发送电子邮件和附件。除了附件部分外,这一切都很好。我正在使用此代码。但我不知道这段代码缺少什么或有什么问题。请帮忙。这是附件部分。

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

在我的主要活动中,我正在使用

public final static String APP_PATH_SD_CARD = "/Images_Book/";
String path = Environment.getExternalStorageDirectory().getAbsolutePath() + APP_PATH_SD_CARD;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    String mailBody = "Email is send along with attachment";
    Mail m = new Mail("FTP backup failed", mailBody);
    try {
        m.addAttachment(path);
        m.send();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}
4

2 回答 2

1

尝试

 public final static String APP_PATH_SD_CARD = "/Images_Book/filetobeattached.ext";

代替

public final static String APP_PATH_SD_CARD = "/Images_Book/";
于 2013-07-12T21:14:49.800 回答
0

如下更新您的功能

public void addAttachment(File filename) throws Exception {
BodyPart messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename.getName());
multipart.addBodyPart(messageBodyPart);

}

于 2020-06-07T15:12:53.150 回答