Click here to Skip to main content
15,880,427 members
Articles / Mobile Apps / Android
Tip/Trick

How to Send Email From an Exception Block in Android

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
9 Jun 2014CPOL1 min read 13.8K   6   1
Simple code to send email which contains Exception text from a Catch Block

And You Thought Spam Was Irritating

You can keep track of exceptions users are getting by adding a method to a "Utils" class in your app like so:

Note: I adapted this code from here.

Java
public void sendEmail(String emailAddress, String emailSubject, String emailBody) {
    final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
    emailIntent.setType("plain/html");
    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] {emailAddress});
    emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, emailSubject);
    emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, emailBody);
    startActivity(Intent.createChooser(emailIntent, "Sending email..."));
}

In context (you will need the context (no pun inten[d,t]ed) to call startActivity):

Java
public class MyBeaudaciousUtils {
    private Context _context;
    public String EMAIL_ADDRESS = "johnny_appleseed@orchardsRUs.com";

    public void sendEmail(String emailAddress, String emailSubject, String emailBody) {
        final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
        emailIntent.setType("plain/html");
        emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] {emailAddress}); 
        emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, emailSubject);
        emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, emailBody); 
        _context.startActivity(Intent.createChooser(emailIntent, "Sending email..."));
    }
    . . .  

Now you can call sendEmail from anywhere in your project:

Java
    MyBeaudaciousUtils.sendEmail("sirspamalot@quixote.net", 
"something", "<p>Yikes! Tykes on Trikes!</p>");     

Exceptional Email

You might want to put the call to sendEmail() inside a catch block, like so:

Java
} catch (Exception e) {
    String errHTML = String.format("<h2>EXCEPTIONAL!</h2><p>%s</p>", e.getMessage());
    hhsUtils.sendEmail(hhsUtils.EMAIL_ADDRESS, "Exception in GetAJob()", errHTML);
}

...or, if your catch block is inside a method that returns a String value, and you also want to write the exception to the debug console, you can do this:

Java
} catch (Exception e) {
    System.out.println(e.getMessage());
    String errHTML = String.format("<h2>EXCEPTIONAL!</h2><p>%s</p>", e.getMessage());
    hhsUtils.sendEmail(hhsUtils.EMAIL_ADDRESS, "Exception in GetURLToUse()", errHTML);
    return e.getMessage();
}

You (or the owner of the email address shown above) will then get an email that looks something like the following following an exception:

EXCEPTIONAL!

Too many vestigial spider monkeys playing rosin-less-bowed violins have escaped into the innards of your code!

YMMV

It's possible the exception message you get will be more informative (but more boring) than the example shown above (Your Monkeys May Vary).

aELttR

"aElttR" is not just a near-anagram of "A Letter"; it stands for, "an Exercise Left to the Reader" which is: you may want to add more info to the email you send to yourself (or to whomever is to be the recipient of the exception messages), such as: username and/or location, version of the software they are running, a time stamp, the specific type of device they are using, or any other tidbit that will help you in your debugging/spelunking/code detective efforts.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Founder Across Time & Space
United States United States
I am in the process of morphing from a software developer into a portrayer of Mark Twain. My monologue (or one-man play, entitled "The Adventures of Mark Twain: As Told By Himself" and set in 1896) features Twain giving an overview of his life up till then. The performance includes the relating of interesting experiences and humorous anecdotes from Twain's boyhood and youth, his time as a riverboat pilot, his wild and woolly adventures in the Territory of Nevada and California, and experiences as a writer and world traveler, including recollections of meetings with many of the famous and powerful of the 19th century - royalty, business magnates, fellow authors, as well as intimate glimpses into his home life (his parents, siblings, wife, and children).

Peripatetic and picaresque, I have lived in eight states; specifically, besides my native California (where I was born and where I now again reside) in chronological order: New York, Montana, Alaska, Oklahoma, Wisconsin, Idaho, and Missouri.

I am also a writer of both fiction (for which I use a nom de plume, "Blackbird Crow Raven", as a nod to my Native American heritage - I am "½ Cowboy, ½ Indian") and nonfiction, including a two-volume social and cultural history of the U.S. which covers important events from 1620-2006: http://www.lulu.com/spotlight/blackbirdcraven

Comments and Discussions

 
GeneralMy vote of 5 Pin
khanhamid92215-Jul-14 11:48
khanhamid92215-Jul-14 11:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.