Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9170301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:59:53+00:00 2026-06-17T15:59:53+00:00

I am using the following code to send emails in my app Sending Email

  • 0

I am using the following code to send emails in my app

Sending Email in Android using JavaMail API without using the default/built-in app

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.

Below is the Send part of the class that is failing

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

}

Thanks for your Time

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T15:59:53+00:00Added an answer on June 17, 2026 at 3:59 pm

    Declare this asyncTask in the activity from where you are calling send() method:

    private class SendEmailTask extends AsyncTask<Object, Object, Object> {        
            @Override
            protected Object doInBackground(Object... arg0) {
                yourEmailProviderObj.send(emailAddress, pEmailSubject, pEmailBody);
                return "executed";
            }
    }
    

    And make this call instead of your send() method call:

    new SendEmailTask().execute();
    

    Thanks.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using the following code to send emails in my Android application: m_properties =
I'm launching the email program in Android from my app, using the following code...
I am using the following code to send an email from my app.The cursor
I've tried to send email in a Windows Store App by using following code:
I am trying to send email using c# following is my code. try {
I am using the following code to send out emails, the problem I am
I am sending emails successfully using following code. But now I want to attach
I want to send an email with an attachment using the following code (Python
I had recently asked a question regarding the following code: Sending Email in Android
I'm using python 2.7.3. and I have following code for send emails with attached

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.