【发布时间】:2013-08-24 00:41:31
【问题描述】:
我需要自动发送电子邮件,而无需在模拟器上选择电子邮件应用程序。可能吗? 也就是点击按钮时默认输入邮件的主题和正文,邮件应该会自动发送。
【问题讨论】:
我需要自动发送电子邮件,而无需在模拟器上选择电子邮件应用程序。可能吗? 也就是点击按钮时默认输入邮件的主题和正文,邮件应该会自动发送。
【问题讨论】:
您可以在您的服务器上制作一个 php 脚本:
<?php
$name = $_POST['name'];
$to = $_POST['to'];
$from = $_POST['from'];
$subject = $_POST['subject'];
$message = "From: ".$name."\r\n";
$message .= $_POST['message'];
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
?>
并从android应用程序使用json发送数据:[您可以从edittext获取值并在此处使用值]
public static void sendData(String name, String to, String from, String subject, String message)
{
String content = "";
try
{
/* Sends data through a HTTP POST request */
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://your.website.com");
List <NameValuePair> params = new ArrayList <NameValuePair>();
params.add(new BasicNameValuePair("name", name));
params.add(new BasicNameValuePair("to", to));
params.add(new BasicNameValuePair("from", from));
params.add(new BasicNameValuePair("subject", subject));
params.add(new BasicNameValuePair("message", message));
httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
/* Reads the server response */
HttpResponse response = httpClient.execute(httpPost);
InputStream in = response.getEntity().getContent();
StringBuffer sb = new StringBuffer();
int chr;
while ((chr = in.read()) != -1)
{
sb.append((char) chr);
}
content = sb.toString();
in.close();
/* If there is a response, display it */
if (!content.equals(""))
{
Log.i("HTTP Response", content);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
【讨论】: