User Id:
Password:
Forgot User ID or Password?
     
 
Facebook Twitter Linked In Stumble Upon Digg In
Articles
Database(11)
Desktop Development(3)
Development Lifecycle(2)
Dot Net Nuke(1)
Enterprise Systems(1)
Frameworks & Libraries(0)
General Programming(25)
General Reading(6)
Graphics / Design(10)
Languages(2)
Mentor Resources(0)
Mobile Development(3)
Multimedia(0)
Research Papers(1)
Social Media(1)
Software Engineering(3)
Third Party Products(0)
Web Development(11)
Add a Article
 
Articles

Sending email with attachment in Android

   

Imagine you are implementing a program for a sales rep that has to print an invoice (reciept) when he is with the client. There is no wifi printer or connection with a local network.

A solution that comes to mind is to send the invoice to the client by email and then he can print it locally.
We will create a function to create the HTML file (invoice file) and save the HTML to an SD card. Note that you have to change the code regarding the invoice details, I just put a sample:

	private void createFile() {
try {
String data = "<meta http-equiv='Content-Type' content='text/html; charset=UTF-8'/><html><body style='tab-interval: .5in'><div class="Section1"><div>";
data += "<p class="MsoNormal" dir="LTR" style="text-align: left; unicode-bidi: embed"><span lang="AR-EG">";
FileOutputStream fos = new FileOutputStream(strFile);

 
Writer out = new OutputStreamWriter(fos, "UTF-8");
 
data += "Email data" + "<br />";
data += "bla bla bla" + "<br />";
data += "Footer" + "<br />";

 
data += "</span></p></div></body></html>";
out.write(data);
out.flush();
out.close();
 
} catch (Throwable t) {
Toast.makeText(this, "Request failed: " + t.toString(),
Toast.LENGTH_LONG).show();
}
}

Now we need code to fire the sending event.

You can add a test box (Edit view) to the activity to let the user write the address and subject, we will use a static variable with fixed variables.

	public void clickbutton(View v) {
try {
strFile = Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/temp";

 
File file = new File(strFile);
if (!file.exists())
file.mkdirs();
strFile = strFile + "/report.html";
createFile();
//
Log.i(getClass().getSimpleName(), "send  task - start");

//
final Intent emailIntent = new Intent(
android.content.Intent.ACTION_SEND);
//
address = "hamdy.ghanem@gmail.com";
subject = "Recite";
emailtext = "Please check the attached recite";

//
emailIntent.setType("plain/text");
 
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
new String[] { address });
 
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
 
emailIntent.putExtra(Intent.EXTRA_STREAM,
Uri.parse("file://" + strFile));

 
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, emailtext);
 
this.startActivity(Intent
.createChooser(emailIntent, "Send mail..."));
 
} catch (Throwable t) {
Toast.makeText(this, "Request failed: " + t.toString(),
Toast.LENGTH_LONG).show();
}
}

Average: 0, Total Ratings: 0

 



FAQ Messages  newPost a Message 
There is no message currently

 
Last Updated June 16, 2019 | Advertise | Privacy Policy
Copyright © QAU Programmers, 1999-2019 All Rights Reserved. Terms of Use