使用SendGrid从Android应用程序发送电子邮件时出错

时间:2014-07-26 21:42:40

标签: android apache-httpclient-4.x sendgrid

在我的应用中尝试使用发送网格发送电子邮件。

在我的AsyncTask的doInBackground方法中,我有以下代码

            SendGrid sendgrid = new SendGrid("XXXXXX", "YYYYYY");
            publishProgress("Preparing mail message....");
            SendGrid.Email email = new SendGrid.Email();
            email.setFrom(args[0].toString());
            email.addTo(args[2].toString());
            email.setSubject(args[3].toString());
            email.setText(args[4].toString());
            // androidEmail.createEmailMessage();
            publishProgress("Sending email....");
            try {
                sendgrid.send(email);
            } catch (SendGridException e) {
                System.out.println(e);
                Log.i("SendGrid Exception", "Mail Not Sent.");
            }
            publishProgress("Email Sent.");

当我辜负他时,我得到以下错误:

 Caused by: java.lang.NoSuchFieldError:        org.apache.http.message.BasicLineFormatter.INSTANCE
 at org.apache.http.impl.io.DefaultHttpRequestWriterFactory.<init>(DefaultHttpRequestWriterFactory.java:52)
 at org.apache.http.impl.io.DefaultHttpRequestWriterFactory.<init>(DefaultHttpRequestWriterFactory.java:56)
 at org.apache.http.impl.io.DefaultHttpRequestWriterFactory.<clinit>(DefaultHttpRequestWriterFactory.java:46)
 at org.apache.http.impl.conn.ManagedHttpClientConnectionFactory.<init>(ManagedHttpClientConnectionFactory.java:72)
 at org.apache.http.impl.conn.ManagedHttpClientConnectionFactory.<init>(ManagedHttpClientConnectionFactory.java:84)
 at org.apache.http.impl.conn.ManagedHttpClientConnectionFactory.<clinit>(ManagedHttpClientConnectionFactory.java:59)
 at   org.apache.http.impl.conn.PoolingHttpClientConnectionManager$InternalConnectionFactory.  <init>(PoolingHttpClientConnectionManager.java:493)
 at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.<init>(PoolingHttpClientConnectionManager.java:149)
 at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.<init>(PoolingHttpClientConnectionManager.java:138)
 at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.<init>(PoolingHttpClientConnectionManager.java:114)
 at org.apache.http.impl.client.HttpClientBuilder.build(HttpClientBuilder.java:726)
 at com.sendgrid.SendGrid.<init>(SendGrid.java:59)

我从他们的github页面下载send-grod.jar。我用示例java程序测试了它,我可以成功发送邮件。但它在Android中不起作用。

我缺少什么。?

1 个答案:

答案 0 :(得分:1)

我理解这种行为的原因。

send-grid使用的某些依赖库在android上不起作用。

所以我使用maven来构建我自己的jar,其中包含所有需要在android上工作的库。

最后,我可以从我的应用发送电子邮件。

这两个链接:first!和second!很有帮助。