Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在安卓系统中,除了Gmail发件人,如何在没有用户交互的情况下发送电子邮件?_Android - Fatal编程技术网

Android 在安卓系统中,除了Gmail发件人,如何在没有用户交互的情况下发送电子邮件?

Android 在安卓系统中,除了Gmail发件人,如何在没有用户交互的情况下发送电子邮件?,android,Android,我使用的代码片段如下所示 class SendEmailAsyncTask extends AsyncTask <Void, Void, Boolean> { Mail m = new Mail("someone@gmail.com", "password"); public SendEmailAsyncTask() { if (BuildConfig.DEBUG) Log.v(SendEmailAsyncTask.cla

我使用的代码片段如下所示

class SendEmailAsyncTask extends AsyncTask <Void, Void, Boolean> {
        Mail m = new Mail("someone@gmail.com", "password"); 


        public SendEmailAsyncTask() {
            if (BuildConfig.DEBUG) Log.v(SendEmailAsyncTask.class.getName(), "SendEmailAsyncTask()");
            String[] toArr = {"someone@domain.com"};
            m.setTo(toArr);
            m.setFrom("someone@domain.com");
            m.setSubject("Email from Android");
            m.setBody("Got message");
        }
类SendEmailAsyncTask扩展了AsyncTask{
邮件m=新邮件(“someone@gmail.com“,”密码“);
公共SendEmailAsyncTask(){
if(BuildConfig.DEBUG)Log.v(SendEmailAsyncTask.class.getName(),“SendEmailAsyncTask()”);
字符串[]toArr={”someone@domain.com"};
m、 设置为(toArr);
m、 setFrom(“someone@domain.com");
m、 setSubject(“来自Android的电子邮件”);
m、 收信人(“收到信息”);
}
我想用someone@anydomain.com而不是someone@gmail.com.


还有什么方法可以避免在那里使用密码吗?

Android SDK使从应用程序发送电子邮件变得非常容易,但不幸的是,只有当你想通过内置的邮件应用程序发送电子邮件时,才可以这样做。在大多数情况下,这很好,但如果你想发送一些东西,并且不希望用户进行任何输入/干预,这并不容易

在本文中,我将向您展示如何在用户不知情的情况下在后台发送电子邮件-应用程序将在后台完成所有操作

在开始之前,您需要通过下面的链接下载一些文件——这是专门为Android编写的JavaMail API的特殊版本

我将带您浏览我编写的邮件包装器,如果您愿意的话,它可以更轻松地发送电子邮件,甚至添加附件

下面是完整的包装器类,我将一步一步地介绍它-记住,如果您希望这样做,您必须添加前面提到的文件。将它们添加为外部库-它们需要被Mail类访问

    import java.util.Date; 
import java.util.Properties; 
import javax.activation.CommandMap; 
import javax.activation.DataHandler; 
import javax.activation.DataSource; 
import javax.activation.FileDataSource; 
import javax.activation.MailcapCommandMap; 
import javax.mail.BodyPart; 
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; 


public class Mail extends javax.mail.Authenticator { 
  private String _user; 
  private String _pass; 

  private String[] _to; 
  private String _from; 

  private String _port; 
  private String _sport; 

  private String _host; 

  private String _subject; 
  private String _body; 

  private boolean _auth; 

  private boolean _debuggable; 

  private Multipart _multipart; 


  public Mail() { 
    _host = "smtp.gmail.com"; // default smtp server 
    _port = "465"; // default smtp port 
    _sport = "465"; // default socketfactory port 

    _user = ""; // username 
    _pass = ""; // password 
    _from = ""; // email sent from 
    _subject = ""; // email subject 
    _body = ""; // email body 

    _debuggable = false; // debug mode on or off - default off 
    _auth = true; // smtp authentication - default on 

    _multipart = new MimeMultipart(); 

    // There is something wrong with MailCap, javamail can not find a handler for the multipart/mixed part, so this bit needs to be added. 
    MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap(); 
    mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html"); 
    mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml"); 
    mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain"); 
    mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed"); 
    mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822"); 
    CommandMap.setDefaultCommandMap(mc); 
  } 

  public Mail(String user, String pass) { 
    this(); 

    _user = user; 
    _pass = pass; 
  } 

  public boolean send() throws Exception { 
    Properties props = _setProperties(); 

    if(!_user.equals("") && !_pass.equals("") && _to.length > 0 && !_from.equals("") && !_subject.equals("") && !_body.equals("")) { 
      Session session = Session.getInstance(props, this); 

      MimeMessage msg = new MimeMessage(session); 

      msg.setFrom(new InternetAddress(_from)); 

      InternetAddress[] addressTo = new InternetAddress[_to.length]; 
      for (int i = 0; i < _to.length; i++) { 
        addressTo[i] = new InternetAddress(_to[i]); 
      } 
        msg.setRecipients(MimeMessage.RecipientType.TO, addressTo); 

      msg.setSubject(_subject); 
      msg.setSentDate(new Date()); 

      // setup message body 
      BodyPart messageBodyPart = new MimeBodyPart(); 
      messageBodyPart.setText(_body); 
      _multipart.addBodyPart(messageBodyPart); 

      // Put parts in message 
      msg.setContent(_multipart); 

      // send email 
      Transport.send(msg); 

      return true; 
    } else { 
      return false; 
    } 
  } 

  public void addAttachment(String filename) throws Exception { 
    BodyPart messageBodyPart = new MimeBodyPart(); 
    DataSource source = new FileDataSource(filename); 
    messageBodyPart.setDataHandler(new DataHandler(source)); 
    messageBodyPart.setFileName(filename); 

    _multipart.addBodyPart(messageBodyPart); 
  } 

  @Override 
  public PasswordAuthentication getPasswordAuthentication() { 
    return new PasswordAuthentication(_user, _pass); 
  } 

  private Properties _setProperties() { 
    Properties props = new Properties(); 

    props.put("mail.smtp.host", _host); 

    if(_debuggable) { 
      props.put("mail.debug", "true"); 
    } 

    if(_auth) { 
      props.put("mail.smtp.auth", "true"); 
    } 

    props.put("mail.smtp.port", _port); 
    props.put("mail.smtp.socketFactory.port", _sport); 
    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); 
    props.put("mail.smtp.socketFactory.fallback", "false"); 

    return props; 
  } 

  // the getters and setters 
  public String getBody() { 
    return _body; 
  } 

  public void setBody(String _body) { 
    this._body = _body; 
  } 

  // more of the getters and setters ….. 
} 
在这段代码中,我们初始化属性,并设置默认值

此外,我们正在为javamail设置mime类型。我还添加了一条注释,描述了我们为什么需要它

您可能已经注意到有两个构造函数-一个重写另一个,只是为了在实例化类时传递用户名和密码

public boolean send() throws Exception { 
  Properties props = _setProperties(); 

  if(!_user.equals("") && !_pass.equals("") && _to.length > 0 && !_from.equals("") && !_subject.equals("") && !_body.equals("")) { 
    Session session = Session.getInstance(props, this); 

    MimeMessage msg = new MimeMessage(session); 

    msg.setFrom(new InternetAddress(_from)); 

    InternetAddress[] addressTo = new InternetAddress[_to.length]; 
    for (int i = 0; i < _to.length; i++) { 
      addressTo[i] = new InternetAddress(_to[i]); 
    } 
    msg.setRecipients(MimeMessage.RecipientType.TO, addressTo); 

    msg.setSubject(_subject); 
    msg.setSentDate(new Date()); 

    // setup message body 
    BodyPart messageBodyPart = new MimeBodyPart(); 
    messageBodyPart.setText(_body); 
    _multipart.addBodyPart(messageBodyPart); 

    // Put parts in message 
    msg.setContent(_multipart); 

    // send email 
    Transport.send(msg); 

    return true; 
  } else { 
    return false; 
  } 
} 
如果要添加附件,可以随时调用此方法,但请确保在使用send方法之前调用它

private Properties _setProperties() { 
  Properties props = new Properties(); 

  props.put("mail.smtp.host", _host); 

  if(_debuggable) { 
    props.put("mail.debug", "true"); 
  } 

  if(_auth) { 
    props.put("mail.smtp.auth", "true"); 
  } 

  props.put("mail.smtp.port", _port); 
  props.put("mail.smtp.socketFactory.port", _sport); 
  props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); 
  props.put("mail.smtp.socketFactory.fallback", "false"); 

  return props; 
} 
这里我们设置邮件检索的属性-默认为SMTP身份验证

还请记住,这是所有默认连接到Gmail(谷歌)SMTP服务器

下面是一个如何在Android活动中使用邮件包装器的示例

@Override 
public void onCreate(Bundle icicle) { 
  super.onCreate(icicle); 
  setContentView(R.layout.main); 

  Button addImage = (Button) findViewById(R.id.send_email); 
  addImage.setOnClickListener(new View.OnClickListener() { 
    public void onClick(View view) { 
      Mail m = new Mail("gmailusername@gmail.com", "password"); 

      String[] toArr = {"bla@bla.com", "lala@lala.com"}; 
      m.setTo(toArr); 
      m.setFrom("wooo@wooo.com"); 
      m.setSubject("This is an email sent using my Mail JavaMail wrapper from an Android device."); 
      m.setBody("Email body."); 

      try { 
        m.addAttachment("/sdcard/filelocation"); 

        if(m.send()) { 
          Toast.makeText(MailApp.this, "Email was sent successfully.", Toast.LENGTH_LONG).show(); 
        } else { 
          Toast.makeText(MailApp.this, "Email was not sent.", Toast.LENGTH_LONG).show(); 
        } 
      } catch(Exception e) { 
        //Toast.makeText(MailApp.this, "There was a problem sending the email.", Toast.LENGTH_LONG).show(); 
        Log.e("MailApp", "Could not send email", e); 
      } 
    } 
  }); 
} 

这是您必须在触发邮件的函数中编写的Android代码段

public void addAttachment(String filename) throws Exception { 
  BodyPart messageBodyPart = new MimeBodyPart(); 
  DataSource source = new FileDataSource(filename); 
  messageBodyPart.setDataHandler(new DataHandler(source)); 
  messageBodyPart.setFileName(filename); 

  _multipart.addBodyPart(messageBodyPart); 
} 
将以异步方式运行的类

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

private Exception exception;

protected String doInBackground(String... urls) {
    try {
        // Creating HTTP client
        HttpClient httpClient = new DefaultHttpClient();
        // Creating HTTP Post
        HttpPost httpPost = new HttpPost(
                "<url>/api.php");

        // Building post parameters
        // key and value pair
        List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(5);
        nameValuePair.add(new BasicNameValuePair("x", "1"));
        nameValuePair.add(new BasicNameValuePair("lat", "<email id>"));
        nameValuePair.add(new BasicNameValuePair("long", "Hi"));
        nameValuePair.add(new BasicNameValuePair("message", "hi"));
        nameValuePair.add(new BasicNameValuePair("header", "Hi"));

        // Url Encoding the POST parameters
        try {
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
        } catch (UnsupportedEncodingException e) {
            // writing error to Log
            e.printStackTrace();
        }

        // Making HTTP Request
        try {
            HttpResponse response = httpClient.execute(httpPost);

            // writing response to log
            Log.d("Http Response:", response.toString());
            return response.toString();
        } catch (ClientProtocolException e) {
            // writing exception to log
            e.printStackTrace();
        } catch (IOException e) {
            // writing exception to log
            e.printStackTrace();

        }

    } catch (Exception e) {
        this.exception = e;
        return null;
    }
    return null;

}

protected void onPostExecute(String feed) {
    // TODO: check this.exception 
    // TODO: do something with the feed
}
}
class RetrieveFeedTask扩展了AsyncTask{
私人例外;
受保护的字符串doInBackground(字符串…URL){
试一试{
//创建HTTP客户端
HttpClient HttpClient=新的DefaultHttpClient();
//创建HTTP Post
HttpPost HttpPost=新的HttpPost(
“/api.php”);
//建筑柱参数
//键和值对
List nameValuePair=新的ArrayList(5);
添加(新的BasicNameValuePair(“x”,“1”));
添加(新的BasicNameValuePair(“lat”和“”);
添加(新的BasicNameValuePair(“长”、“高”));
添加(新的BasicNameValuePair(“消息”,“嗨”));
添加(新的BasicNameValuePair(“header”,“Hi”));
//对POST参数进行Url编码
试一试{
setEntity(新的UrlEncodedFormEntity(nameValuePair));
}捕获(不支持的编码异常e){
//将错误写入日志
e、 printStackTrace();
}
//发出HTTP请求
试一试{
HttpResponse response=httpClient.execute(httpPost);
//将响应写入日志
Log.d(“Http响应:,Response.toString());
返回response.toString();
}捕获(客户端协议例外e){
//将异常写入日志
e、 printStackTrace();
}捕获(IOE异常){
//将异常写入日志
e、 printStackTrace();
}
}捕获(例外e){
这个异常=e;
返回null;
}
返回null;
}
受保护的void onPostExecute(字符串馈送){
//TODO:检查此项。异常
//TODO:对提要做些什么
}
}
现在你可以这么叫了

new RetrieveFeedTask().execute(
            "<url>/api.php");
新建RetrieveFeedTask()。执行(
“/api.php”);
在api.php中,您可以提取通过POST请求发送的所有参数,然后发送普通php邮件

<?php
extract($_POST);
mail($to,$subject,$message,$header);
echo "Sent";

?>


这就足够了。

您好,谢谢您的回复,但我想知道,我可以从android更改默认的gmail smtp服务器吗?是否只能从gmail发送此邮件?请尝试更改所需邮件配置的smtp配置。在没有用户交互的情况下在后台发送邮件可能会给您的应用程序带来安全问题用户。确保您的用户知道。我需要在不知道用户的情况下发送该邮件,但我需要从gmail域以外的其他地方发送该邮件。我如何才能做到?
<?php
extract($_POST);
mail($to,$subject,$message,$header);
echo "Sent";

?>