2014-02-24 49 views
1

我有下面的代码,它只是打开我的gmail的邮件发送屏幕,我不想那样。 其实我想在onClick期间发送即时邮件给邮件标识。 这可能吗?帮助please.Thanks提前。 我是否需要添加任何权限?在Onclick期间自动发送邮件到[email protected] ,,,,,

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[]{ "[email protected]"}); 
        //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; 
     } 

    } 

回答

0

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

+0

正在开发Android应用程序,,,在应用程序注册画面,,,后 注册过程完成,,,我想送电子邮件提醒管理员,, admin @ gmail.com ,,, –

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, "")); 
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. 
+0

它显示了很多错误,,,, –

+0

在这里发布所有错误 – Dev

相关问题