react native android设置邮箱,进行邮件发送

react native android设置邮箱,进行邮件发送

  1. 引入发送邮件的架包。

    在build.gradle 的dependencies 下引入:

    复制代码
     //发送邮件的依赖
     implementation 'com.sun.mail:android-mail:1.6.7'
     implementation 'com.sun.mail:android-activation:1.6.7'
  2. 导入EmailSender类

    复制代码
     import android.widget.Toast;
     
     import java.util.Properties;
     
     import javax.mail.Authenticator;
     import javax.mail.Message;
     import javax.mail.MessageRemovedException;
     import javax.mail.MessagingException;
     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 {
         private String username;
         private String password;
         private String host;
         private String port;
     
     
         public EmailSender(String username,String password,String host,String port){
             this.username = username;
             this.password = password;
             this.host = host;
             this.port = port;
     
         }
     
         public void sendEmail(String to, String subject , String text){
     
             Properties properties = new Properties();
             properties.put("mail.smtp.auth",true);
             properties.put("mail.smtp.starttls.enable",true);
             properties.put("mail.smtp.host",host);
             properties.put("mail.smtp.port",port);
     
             Session session = Session.getInstance(properties, new Authenticator() {
                 @Override
                 protected PasswordAuthentication getPasswordAuthentication() {
                     return new PasswordAuthentication(username,password);
                 }
             });
     
             try {
                 Message message = new MimeMessage(session);
                 message.setFrom(new InternetAddress(username));
                 message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to));
                 message.setSubject(subject);
                 message.setText(text);
     
     
                 Transport.send(message);
     
             } catch (MessagingException e){
                 e.printStackTrace();
             }
     
         }
     
     }
  3. 在NativeModule 中调用。

username:账户名、邮箱

password:密码

host:主机

port:端口

to:发送的邮箱

subject:主题

text:内容

复制代码
	@ReactMethod
	    public void sendEmail(String username,String password,String host,String port,String to,String subject,String text) {
	
	        new Thread(new Runnable() {
	            @Override
	            public void run() {
	                EmailSender emailSender = new EmailSender(username,password,host,port);
	
	                emailSender.sendEmail(to,subject,text);
	            }
	        }).start();
	

}
相关推荐
阿维的博客日记3 小时前
Hippo4j 线程池监控平台部署手册
java·spring boot·后端
开维游戏引擎6 小时前
AI自动生成游戏时,deepseek和mimo对比
android·游戏·语言模型·游戏引擎·ai编程
BreezeDove12 小时前
【Android】AS项目自动连接mumu模拟器配置
android
乐世东方客15 小时前
备份脚本记录(binlog文件+mysql+mongo)
android·数据库·mysql
私人珍藏库15 小时前
[Android] 视频下载鸟 v20.02 会员
android·人工智能·智能手机·app·工具·多功能
鹅城剑仙15 小时前
Spring Boot 微服务架构设计与最佳实践
spring boot·后端·微服务
zh_xuan16 小时前
tv浏览网页工具
android·tv浏览网页
心之伊始16 小时前
Spring Boot Actuator + Micrometer 实战:自定义业务指标并接入 Prometheus 观测接口耗时
java·spring boot·prometheus·actuator·micrometer
我登哥MVP17 小时前
走进 Gang of Four 设计模式:装饰器模式
java·spring boot·设计模式·装饰器模式
Carson带你学Android17 小时前
Compose 终于上线 FlexBox:换行与弹性伸缩 都轻松搞定!
android·composer