简体   繁体   中英

unable to send mail from personal email android

I am creating a web app in which i want to mail the forms created by me in my phone the form should be mail automatically when in the internet connection it was working fine when i am using the official email of my office but when i change the email(to my personal email id) it is not sending the message and showing this error

> 12-21 16:34:10.398 12978-12978/com.example.admin.myinsert
> W/System.err: javax.mail.AuthenticationFailedException: 534-5.7.14
> <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbu5
> 12-21 16:34:10.399 12978-12978/com.example.admin.myinsert
> W/System.err: 534-5.7.14
> 559sFRUM5ZAOllEuctlP6cgwoHmZHeLNMIOI-ExU6Hb0lod3Dd3_w2-yyHe5Ul_M3UljFy
> 12-21 16:34:10.399 12978-12978/com.example.admin.myinsert
> W/System.err: 534-5.7.14
> kaaPDRNSc0YQJHyWAWeVDOQl8XGiB4KHfI6c7ihXvumt2wKdrcJA4Zq02foAL-sDcPxHW3
> 12-21 16:34:10.399 12978-12978/com.example.admin.myinsert
> W/System.err: 534-5.7.14
> fI1f4wxITmUVYE3iqPw4FpGdUdYsdnbbF31su3LDZjVhfQqinmwm4u45LQDNrh1F2ru79Q
> 12-21 16:34:10.399 12978-12978/com.example.admin.myinsert
> W/System.err: 534-5.7.14 ROdbX2QIgauzaDDzfCMTqan3D3dqI> Please log in
> via your web browser and 12-21 16:34:10.399
> 12978-12978/com.example.admin.myinsert W/System.err: 534-5.7.14 then
> try again. 12-21 16:34:10.399 12978-12978/com.example.admin.myinsert
> W/System.err: 534-5.7.14  Learn more at 12-21 16:34:10.399
> 12978-12978/com.example.admin.myinsert W/System.err: 534 5.7.14 
> https://support.google.com/mail/answer/78754 i194sm46376461pgc.46 -
> gsmtp 12-21 16:34:10.407 12978-12978/com.example.admin.myinsert
> W/System.err:     at
> com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:914)
> 12-21 16:34:10.407 12978-12978/com.example.admin.myinsert
> W/System.err:     at
> com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:825)
> 12-21 16:34:10.407 12978-12978/com.example.admin.myinsert
> W/System.err:     at
> com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:730)
> 12-21 16:34:10.407 12978-12978/com.example.admin.myinsert
> W/System.err:     at javax.mail.Service.connect(Service.java:388)
> 12-21 16:34:10.407 12978-12978/com.example.admin.myinsert
> W/System.err:     at javax.mail.Service.connect(Service.java:246)
> 12-21 16:34:10.407 12978-12978/com.example.admin.myinsert
> W/System.err:     at javax.mail.Service.connect(Service.java:195)
> 12-21 16:34:10.407 12978-12978/com.example.admin.myinsert
> W/System.err:     at javax.mail.Transport.send0(Transport.java:254)
> 12-21 16:34:10.407 12978-12978/com.example.admin.myinsert
> W/System.err:     at javax.mail.Transport.send(Transport.java:124)
> 12-21 16:34:10.407 12978-12978/com.example.admin.myinsert
> W/System.err:     at
> com.example.admin.myinsert.ChekInet.onReceive(ChekInet.java:130) 12-21
> 16:34:10.407 12978-12978/com.example.admin.myinsert W/System.err:    
> at android.app.ActivityThread.handleReceiver(ActivityThread.java:2612)
> 12-21 16:34:10.408 12978-12978/com.example.admin.myinsert
> W/System.err:     at
> android.app.ActivityThread.access$1700(ActivityThread.java:151) 12-21
> 16:34:10.408 12978-12978/com.example.admin.myinsert W/System.err:    
> at
> android.app.ActivityThread$H.handleMessage(ActivityThread.java:1418)
> 12-21 16:34:10.408 12978-12978/com.example.admin.myinsert
> W/System.err:     at
> android.os.Handler.dispatchMessage(Handler.java:110) 12-21
> 16:34:10.408 12978-12978/com.example.admin.myinsert W/System.err:    
> at android.os.Looper.loop(Looper.java:193) 12-21 16:34:10.408
> 12978-12978/com.example.admin.myinsert W/System.err:     at
> android.app.ActivityThread.main(ActivityThread.java:5333) 12-21
> 16:34:10.408 12978-12978/com.example.admin.myinsert W/System.err:    
> at java.lang.reflect.Method.invokeNative(Native Method) 12-21
> 16:34:10.408 12978-12978/com.example.admin.myinsert W/System.err:    
> at java.lang.reflect.Method.invoke(Method.java:515) 12-21 16:34:10.408
> 12978-12978/com.example.admin.myinsert W/System.err:     at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
> 12-21 16:34:10.408 12978-12978/com.example.admin.myinsert
> W/System.err:     at
> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645) 12-21
> 16:34:10.408 12978-12978/com.example.admin.myinsert W/System.err:    
> at dalvik.system.NativeStart.main(Native Method) 12-21 16:34:10.410 12

