android发送邮件

android studio邮件开发记录

1.代码

MyAuthenticator.java

/*
 * Copyright (c)2016 thatluck
 * 版权所有,违者必究
 */

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
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);
    }
}



MailService.java

/*
 * Copyright (c)2016 thatluck
 * 版权所有,违者必究
 */




import java.io.IOException;

import java.io.InputStream;
import java.util.Date;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class MailService {

    public static void send_email(String content) throws IOException, MessagingException{

        Properties properties = new Properties();
        String to = "收信地址";
        String subject ="提交日志";

       

        properties.put("mail.smtp.host", "smtp.qq.com");
        properties.put("mail.smtp.port", "465");
        properties.setProperty("mail.debug", "true");
        properties.setProperty("mail.smtp.auth", "true");  
          
        // 协议名称设置为smtps,会使用SSL  
        properties.setProperty("mail.transport.protocol", "smtps"); 
        properties.put("mail.smtp.ssl.enable", "true"); 

        properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        properties.put("mail.smtp.socketFactory.fallback", "false");
        properties.put("mail.smtp.socketFactory.port", properties.get("mail.smtp.port").toString());
        MyAuthenticator authenticator = new MyAuthenticator("发送地址", "授权码");
        javax.mail.Session sendMailSession = javax.mail.Session.getDefaultInstance(properties, authenticator);
        MimeMessage mailMessage = new MimeMessage(sendMailSession);
        mailMessage.setFrom(new InternetAddress("发送地址"));
       
        mailMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
        mailMessage.setSubject(subject, "UTF-8");
        mailMessage.setSentDate(new Date());
        mailMessage.setText(content);
        Transport.send(mailMessage);
    }
}


build.gradle文件中配置dependenies信息如下:

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.sun.mail:android-mail:1.5.5'
    compile 'com.sun.mail:android-activation:1.5.5'
}


2.混淆签名打包

......
Warning:Ignoring InnerClasses attribute for an anonymous inner class
(javax.activation.DataHandler$1) that doesn't come with an
associated EnclosingMethod attribute. This class was probably produced by a
compiler that did not target the modern .class file format. The recommended
solution is to recompile the class from source, using an up-to-date compiler
and without specifying any "-target" type options. The consequence of ignoring
this warning is that reflective operations on this class will incorrectly
indicate that it is *not* an inner class.
......

解决方案:

升级androidstudio到最新版本2.1.2

Build时使用InstantRun功能构建程序

然后会出现下面的新的问题:

......
ProGuard, version 5.2.1
Reading input...
Reading program jar [D:\Android\AndroidStudioProjects\Zhiling\app\libs\VoiceRecognition-2.0.1.jar] (filtered)
Reading program jar [D:\android-sdk\extras\android\m2repository\com\android\support\support-annotations\23.2.1\support-annotations-23.2.1.jar] (filtered)
Reading program jar [C:\Users\Administrator\.gradle\caches\modules-2\files-2.1\com.sun.mail\android-mail\1.5.5\53a2f927b235acb565dbe9314ececfb7e6d43020\android-mail-1.5.5.jar] (filtered)
Reading program jar [C:\Users\Administrator\.gradle\caches\modules-2\files-2.1\com.sun.mail\android-activation\1.5.5\5442d5e05808145434b750c9f8a0c3f537383c5a\android-activation-1.5.5.jar] (filtered)
Reading program jar [D:\Android\AndroidStudioProjects\Zhiling\app\build\intermediates\exploded-aar\com.android.support\appcompat-v7\23.2.1\jars\classes.jar] (filtered)
Reading program jar [D:\Android\AndroidStudioProjects\Zhiling\app\build\intermediates\exploded-aar\com.android.support\animated-vector-drawable\23.2.1\jars\classes.jar] (filtered)
Reading program jar [D:\Android\AndroidStudioProjects\Zhiling\app\build\intermediates\exploded-aar\com.android.support\support-vector-drawable\23.2.1\jars\classes.jar] (filtered)
Reading program jar [D:\Android\AndroidStudioProjects\Zhiling\app\build\intermediates\exploded-aar\com.google.android.gms\play-services-appindexing\8.1.0\jars\classes.jar] (filtered)
Reading program jar [D:\Android\AndroidStudioProjects\Zhiling\app\build\intermediates\exploded-aar\com.google.android.gms\play-services-basement\8.1.0\jars\classes.jar] (filtered)
Reading program jar [D:\Android\AndroidStudioProjects\Zhiling\app\build\intermediates\exploded-aar\com.android.support\support-v4\23.2.1\jars\classes.jar] (filtered)
Reading program jar [D:\Android\AndroidStudioProjects\Zhiling\app\build\intermediates\exploded-aar\com.android.support\support-v4\23.2.1\jars\libs\internal_impl-23.2.1.jar] (filtered)
Reading program jar [D:\Android\AndroidStudioProjects\Zhiling\app\build\intermediates\transforms\mergeJavaRes\android360\release\jars\2\1f\main.jar] (filtered)
Reading program directory [D:\Android\AndroidStudioProjects\Zhiling\app\build\intermediates\classes\android360\release] (filtered)
Reading library jar [D:\android-sdk\platforms\android-23\android.jar]
Reading library jar [D:\android-sdk\platforms\android-23\optional\org.apache.http.legacy.jar]
Note: duplicate definition of library class [android.net.http.SslCertificate]
Note: duplicate definition of library class [android.net.http.SslError]
Note: duplicate definition of library class [android.net.http.SslCertificate$DName]
Note: duplicate definition of library class [org.apache.http.conn.scheme.SocketFactory]
Note: duplicate definition of library class [org.apache.http.conn.scheme.HostNameResolver]
Note: duplicate definition of library class [org.apache.http.conn.ConnectTimeoutException]
Note: duplicate definition of library class [org.apache.http.params.HttpParams]
Note: there were 7 duplicate class definitions.
      (http://proguard.sourceforge.net/manual/troubleshooting.html#duplicateclass)
Initializing...
Note: the configuration refers to the unknown class 'com.google.vending.licensing.ILicensingService'
Note: the configuration refers to the unknown class 'com.android.vending.licensing.ILicensingService'
Note: the configuration refers to the unknown class 'com.google.vending.licensing.ILicensingService'
Note: the configuration refers to the unknown class 'com.android.vending.licensing.ILicensingService'
Note: android.support.v4.app.NotificationCompatJellybean calls 'Field.getType'
Note: com.google.android.gms.internal.zzsf calls 'Field.getType'
......
Warning:com.sun.mail.handlers.handler_base: can't find referenced method 'boolean equals(java.awt.datatransfer.DataFlavor)' in program class javax.activation.ActivationDataFlavor
Warning:com.sun.mail.handlers.handler_base: can't find referenced class java.awt.datatransfer.DataFlavor


......


Note: there were 14 unresolved dynamic references to classes or interfaces.
      You should check if you need to specify additional program jars.
      (http://proguard.sourceforge.net/manual/troubleshooting.html#dynamicalclass)
Warning:there were 1 unresolved references to classes or interfaces.
         You may need to add missing library jars or update their versions.
         If your code works fine without the missing classes, you can suppress
         the warnings with '-dontwarn' options.
         (http://proguard.sourceforge.net/manual/troubleshooting.html#unresolvedclass)
Warning:there were 1 unresolved references to program class members.
         Your input classes appear to be inconsistent.
         You may need to recompile the code.
         (http://proguard.sourceforge.net/manual/troubleshooting.html#unresolvedprogramclassmember)
Warning:Exception while processing task java.io.IOException: Please correct the above warnings first.
:app:transformClassesAndResourcesWithProguardForAndroid360Release FAILED
Error:Execution failed for task ':app:transformClassesAndResourcesWithProguardForAndroid360Release'.
> java.io.IOException: Please correct the above warnings first.
Information:BUILD FAILED
Information:Total time: 17.929 secs
Information:1 error
Information:5 warnings
Information:See complete output in console
解决方案:

proguardFile中添加如下设置

-keep class org.apache.** { *; }
-keep class com.sun.** { *; }
-dontwarn com.sun.mail.handlers.handler_base


注意:

AndroidStudio版本号:2.1.2

设置InstanceRun模式调试程序

InstanceRun的介绍可以参考:

1.Instant Run


2.Android Studio2.0的逆天功能Instant Run





你可能感兴趣的:(android,邮件开发,android发送邮件,android,studio发送邮件,android,studio,android,邮件)