Sending Emails get a NetworkOnMainThreadException
<p>I am using the following code to send emails in my app</p>

<p><a href="http://stackoverflow.com/questions/2020088/sending-email-in-android-using-javamail-api-without-using-the-default-built-in-a?lq=1">Sending Email in Android using JavaMail API without using the default/built-in app</a></p>

<p>This has worked before but now has stopped with a NetworkOnMainThreadExceoption. I know the solution is to use a AsyncTask but I do not know how to convert the class I have now to work with AsyncTask. Not used them before. </p>

<p>Below is the Send part of the class that is failing</p>

<pre><code>public class EmailProvider extends Authenticator { 
    private static String emailUsername  = ""; 
    private static String emailPassword = "";
    private static String emailFrom  = ""; 
    private static String emailDefaultSMTPport = 
    private static String emailDefaultSocketFactoryPort = 
    private static String emailHost = ""; 

    private boolean emailAuth; 
    private boolean emailDebuggable; 

    private Multipart emailMultipart; 

    public EmailProvider(Context context) { 
        //this.context = context;

        emailDebuggable = false; // debug mode on or off - default off 
        emailAuth = true; // smtp authentication - default on 

        emailMultipart = new MimeMultipart(); 

    } 

    public boolean send(String emailAddress, String pEmailSubject, String pEmailBody) throws Exception { 

        Properties props = setProperties(); 

        Session session = Session.getInstance(props, this); 

        MimeMessage msg = new MimeMessage(session); 

        msg.setFrom(new InternetAddress(emailFrom)); 

        msg.setRecipients(MimeMessage.RecipientType.TO, emailAddress); 

        msg.setSubject(pEmailSubject); 
        msg.setSentDate(new Date()); 

        // setup message body 
        BodyPart messageBodyPart = new MimeBodyPart();
        messageBodyPart.setText(pEmailBody);

        emailMultipart.addBodyPart(messageBodyPart); 

        // Put parts in message 
        msg.setContent(emailMultipart); 

        // send email 
        Transport.send(msg); 

        return true; 
    } 

}
</code></pre>

<p>Thanks for your Time</p>
