无法从个人电子邮件android发送邮件

时间:2016-12-21 11:09:23

标签: java android email android-intent

我正在创建一个网络应用程序,我希望在我的手机中邮寄我创建的表单,当我在使用我办公室的官方电子邮件时,在互联网连接时表格应该是自动发送的。我更改了电子邮件(到我的个人电子邮件ID)它没有发送消息并显示此错误

> 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

这是我编码的相同

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

我需要做什么,我想从我的个人帐户发送邮件,而不是从公司的帐户发送邮件

1 个答案:

答案 0 :(得分:1)

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

使用此方法发送邮件