在Onclick期间自动发送电子邮件到somename@gmail.com,,,,

时间:2014-02-24 07:44:42

标签: android email

我有以下代码,它只是打开我的gmail的邮件发送屏幕,我不希望这样。 实际上我想在onClick期间将即时邮件发送到mail-id。 可能吗?请帮助。谢谢。 我需要添加任何权限吗?

package com.example.emails;
    import android.os.Bundle;
    import android.app.Activity;
    import android.content.Intent;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;

    public class Emails extends Activity {
        private static Button email;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_emails);

           email= (Button)findViewById(R.id.button1);
           email.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub


                Intent email = new Intent(Intent.ACTION_SEND);
                  email.putExtra(Intent.EXTRA_EMAIL, new String[]{ "arunshankar.certify@gmail.com"});
                  //email.putExtra(Intent.EXTRA_CC, new String[]{ to});
                  //email.putExtra(Intent.EXTRA_BCC, new String[]{to});
                  email.putExtra(Intent.EXTRA_SUBJECT, "new mail");
                  email.putExtra(Intent.EXTRA_TEXT, "hello");

                  //need this to prompts email client only
                  email.setType("message/rfc822");

                  startActivity(Intent.createChooser(email, "Choose an Email client :"));
            }
        });
        }


        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.emails, menu);
            return true;
        }

    }

4 个答案:

答案 0 :(得分:0)

是的,有可能。但是你需要一个能够发送电子邮件的服务器。设备将对此服务器发出HTTP POST请求,并传递需要发送的参数。

答案 1 :(得分:0)

您可以使用JAVA邮件API发送邮件而无需设置意图。请按照 https://code.google.com/p/javamail-android/

进行操作

答案 2 :(得分:0)

试试这个

Intent intent = new Intent(Intent.ACTION_SEND);
                intent.setType("plain/text");
                intent.putExtra(android.content.Intent.EXTRA_EMAIL,
                        new String[] { "your email" });
                startActivity(Intent.createChooser(intent, ""));

答案 3 :(得分:0)

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Properties;
import javax.mail.Authenticator;import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class EMailSender {
public EMailSender(String host, final String from, final String pass, String to, String     sub, String mess) throws Exception {
Properties props = System.getProperties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
Authenticator auth = new Authenticator() {

protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(from, pass);
}};
Session session = Session.getInstance(props, auth);
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
message.setSubject(sub);
message.setText(mess);
Transport.send(message);
}

public static void main(String arg[]) throws Exception {
if(arg.length == 5) {
StringBuilder message = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String temp = "", subject;
System.out.print("Enter subject: ");
subject = br.readLine();
System.out.println("Enter the message (end it with a . on a single line):");
while((temp = br.readLine()) != null) {
if(temp.equals("."))
break;
message.append(temp+"\n");
}
System.out.println("Sending message...");
new EMailSender(arg[0], arg[1], arg[2], arg[3], subject, message.toString());
System.out.println("Sent the message.");
}
else System.err.println("Usage:\njava SendTextMail <host> <port> <from> <pass> <to>");
}
}

Call Constructor Of This Class In Your Activity On Button Click.