我尝试将文件附加到电子邮件中。 用户写入该文件。 完成后,文件将保存在内部存储器中,并应通过电子邮件发送。
以下是代码:
// write text to file
public void WriteBtn(View v) {
// add-write text into file
try {
FileOutputStream fileout = openFileOutput(fileName, MODE_PRIVATE);
OutputStreamWriter outputWriter=new OutputStreamWriter(fileout);
outputWriter.write(textmsg.getText().toString());
outputWriter.close();
fileout.close();
String fileLocation= Environment.getDataDirectory()+"/"+fileName;
Intent emailIntent = new Intent(Intent.ACTION_SEND);
// set the type to 'email'
emailIntent .setType("vnd.android.cursor.dir/email");
String to[] = {"oshrat0207@gmail.com"};
emailIntent .putExtra(Intent.EXTRA_EMAIL, to);
// the attachment
emailIntent .putExtra(Intent.EXTRA_STREAM, fileLocation);
// the mail subject
emailIntent .putExtra(Intent.EXTRA_SUBJECT, "Subject");
// Uri uri = Uri.fromFile(new File(fileLocation));
String sdCard = Environment.getExternalStorageDirectory().getAbsolutePath();
Uri uri = Uri.fromFile(new File(sdCard +
new String(new char[sdCard.replaceAll("[^/]", "").length()])
.replace("\0", "/..") + getFilesDir() + "/" + fileName));
emailIntent.putExtra(android.content.Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(emailIntent , "Send email..."));
//display file saved message
Toast.makeText(getBaseContext(), "File saved successfully! path:" + fileLocation,
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
}
例外情况:
从未在数据库' /data/data/com.google.android.gms/databases/phenotype.db'上显式调用close(); android.database.sqlite.DatabaseObjectNotClosedException:应用程序未关闭此处打开的游标或数据库对象
找到电子邮件提供商的版本时出错.....
ctivity com.android.internal.app.ChooserActivity泄露了最初在这里注册的IntentReceiver com.android.internal.app.ResolverActivity$1@4107d478。你是否错过了对unregisterReceiver()的调用? android.app.IntentReceiverLeaked:Activity com.android.internal.app.ChooserActivity泄露了最初在这里注册的IntentReceiver com.android.internal.app.ResolverActivity$1@4107d478。你是否错过了对unregisterReceiver()的调用?
我做错了什么?
答案 0 :(得分:1)
您可以使用JavaMail API发送可以将文件附加到电子邮件的多部分电子邮件。要使用JavaMail API发送电子邮件,您需要加载两个jar文件:
的mail.jar 的activation.jar
您可以访问Oracle网站下载最新版本。
检查以下示例
public MimeMessage createEmailMessage() throws AddressException,
MessagingException, UnsupportedEncodingException {
Session mailSession;
MimeMessage emailMessage;
File csvFile = new File(csvFilePath);
if(null != csvFile.list()){
csvName = (csvFile.list())[0];
}
if(null != csvName ){
mailSession = Session.getDefaultInstance(emailProperties, null);
emailMessage = new MimeMessage(mailSession);
emailMessage.setFrom(new InternetAddress(fromEmail, fromEmail));
emailMessage.addRecipient(Message.RecipientType.TO,new InternetAddress(toEmail));
emailMessage.setSubject(emailSubject);
//emailMessage.setContent(emailBody, "text/html");// for a html email
// creates message part
MimeBodyPart messageBodyPart = new MimeBodyPart();
// messageBodyPart.setContent(message, "text/html");
// creates multi-part
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
File csvDir = new File(Constant.FilePathConstant.CSV_PATH );
/***** attach csv files *****/
if(csvDir.exists()){
File[] csvList = xlsDir.listFiles();
for (File file : csvList) {
MimeBodyPart attachPart = new MimeBodyPart();
System.out.println("@@@@@@@@@@@@@@@@@@@@@@ csv " + file.getAbsolutePath());
try{
attachPart.attachFile(file.getAbsolutePath());
}catch(Exception e){
e.printStackTrace();
}
multipart.addBodyPart(attachPart);
}
}
// sets the multi-part as e-mail's content
emailMessage.setContent(multipart);
emailMessage.setText(emailBody);
Logger.i("GMail", "Email Message created.");
}
return emailMessage;
}
public boolean sendEmail() throws AddressException, MessagingException {
if(null != emailMessage){
Transport transport = mailSession.getTransport("smtp");
transport.connect(emailHost, fromEmail, fromPassword);
transport.sendMessage(emailMessage, emailMessage.getAllRecipients());
transport.close();
Logger.i("Report", "Email sent successfully.");
return true;
}else{
return false;
}
}