Android学习笔记之实现自动拨打电话,发短信,发邮件(QQ邮件)功能
Android学习笔记之实现自动拨打电话,发短信,发邮件(QQ邮件)功能
拨打电话
java源文件中添加
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:5554"));
startActivity(intent);
manifest中加入
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
测试时注意android系统是否允许了权限,否则可能会报错
发短信
java源文件中添加
String message = "test scos helper";
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage("5554", null, message, null, null);
Toast.makeText(this,"求助短信发送成功",Toast.LENGTH_SHORT).show();
/*Intent sentIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:5554"));
sentIntent.putExtra("sms_body", message);
startActivity(sentIntent); //这个方式需要手动确认发送
*/
manifest中加入
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
测试时注意android系统是否允许了权限,否则可能会报错
发邮件
这里我才用了javax.mail包
需要自行下载相关jar包
gradle下添加
dependencies {
implementation 'com.sun.mail:android-mail:1.6.0'
implementation 'com.sun.mail:android-activation:1.6.0'
}
再Sync也行
MailSender, (用了好几个内部类,毕竟只是为了实现发一个test邮件功能)
import javax.mail.Authenticator;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import java.util.Date;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.AddressException;
public class MailSender {
MailSenderInfo mailSenderInfo = new MailSenderInfo();
public boolean run(){
return this.sendTextMail(mailSenderInfo);
}
public boolean sendTextMail(final MailSenderInfo mailInfo) {
// 判断是否需要身份认证
MyAuthenticator authenticator = null;
Properties pro = mailInfo.getProperties();
if (mailInfo.isValidate()) {
// 如果需要身份认证,则创建一个密码验证器
authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());
}
// 根据邮件会话属性和密码验证器构造一个发送邮件的session
Session sendMailSession = Session.getDefaultInstance(pro, authenticator);
// Session sendMailSession = Session.getInstance(pro, new Authenticator() {
// @Override
// protected PasswordAuthentication getPasswordAuthentication() {
// return new PasswordAuthentication(mailInfo.getUserName(),mailInfo.getPassword());
// }
// });
try {
// 根据session创建一个邮件消息
Message mailMessage = new MimeMessage(sendMailSession);
// 创建邮件发送者地址
Address from = new InternetAddress(mailInfo.getFromAddress());
// 设置邮件消息的发送者
mailMessage.setFrom(from);
// 创建邮件的接收者地址,并设置到邮件消息中
Address to = new InternetAddress(mailInfo.getToAddress());
mailMessage.setRecipient(Message.RecipientType.TO, to);
// 设置邮件消息的主题
mailMessage.setSubject(mailInfo.getSubject());
// 设置邮件消息发送的时间
mailMessage.setSentDate(new Date());
// 设置邮件消息的主要内容
String mailContent = mailInfo.getContent();
mailMessage.setText(mailContent);
// 发送邮件
Transport.send(mailMessage);
return true;
} catch (MessagingException ex) {
ex.printStackTrace();
}
return false;
}
public class MailSenderInfo {
// 发送邮件的服务器的IP和端口
private String mailServerHost;
// 发送邮件服务器端口,有些邮箱可能不是25
private String mailServerPort = "25";
// 邮件发送者的地址
private String fromAddress;
// 邮件接收者的地址
private String toAddress;
// 登陆邮件发送服务器的用户名和密码
private String userName;
private String password;
// 是否需要身份验证
private boolean validate = false;
// 邮件主题
private String subject;
// 邮件的文本内容
private String content;
// 邮件附件的文件名
private String[] attachFileNames;
/**
* 获得邮件会话属性
*/
public Properties getProperties(){
Properties p = new Properties();
p.put("mail.smtp.host", this.mailServerHost);
p.put("mail.smtp.port", this.mailServerPort);
p.put("mail.smtp.auth", validate ? "true" : "false");
return p;
}
public MailSenderInfo(){
this.mailServerHost = "smtp.qq.com"; //qq邮件的服务器地址
this.mailServerPort = "25";
this.validate = true;
//注意需要开启邮箱的SMTP/POP3/IMAP 的服务
this.password = "......."; //这里密码应设置为qq邮箱的第三方授权码
this.userName = "....@qq.com";//qq邮箱账户名
this.toAddress = "....@qq.com";//发到的qq邮箱账户
this.fromAddress = "@qq.com";//发送邮件的qq邮箱地址
this.subject = "test";
this.content = "This is a test of mailsender.\n If you get this email, please return \"succeeded\" to" +
"your father!";
}
public String getMailServerHost() {
return mailServerHost;
}
public void setMailServerHost(String mailServerHost) {
this.mailServerHost = mailServerHost;
}
public String getMailServerPort() {
return mailServerPort;
}
public void setMailServerPort(String mailServerPort) {
this.mailServerPort = mailServerPort;
}
public boolean isValidate() {
return validate;
}
public void setValidate(boolean validate) {
this.validate = validate;
}
public String[] getAttachFileNames() {
return attachFileNames;
}
public void setAttachFileNames(String[] fileNames) {
this.attachFileNames = fileNames;
}
public String getFromAddress() {
return fromAddress;
}
public void setFromAddress(String fromAddress) {
this.fromAddress = fromAddress;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getToAddress() {
return toAddress;
}
public void setToAddress(String toAddress) {
this.toAddress = toAddress;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getContent() {
return content;
}
public void setContent(String textContent) {
this.content = textContent;
}
}
public class MyAuthenticator extends Authenticator {
String userName = null;
String password = null;
public MyAuthenticator() {
}
public MyAuthenticator(String username, String password) {
this.userName = username;
this.password = password;
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, password);
}
}
}
使用的话run就可以了
MailSender sender = new MailSender();
sender.run();
注意manifest下添加权限
<uses-permission android:name="android.permission.INTERNET"></uses-permission>