Click here to Skip to main content
12,250,675 members (39,299 online)
Click here to Skip to main content
Add your own
alternative version

Stats

6.8K views
6 bookmarked
Posted

How to Send Email From an Exception Block in Android

, 9 Jun 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
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.

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):

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:

    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:

} 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:

} 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)

Share

About the Author

B. Clay Shannon
Founder "Across Time & Space"
United States United States
Ideaman and Coder at Across Time & Space, creator of "Mark Twain Central" at http://twaincentral.azurewebsites.net/

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: http://www.lulu.com/spotlight/blackbirdcraven

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 5 Pin
khanhamid92215-Jul-14 11:48
memberkhanhamid92215-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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160426.1 | Last Updated 9 Jun 2014
Article Copyright 2014 by B. Clay Shannon
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid