我有一个邮件发送应用程序,它发送电子邮件和附件。除了附件之外,每一件工作都很好。我正在使用此code。但我不知道这段代码中缺少什么或出了什么问题。请帮忙。这是附件部分。
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();
}
}
答案 0 :(得分:1)
尝试
public final static String APP_PATH_SD_CARD = "/Images_Book/filetobeattached.ext";
而不是
public final static String APP_PATH_SD_CARD = "/Images_Book/";
答案 1 :(得分: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);
}