没有客户端Android发送邮件

时间:2016-08-09 06:55:08

标签: java android email javamail

public class MainActivity extends Activity implements View.OnClickListener {

    Session session=null;
    ProgressDialog pdialog=null;
    Context context=null;
    EditText reciep,sub,msg;
    String rec,subject,textMessage;
    Button login;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = this;

        login = (Button) findViewById(R.id.btn_submit);
        reciep = (EditText) findViewById(R.id.et_to);
        sub = (EditText) findViewById(R.id.et_sub);
        msg = (EditText) findViewById(R.id.et_text);

        login.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        rec=reciep.getText().toString();
        subject=sub.getText().toString();
        textMessage=msg.getText().toString();

        Properties props=new Properties();
        props.put("mail.smtp.host","smpt.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.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class",
                "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.socketFactory.fallback", "false");
        props.setProperty("mail.smtp.quitwait", "false");

       session=Session.getDefaultInstance(props,new Authenticator(){
           protected    PasswordAuthentication getPasswordAuthentication()
           {
               return new PasswordAuthentication("example@gmail.com","password");
           }
        });

        pdialog=ProgressDialog.show(context,"","Sending mail....",true);
        RetreiveFeedTask task=new RetreiveFeedTask();
        task.execute();
    }

        class RetreiveFeedTask extends AsyncTask<String,Void,String> {


            @Override
            protected String doInBackground(String... strings) {

                try{
                    Message message=new MimeMessage(session);
                    message.setFrom(new InternetAddress("example@gmail.com"));
                    message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(rec));
                    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;
            }

            @Override
        protected void onPostExecute(String result) {
            pdialog.dismiss();
            reciep.setText("");
            msg.setText("");
            sub.setText("");
            Toast.makeText(getApplicationContext(), "Message Sended", Toast.LENGTH_SHORT).show();
        }
    }





}

它显示了一个致命的例外。你能帮忙吗?

08-09 12:18:53.957 23203-23203/com.example.bathri.mail D/Dialog: show
08-09 12:18:53.979 23203-23323/com.example.bathri.mail E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #4
 Process: com.example.bathri.mail, PID: 23203
 java.lang.RuntimeException: An error occured while executing doInBackground()
     at android.os.AsyncTask$3.done(AsyncTask.java:304)
     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
     at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
     at java.util.concurrent.FutureTask.run(FutureTask.java:242)
     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
     at java.lang.Thread.run(Thread.java:818)
  Caused by: java.lang.VerifyError: Rejecting class javax.mail.internet.MimeMessage because it failed compile-time verification (declaration of 'javax.mail.internet.MimeMessage' appears in /data/data/com.example.bathri.mail/files/instant-run/dex/slice-slice_4-classes.dex)
     at com.example.bathri.mail.MainActivity$RetreiveFeedTask.doInBackground(MainActivity.java:92)
     at com.example.bathri.mail.MainActivity$RetreiveFeedTask.doInBackground(MainActivity.java:85)
     at android.os.AsyncTask$2.call(AsyncTask.java:292)
     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
     at java.lang.Thread.run(Thread.java:818) 

0 个答案:

没有答案