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();
	

}
相关推荐
vx_Biye_Design27 分钟前
springboot游泳馆系统93765-计算机课程设计、毕业设计
java·javascript·spring boot·后端·python·spring·课程设计
专业程序开发源1 小时前
springbootLivehouse票务系统-计算机课程设计、毕业设计
vue.js·spring boot·后端·python·django·课程设计·pygame
卓怡学长1 小时前
w233基于vue和springboot的宠物管理系统的设计与实现
java·vue.js·spring boot·spring·intellij-idea
三少爷的鞋2 小时前
Kotlin 2026:裁员、AI、Rust——黄金时代结束了吗?Jake Wharton 为你解答
android
alexhilton2 小时前
从零开始的架构测试套件
android·kotlin·android jetpack
finhaz3 小时前
台电x98plus在安卓x86的使用
android·平板·安卓x86
事圆则缓4 小时前
遗留项目改造实战指南:从可读性、代码质量到性能和 AI 约束
android·ai·代码规范
梨涡泥窝4 小时前
JavaWeb——基于 Spring Boot + Vue 的图书管理系统的设计与实现
vue.js·spring boot·后端
mmsx4 小时前
Android 手簿 ADB 无线调试全攻略:USB 转 WiFi 一键连接
android·前端