this is my coding for the same

package com.example.admin.myinsert;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Environment;

import android.os.StrictMode;
import android.widget.Toast;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import static com.sun.mail.imap.SortTerm.CC;

/**
 * Created by Admin on 12/16/2016.
 */

public class ChekInet extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }
        Toast.makeText(context,"Recive",Toast.LENGTH_SHORT).show();
        ConnectivityManager cm = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo net=cm.getActiveNetworkInfo();
        boolean isconnected=net!=null && net.isConnectedOrConnecting();
        if(isconnected==true)
        {
            Toast.makeText(context,"Connected",Toast.LENGTH_SHORT).show();
            File[] myFiles;
            File pathToMySongs = new File(
                    Environment.getExternalStorageDirectory() + "/Testing");
            if(pathToMySongs.exists())
            {
                myFiles = pathToMySongs.listFiles();
                Toast.makeText(context, "files found.", Toast.LENGTH_SHORT).show();
                Session session=null;
                for(File file : myFiles)
                {

                    ArrayList<String>list=new ArrayList<>();
                    String path = file.getAbsolutePath();
                    list.add(path);
                    try
                    {
                        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");*/

                        props.setProperty("mail.transport.protocol", "smtp");
                        props.setProperty("mail.host", "smtp.gmail.com");
                        props.put("mail.smtp.auth", "true");
                        props.put("mail.smtp.port", "465");
                        props.put("mail.smtp.socketFactory.port", "465");
                        props.put("mail.smtp.socketFactory.class",
                                "javax.net.ssl.SSLSocketFactory");
                        props.put("mail.smtp.starttls.enable", "true");
                        props.put("mail.smtp.socketFactory.fallback", "false");
                        props.setProperty("mail.smtp.quitwait", "false");

                        session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
                            protected PasswordAuthentication getPasswordAuthentication() {
                                return new PasswordAuthentication("username", "password");
                            }
                        });
                    }
                    catch (Exception e)
                    {

                    }

                    Multipart _multipart = new MimeMultipart();
                    Message message = new MimeMessage(session);
                    try {


                        message.setFrom(new InternetAddress("sender"));

                        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("reciver"));


                        message.setSubject("Testing");

                        BodyPart messageBodyPart = new MimeBodyPart();

                        messageBodyPart.setText("Hi");
                        MimeBodyPart messageBodyPart2 = new MimeBodyPart();

                        String filename = "SendAttachment.java";//change accordingly
                        DataSource source = new FileDataSource(path);
                        messageBodyPart2.setDataHandler(new DataHandler(source));
                        messageBodyPart2.setFileName(path);



                        _multipart.addBodyPart(messageBodyPart);
                        _multipart.addBodyPart(messageBodyPart2);

                        message.setContent(_multipart);
                        Transport.send(message);

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

                    }
                }
            }
            else
                Toast.makeText(context, "No files found.", Toast.LENGTH_SHORT).show();
        }
        else
        {
            Toast.makeText(context,"DisConnected",Toast.LENGTH_SHORT).show();
        }
    }
}

what i need to do, i want to send the mail from my personal account and not from company's account

private void sendMail(String filePath) {
    final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
            emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
            new String[]{getString(R.string.support_mail_id)});
            emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
            getString(R.string.fare_title));
             emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,
            getString(R.string.mail_message));
            emailIntent.setType("image/png");
            final Uri myUri = Uri.parse("file://" + filePath);
            emailIntent.putExtra(Intent.EXTRA_STREAM, myUri);
            startActivity(Intent.createChooser(emailIntent,getString(R.string.sending_mail)));
}

Use this method to send mail

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