How to save text in a text file and send it as attachment over email in Android? -
when users want email me, want add option can send me logs app. save text in text file , send text file attachment email user going send me.
i have tried following 2 approaches.
approach #1:
calling 2 functions below follows:
copyfiletoexternal("test_file_name" + ".xml"); sendemail("nilashis@gmail.com", "test_file_name"); below functions:
private file copyfiletoexternal(string filename) { file file = null; string newpath = environment.getexternalstoragedirectory()+"/foldername/"; try { file f = new file(newpath); f.mkdirs(); fileinputstream fin = openfileinput(filename); fileoutputstream fos = new fileoutputstream(newpath + filename); //byte[] buffer = new byte[1024]; byte[] buffer = "safdsdfsdfsdfsdfdsf".getbytes(); int len1 = 0; while ((len1 = fin.read(buffer)) != -1) { fos.write(buffer, 0, len1); } fin.close(); fos.close(); file = new file(newpath + filename); if (file.exists()) return file; } catch (exception e) { } return null; } //method email private void sendemail(string email, string filename) { file file = new file(environment.getexternalstoragestate()+"/foldername/" + filename+ ".xml"); uri path = uri.fromfile(file); intent intent = new intent(android.content.intent.action_send); intent.settype("application/octet-stream"); intent.putextra(android.content.intent.extra_subject, "this subject want"); string to[] = { email }; intent.putextra(intent.extra_email, to); intent.putextra(intent.extra_text, "here message want"); intent.putextra(intent.extra_stream, path); startactivityforresult(intent.createchooser(intent, "send mail..."), 1222); } approach #2:
does not work:
public void dosendfile() { string xmlfilename = "filetosend.txt"; fileoutputstream fos = null; try { fos = openfileoutput(xmlfilename, mode_world_readable); } catch (filenotfoundexception e) { e.printstacktrace(); } try { fos.write("this test being written ".getbytes()); } catch (ioexception e) { e.printstacktrace(); } try { fos.close(); } catch (ioexception e) { e.printstacktrace(); } intent intent = new intent(android.content.intent.action_send); intent.settype("text/plain"); // uri uri = uri.fromfile(new file(xmlfilename)); uri uri = uri.fromfile(new file("/mnt/sdcard/../.."+getfilesdir()+"/"+xmlfilename)); intent.putextra(android.content.intent.extra_stream, uri); startactivity(intent.createchooser(intent, "send email..asdasd")); }
i think can here. sending email in android using javamail api without using default/built-in app
http://techblogon.com/send-email-from-an-android-application-programmatically/
and add in manifest.
<uses-permission android:name="android.permission.internet"/>
Comments
Post a Comment