Add MailUtil and send log mail
authorPeng Li <seudut@gmail.com>
Fri, 25 May 2018 10:02:51 +0000 (18:02 +0800)
committerPeng Li <seudut@gmail.com>
Fri, 25 May 2018 10:02:51 +0000 (18:02 +0800)
app/build.gradle
app/libs/android-activation-1.5.5.jar [new file with mode: 0644]
app/libs/android-mail-1.5.5.jar [new file with mode: 0644]
app/src/main/java/ai/suanzi/rtmpclient/LogUtil.java [new file with mode: 0644]
app/src/main/java/ai/suanzi/rtmpclient/MailUtil.java [new file with mode: 0644]
app/src/main/java/ai/suanzi/rtmpclient/MainActivity.java

index 6e4aebf..618570c 100644 (file)
@@ -33,5 +33,7 @@ dependencies {
     androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
     implementation files('libs/log4j-1.2.17.jar')
     implementation files('libs/android-logging-log4j-1.0.3.jar')
+    implementation files('libs/android-mail-1.5.5.jar')
+    implementation files('libs/android-activation-1.5.5.jar')
     implementation 'com.android.support:design:27.1.1'
 }
diff --git a/app/libs/android-activation-1.5.5.jar b/app/libs/android-activation-1.5.5.jar
new file mode 100644 (file)
index 0000000..71ea461
Binary files /dev/null and b/app/libs/android-activation-1.5.5.jar differ
diff --git a/app/libs/android-mail-1.5.5.jar b/app/libs/android-mail-1.5.5.jar
new file mode 100644 (file)
index 0000000..de71d4e
Binary files /dev/null and b/app/libs/android-mail-1.5.5.jar differ
diff --git a/app/src/main/java/ai/suanzi/rtmpclient/LogUtil.java b/app/src/main/java/ai/suanzi/rtmpclient/LogUtil.java
new file mode 100644 (file)
index 0000000..2240df4
--- /dev/null
@@ -0,0 +1,136 @@
+package ai.suanzi.rtmpclient;
+
+import android.content.Intent;
+
+import org.apache.log4j.Level;
+import org.apache.log4j.Logger;
+import de.mindpipe.android.logging.log4j.LogConfigurator;
+import java.util.Properties;
+import javax.mail.Transport;
+import javax.mail.Session;
+import javax.mail.internet.MimeMessage;
+import javax.mail.internet.InternetAddress;
+import java.util.Date;
+import android.os.StrictMode;
+
+import java.io.File;
+
+public class LogUtil {
+
+    private static Logger gLogger;
+    private static final String FILE_NAME = "log.txt";
+    private static final long MAX_SIZE = 1024 * 1024 * 10; // 10M
+    private static String logFile = "";
+
+
+    public static void config(File dir) {
+        try {
+            final LogConfigurator logConfigurator = new LogConfigurator();
+            //String fname = getExternalFilesDir(null) + File.separator + "log.txt";
+            logFile = dir + File.separator + FILE_NAME;
+            logConfigurator.setFileName(logFile);
+            logConfigurator.setRootLevel(Level.DEBUG);
+            logConfigurator.setLevel("org.apache", Level.ERROR);
+            logConfigurator.setMaxFileSize(MAX_SIZE);
+            logConfigurator.configure();
+            gLogger = Logger.getLogger("LogUtil");
+            gLogger.error("#######################################");
+            gLogger.debug("Log file is located at: " + logFile);
+
+        } catch (Exception e){
+            e.printStackTrace();
+        }
+        try {
+
+
+            int SDK_INT = android.os.Build.VERSION.SDK_INT;
+            if (SDK_INT > 8)
+            {
+                StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
+                        .permitAll().build();
+                StrictMode.setThreadPolicy(policy);
+                //your codes here
+                MailUtil.sendMail();
+
+
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
+
+
+
+}
+
+
+//
+//
+//    public void sendEmail()
+//    {
+//
+//        private static final int PICK_FROM_GALLERY = 101;
+//        int columnIndex;
+//        try
+//        {
+//
+//
+//
+//            final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
+//            emailIntent.setType("plain/text");
+//            emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,new String[] { "Email" });
+//            emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject");
+//            if (URI != null) {
+//                emailIntent.putExtra(Intent.EXTRA_STREAM, URI);
+//            }
+//            emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "This is a message");
+//            this.startActivity(Intent.createChooser(emailIntent,"Sending email..."));
+//        }
+//        catch (Throwable t)
+//        {
+//            Toast.makeText(this, "Request failed try again: " + t.toString(),Toast.LENGTH_LONG).show();
+//        }
+//    }
+//    public void openFolder()
+//    {
+//        Intent intent = new Intent();
+//        intent.setType("image/*");
+//        intent.setAction(Intent.ACTION_GET_CONTENT);
+//        intent.putExtra("return-data", true);
+//        startActivityForResult(Intent.createChooser(intent, "Complete action using"), PICK_FROM_GALLERY);
+//    }
+//
+//    public static sendLogs(){
+//
+//    }
+//
+//    public void SendLoagcatMail(){
+//
+//        // save logcat in file
+//        /*File outputFile = new File(Environment.getExternalStorageDirectory(),
+//                "logcat.txt");
+//        try {
+//            Runtime.getRuntime().exec(
+//                    "logcat -f " + outputFile.getAbsolutePath());
+//        } catch (IOException e) {
+//            // TODO Auto-generated catch block
+//            e.printStackTrace();
+//        }*/
+//        String fname = getExternalFilesDir(null) + File.separator + "log.txt";
+//
+//        File logfile = new File(fname);
+//
+//        //send file using email
+//        Intent emailIntent = new Intent(Intent.ACTION_SEND);
+//        emailIntent.setType("vnd.android.cursor.dir/email");
+//        String to[] = {"seudut@gmail.com"};
+//        emailIntent.putExtra(Intent.EXTRA_EMAIL, to);
+//        // the attachment
+//        emailIntent.putExtra(Intent.EXTRA_STREAM, logfile.getAbsolutePath());
+//        // the mail subject
+//        emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
+//        startActivity(Intent.createChooser(emailIntent , "Send email..."));
+//    }
+
+//}
diff --git a/app/src/main/java/ai/suanzi/rtmpclient/MailUtil.java b/app/src/main/java/ai/suanzi/rtmpclient/MailUtil.java
new file mode 100644 (file)
index 0000000..bf10280
--- /dev/null
@@ -0,0 +1,72 @@
+package ai.suanzi.rtmpclient;
+
+import java.util.Date;
+import java.util.Properties;
+
+import javax.mail.Session;
+import javax.mail.Transport;
+import javax.mail.internet.InternetAddress;
+import javax.mail.internet.MimeMessage;
+import android.util.Log;
+
+public class MailUtil {
+
+    /**
+     * 创建一封只包含文本的简单邮件
+     *
+     * @param session 和服务器交互的会话
+     * @param sendMail 发件人邮箱
+     * @param receiveMail 收件人邮箱
+     * @return
+     * @throws Exception
+     */
+    public static MimeMessage createMimeMessage(Session session, String sendMail, String receiveMail) throws Exception {
+        // 1. 创建一封邮件
+        MimeMessage message = new MimeMessage(session);
+        // 2. From: 发件人
+        message.setFrom(new InternetAddress(sendMail, "test", "UTF-8"));
+        // 3. To: 收件人(可以增加多个收件人、抄送、密送)
+        message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(receiveMail, "test", "UTF-8"));
+        // 4. Subject: 邮件主题
+        message.setSubject("test", "UTF-8");
+        // 5. Content: 邮件正文(可以使用html标签)
+        message.setContent("hahaha", "text/html;charset=UTF-8");
+        // 6. 设置发件时间
+        message.setSentDate(new Date());
+        // 7. 保存设置
+        message.saveChanges();
+        return message;
+    }
+
+    public static void sendMail() throws Exception{
+        Log.e("MMMM", "sendMail");
+        // 1. 创建参数配置, 用于连接邮件服务器的参数配置
+        Properties props = new Properties();          // 参数配置
+        props.setProperty("mail.transport.protocol", "smtp");  // 使用的协议(JavaMail规范要求)
+        //props.setProperty("mail.smtp.host", "smtp.163.com");   // 发件人的邮箱的 SMTP 服务器地址
+        props.setProperty("mail.smtp.host", "smtp.exmail.qq.com");   // 发件人的邮箱的 SMTP 服务器地址
+
+        props.setProperty("mail.smtp.auth", "true");      // 需要请求认证
+        //如果遇到ssl类错误,请打开一下代码
+    /*final String smtpPort = "465";
+    props.setProperty("mail.smtp.port", smtpPort);
+    props.setProperty("mail.smtp.socketFactory.class", "javax.NET.ssl.SSLSocketFactory");
+    props.setProperty("mail.smtp.socketFactory.fallback", "false");
+    props.setProperty("mail.smtp.socketFactory.port", smtpPort);*/
+        // 2. 根据配置创建会话对象, 用于和邮件服务器交互
+        Session session = Session.getDefaultInstance(props);
+        // 设置为debug模式, 可以查看详细的发送 log
+        session.setDebug(true);
+        // 3. 创建一封邮件
+        MimeMessage message = MailUtil.createMimeMessage(session, "support@suanzi.ai", "support@suanzi.ai");//我这里是以163邮箱为发信邮箱测试通过
+        // 4. 根据 Session 获取邮件传输对象
+        Transport transport = session.getTransport();
+        transport.connect("support@suanzi.ai", "oqiDX8fcWa58CmNf");
+        // 6. 发送邮件, 发到所有的收件地址, message.getAllRecipients() 获取到的是在创建邮件对象时添加的所有收件人, 抄送人, 密送人
+        transport.sendMessage(message, message.getAllRecipients());
+        // 7. 关闭连接
+        transport.close();
+    }
+
+
+}
index 16c39d9..a0b4cc1 100644 (file)
@@ -51,24 +51,6 @@ public class MainActivity extends AppCompatActivity implements MyService.MyServi
 
     private CameraView mCameraView;
 
-    private void configLog(){
-        try {
-            final LogConfigurator logConfigurator = new LogConfigurator();
-            String fname = getExternalFilesDir(null) + File.separator + "log.txt";
-            logConfigurator.setFileName(fname);
-            logConfigurator.setRootLevel(Level.DEBUG);
-            logConfigurator.setLevel("org.apache", Level.ERROR);
-            logConfigurator.setMaxFileSize(1024 * 1024 * 10);
-            logConfigurator.configure();
-            gLogger = Logger.getLogger(getClass());
-            gLogger.error("#######################################");
-            gLogger.debug("Log file is located at: " + fname);
-
-        } catch (Exception e){
-            e.printStackTrace();
-        }
-    }
-
     private void init(){
         // set config file
         UserInfo.setConfigPath(getExternalFilesDir(null) + File.separator + "config");
@@ -131,7 +113,7 @@ public class MainActivity extends AppCompatActivity implements MyService.MyServi
                 doBindService();
             }
         });
