在日常生活中,我们不仅可以使用微信、QQ等聊天工具交流,还可以使用短信、邮箱哦!
这里我分享一个通过邮箱交流的android程序,代码非常简单,希望大家好好阅读哦!
它的界面图如下:
首先,我们在manifest文件中添加一下权限哦!
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
然后,在res/values下的strings.xml文件中,添加:
<string name="start">邮件发送中...</string>
好,准备工作都做好了,接下来,出示我们的layout文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#4CAF50">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textView01"
android:text="收件人地址:"
android:textColor="#222222"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/editText01"
android:text="fayahua168@126.com"
android:textColor="#222222"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textView04"
android:text="发件人地址:"
android:textColor="#222222"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/editText04"
android:text="fs3344168@126.com"
android:textColor="#222222"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textView02"
android:text="邮件主题"
android:textColor="#222222"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/editText02"
android:textColor="#222222"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<TextView
android:id="@+id/textView03"
android:text="邮件内容"
android:textColor="#222222"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/editText03"
android:textColor="#222222"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="top|left"/>
<Button
android:background="#CDDC39"
android:id="@+id/Button01"
android:text="发送"
android:textColor="#222222"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
最后,java代码来啦:
package com.annan.welinkdemo;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class SendEmail extends AppCompatActivity {
private EditText receiver,sender,theme,message;
private Button send;
private String strR,strS,strT,strM;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.send_email);
receiver=findViewById(R.id.editText01);
sender=findViewById(R.id.editText04);
theme=findViewById(R.id.editText02);
message=findViewById(R.id.editText03);
send=findViewById(R.id.Button01);
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
strR=receiver.getText().toString().trim();
strS=sender.getText().toString().trim();
strT=theme.getText().toString().trim();
strM=message.getText().toString().trim();
String parent="^[a-zA-Z][\\w\\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\\w\\.-]*[a-zA-Z0-9]\\.[a-zA-Z][a-zA-Z\\.]*[a-zA-Z]$";
if (!strR.matches(parent)) {
Toast.makeText(SendEmail.this, "收件人地址格式错误", Toast.LENGTH_SHORT).show();
}else if (!strS.matches(parent)){
Toast.makeText(SendEmail.this, "发件人地址格式错误", Toast.LENGTH_SHORT).show();
}else {
Intent intent=new Intent(Intent.ACTION_SEND);
intent.setType("plain/text");
intent.putExtra(Intent.EXTRA_EMAIL,strR);
intent.putExtra(Intent.EXTRA_CC,strS);
intent.putExtra(Intent.EXTRA_SUBJECT,strT);
intent.putExtra(Intent.EXTRA_TEXT,strM);
startActivity(Intent.createChooser(intent,getResources().getString(R.string.start)));
}
}
});
}
}
OK,大功告成了,小伙伴们在自己手机上运行试试哈,有任何问题咱们一起讨论吖!