Sending mail in android without intents using SMTP

后端 未结 6 2256
有刺的猬
有刺的猬 2020-11-29 03:28

Hi I am developing an android app which will send mail on click of a button. Code worked at first but due to some reason its not working now. Could anyone please help me wit

6条回答
  •  渐次进展
    2020-11-29 04:14

    config gradle as per below define get reference from Here

    repositories { 
         jcenter()
         maven {
             url "https://maven.java.net/content/groups/public/"
         }
    }
    
    dependencies {
         compile 'com.sun.mail:android-mail:1.5.5'
         compile 'com.sun.mail:android-activation:1.5.5'
    }
    
    android {
       packagingOptions {
           pickFirst 'META-INF/LICENSE.txt' // picks the JavaMail license file
       }
    }
    

    Add this async task to send mail

     public class sendemail extends AsyncTask {
    
        ProgressDialog progressDialog;
        private StringBuilder all_email;
    
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progressDialog = new ProgressDialog(GetuserActivity.this);
            progressDialog.setMessage("Uploading, please wait...");
            progressDialog.show();
            if (selecteduser_arr != null) {
                all_email = new StringBuilder();
                for (int i = 0; i < selecteduser_arr.size(); i++) {
                    if (i == 0) {
                        all_email.append(selecteduser_arr.get(i));
                    } else {
                        String temp = "," + selecteduser_arr.get(i);
                        all_email.append(temp);
                    }
                }
            }
        }
    
        @Override
        protected Integer doInBackground(String... strings) {
    
            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 = Session.getDefaultInstance(props,
                    new javax.mail.Authenticator() {
                        protected PasswordAuthentication getPasswordAuthentication() {
                            return new PasswordAuthentication("enterhereyouremail", "enterherepassword");
                        }
                    });
    
            try {
    
                Message message = new MimeMessage(session);
                message.setFrom(new InternetAddress("enterhereyouremail"));
                message.setRecipients(Message.RecipientType.TO,
                        InternetAddress.parse("sendermail@gmail.com,sendermail2@gmail.com"));
                message.setSubject("Testing Subject");
                message.setText("Dear Mail Crawler," +
                        "\n\n No spam to my email, please!");
    
                Transport.send(message);
    
                System.out.println("Done");
    
            } catch (MessagingException e) {
                throw new RuntimeException(e);
            }
            return 1;
        }
    
        @Override
        protected void onPostExecute(Integer integer) {
            super.onPostExecute(integer);
            progressDialog.dismiss();
        }
    }
    

提交回复
热议问题