-        mServiceHealthMonitor.setInterval(INTERVAL); // 5 minutes
+        mServiceHealthMonitor.setInterval(INTERVAL);
     }
 
 
@@ -153,7 +135,6 @@ public class MainActivity extends AppCompatActivity implements MyService.MyServi
             mServer = mLocalBinder.getServiceInstance();
             mServer.setServiceEventListener(MainActivity.this);
             if(mServer.setRtmpUrl(UserInfo.getConfig().toUrl(mMacAddr))){
-                //mServer.startPreview(mHolder);
                 mServer.startPreview(mCameraView.getHolder());
                 Camera.Size cs = mServer.getBestPictureSize();
                 mCameraView.setLayout(cs.width, cs.height);
@@ -208,8 +189,10 @@ public class MainActivity extends AppCompatActivity implements MyService.MyServi
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
-        configLog();
-        //gLogger.debug("#######################################");
+        LogUtil.config(getExternalFilesDir(null));
+        gLogger = Logger.getLogger(getClass());
+        gLogger.error("#######################################");
+        //configLog();
         initCameraView();
         init();
         loadConfig();
@@ -279,6 +262,7 @@ public class MainActivity extends AppCompatActivity implements MyService.MyServi
     private String getMacAddr() {
         WifiManager manager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
         WifiInfo info = manager.getConnectionInfo();
+        gLogger.debug("Mac Address is " + info.getMacAddress());
         return info.getMacAddress().replace(":", ""); //02:00:00:00:00:00 - 020000000000
     }