简体   繁体   中英

Send a Mail with attachment in background Android

I want mail sent in background with file attach from mobile memory in background without intent or user interaction.

Please help me.

Download javamal api jar files, and do like this

recpient= reciept.getText().toString();
      subject= sub.getText().toString();
      textmessage= msg.getText().toString();

    Properties props = new Properties();
     props.put("mail.smtp.host", "smtp.gmail.com");
     props.put("mail.smtp.socketFactory.port", "465");
     props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
     props.put("mail.smtp.auth", "true");
     props.put("mail.smtp.port", "465");

     session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
                       protected PasswordAuthentication getPasswordAuthentication() {
     return new PasswordAuthentication("Your Gmail-ID", "Your Gmail_password");
     }
     });
     pdialog = ProgressDialog.show(context, "", "Sending Mail...",true);
     RetreiveFeedTask task= new RetreiveFeedTask();
     task.execute();
  }    
   class RetreiveFeedTask extends AsyncTask<string string="" void=""> {     
    protected String doInBackground(String... urls) {
          try {

             Message message = new MimeMessage(session);
             message.setFrom(new InternetAddress("</string><string string="" void="">Your Gmail-ID"));
             message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recpient));
             message.setSubject(subject);
             message.setContent(textmessage, "text/html; charset=utf-8");

             Transport.send(message);


          } 
          catch (MessagingException e) {
            e.printStackTrace();
             }
          catch (Exception e) {
             e.printStackTrace();

          }
          return null;
      }

      protected void onPostExecute(String feed) {
       pdialog.dismiss();
       reciept.setText("");
       msg.setText("");
       sub.setText("");
       Toast.makeText(getApplicationContext(), "Message sent", Toast.LENGTH_LONG).show();

      }
   }


 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM