如何修复使用Mandrill API的崩溃应用?

时间:2018-10-06 12:05:11

标签: android api mandrill

我正在尝试使用通过Mandrill API和flat构建的Android应用发送电子邮件。而且我已经在Mandrill中注册了我的应用程序,并且从Mandrill网站上获得了API密钥,然后将其放入我的应用程序中,但是该应用程序崩溃了。

我的设备(仿真器)已经连接到互联网。

这是我的API密钥状态:

this API

这是我的方法和代码

        public void sendEmail(){


            MandrillMessage allMessage = new MandrillMessage("545b0acfc446e44f6734be###########");
            // create your message
            EmailMessage message = new EmailMessage();
            message.setFromEmail("info@mydocapp.net");
            message.setFromName("buddy");
            message.setHtml("<p>Your appointment added was successfully</p>");
            message.setText("Your appointment date was fixed to 2018");
            message.setSubject("Your appointment added was successfully");

            // add recipients
            Recipient recipient = new Recipient();
            List<Recipient> recipients = new ArrayList<Recipient>();
            recipient.setEmail("sahanpasindu07@gmail.com");
            recipient.setName("Sahan");
            recipients.add(recipient);

            // add attachment if you want
    //        Attachment attachment = new Attachment();
    //        List<Attachment> attachments = new ArrayList<Attachment>();
    //
    //        attachment.setType("image/png");
    //        attachment.setName("Image name");
    //        attachment.setContent(" set your base64 encode of your file");
    //        attachments.add(attachment);

            message.setTo(recipients);
    //        message.setImages(attachments);

            allMessage.setMessage(message);
            allMessage.send();
        }

我的logcat是

> 10-06 17:22:06.231 3871-4223/com.me.doctor.doctor_me E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #2
    Process: com.me.doctor.doctor_me, PID: 3871
    java.lang.RuntimeException: An error occured while executing doInBackground()
        at android.os.AsyncTask$3.done(AsyncTask.java:304)
        at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
        at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
        at java.util.concurrent.FutureTask.run(FutureTask.java:242)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
        at java.lang.Thread.run(Thread.java:818)
     Caused by: java.lang.SecurityException: Permission denied (missing INTERNET permission?)
        at java.net.InetAddress.lookupHostByName(InetAddress.java:451)
        at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
        at java.net.InetAddress.getAllByName(InetAddress.java:215)
        at com.android.okhttp.HostResolver$1.getAllByName(HostResolver.java:29)
        at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:232)
        at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:124)
        at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:272)
        at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:211)
        at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:382)
        at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:106)
        at com.android.okhttp.internal.http.DelegatingHttpsURLConnection.connect(DelegatingHttpsURLConnection.java:89)
        at com.android.okhttp.internal.http.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:25)
        at net.soroushjavdan.androidmandrillinterface.MandrillMessage$1.doInBackground(MandrillMessage.java:96)
        at net.soroushjavdan.androidmandrillinterface.MandrillMessage$1.doInBackground(MandrillMessage.java:88)
        at android.os.AsyncTask$2.call(AsyncTask.java:292)
        at java.util.concurrent.FutureTask.run(FutureTask.java:237)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
        at java.lang.Thread.run(Thread.java:818) 
     Caused by: android.system.GaiException: android_getaddrinfo failed: EAI_NODATA (No address associated with hostname)
        at libcore.io.Posix.android_getaddrinfo(Native Method)
        at libcore.io.ForwardingOs.android_getaddrinfo(ForwardingOs.java:55)
        at java.net.InetAddress.lookupHostByName(InetAddress.java:438)
        at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252) 
        at java.net.InetAddress.getAllByName(InetAddress.java:215) 
        at com.android.okhttp.HostResolver$1.getAllByName(HostResolver.java:29) 
        at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:232) 
        at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:124) 
        at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:272) 
        at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:211) 
        at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:382) 
        at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:106) 
        at com.android.okhttp.internal.http.DelegatingHttpsURLConnection.connect(DelegatingHttpsURLConnection.java:89) 
        at com.android.okhttp.internal.http.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:25) 
        at net.soroushjavdan.androidmandrillinterface.MandrillMessage$1.doInBackground(MandrillMessage.java:96) 
        at net.soroushjavdan.androidmandrillinterface.MandrillMessage$1.doInBackground(MandrillMessage.java:88) 
        at android.os.AsyncTask$2.call(AsyncTask.java:292) 
        at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
        at java.lang.Thread.run(Thread.java:818) 
     Caused by: android.system.ErrnoException: android_getaddrinfo failed: EACCES (Permission denied)
        at libcore.io.Posix.android_getaddrinfo(Native Method) 
        at libcore.io.ForwardingOs.android_getaddrinfo(ForwardingOs.java:55) 
        at java.net.InetAddress.lookupHostByName(InetAddress.java:438) 
        at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252) 
        at java.net.InetAddress.getAllByName(InetAddress.java:215) 
        at com.android.okhttp.HostResolver$1.getAllByName(HostResolver.java:29) 
        at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:232) 
        at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:124) 
        at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:272) 
        at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:211) 
        at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:382) 
        at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:106) 
        at com.android.okhttp.internal.http.DelegatingHttpsURLConnection.connect(DelegatingHttpsURLConnection.java:89) 
        at com.android.okhttp.internal.http.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:25) 
        at net.soroushjavdan.androidmandrillinterface.MandrillMessage$1.doInBackground(MandrillMessage.java:96) 
        at net.soroushjavdan.androidmandrillinterface.MandrillMessage$1.doInBackground(MandrillMessage.java:88) 
        at android.os.AsyncTask$2.call(AsyncTask.java:292) 
        at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
        at java.lang.Thread.run(Thread.java:818) 

我知道有一些方法可以实现我的流程,但是我认为Mandrill比Java Mail API容易。但是在Java邮件api中,我注意到了一些错误,并且还需要对邮件设置进行一些更改,因此我选择了Mandrill。我愿意采取更好的方法。

1 个答案:

答案 0 :(得分:1)

此行告诉您问题的原因。

Caused by: java.lang.SecurityException: Permission denied (missing INTERNET permission?)

在您的清单顶部添加<uses-permission android:name="android.permission.INTERNET" />行。