在Android中发送电子邮件

时间:2014-11-04 11:45:28

标签: java php android email

在android中如何自动发送电子邮件。我跟着这个Tutorial但是没有为我工作这里的代码

package com.example.email;

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.Message;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener
{
Session session = null;
ProgressDialog pdialog = null;
Context context = null;
EditText reciep, sub, msg;
String rec, subject,textMessage;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = this;
        Button login = (Button)findViewById(R.id.btnLogin);
        reciep =(EditText) findViewById(R.id.ed_to);
        sub =(EditText) findViewById(R.id.ed_sub);
        msg =(EditText) findViewById(R.id.ed_text);

        login.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        rec=reciep.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 Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication(){
            return new PasswordAuthentication("abcdef@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... params) {
            try {
                Message message =new MimeMessage(session);
                message.setFrom(new InternetAddress("abcdef@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(Exception e)
            {
                e.printStackTrace();
            }
            return null;
        }
@Override
protected void onPostExecute(String result) {
    pdialog.dismiss();
    reciep.setText("");
    msg.setText("");
    sub.setText("");
    Toast.makeText(getApplicationContext(), "Message Sent", Toast.LENGTH_LONG).show();
}
    }
}

使用此代码但不为我工作它显示已发送的消息,但我没有收到任何消息
任何人都可以帮助我 感谢

0 个答案:

没有答案