所以我可以使用我的Gmail帐户在应用程序中使用AmazonSES成功发送不带附件的电子邮件,但是我很难找到一种解决方案来发送带附件的电子邮件。亚马逊提供了通过Java进行操作的说明,但是当我尝试执行操作时,我总是遇到错误。
我到了需要导入org.apache.httpcomponents的地步,但这没有用,因为我需要为其获取Android端口。现在我遇到了:
Caused by: java.lang.VerifyError: Verifier rejected class com.amazonaws.http.apache.request.impl.ApacheHttpRequestFactory: org.apache.http.client.methods.HttpRequestBase com.amazonaws.http.apache.request.impl.ApacheHttpRequestFactory.createApacheRequest(com.amazonaws.Request, java.lang.String, java.lang.String) failed to verify: org.apache.http.client.methods.HttpRequestBase com.amazonaws.http.apache.request.impl.ApacheHttpRequestFactory.createApacheRequest(com.amazonaws.Request, java.lang.String, java.lang.String): [0x43] register v0 has type Precise Reference: org.apache.http.client.methods.HttpPatch but expected Reference: org.apache.http.client.methods.HttpEntityEnclosingRequestBase (declaration of 'com.amazonaws.http.apache.request.impl.ApacheHttpRequestFactory' appears in /data/app/e.my.package-ki0yo2nHePW9QH2mDg31Kw==/split_lib_dependencies_apk.apk)
好的,然后我发现我需要一个用于AWS开发工具包的Android端口。然后,我在依赖项中添加了aws-android-sdk-ses-2.7.4.jar。现在,在我的代码中,我缺少这两个导入的依赖关系:
import com.amazonaws.regions.Regions;
import com.amazonaws.services.simpleemail.AmazonSimpleEmailServiceClientBuilder;
我不确定从这里去哪里。在添加Android AWS开发工具包之前,我调用了sendEmail()方法之前,代码似乎运行良好。由于缺少两个输入,我现在无法编译。
这是我的类中的doInBackground()方法,该方法扩展了AsyncTask:
@Override
protected String doInBackground(Object... params) {
try {
Log.i("SendMail", "About to instantiate GMail...");
AmazonSES androidEmail = new AmazonSES(params[0].toString(),
params[1].toString(), (FragCommunication) params[2]);
androidEmail.createEmailMessage();
publishProgress("Sending order....");
androidEmail.sendEmail();
publishProgress("Order Sent!");
Log.i("SendMail", "Order Sent.");
} catch (Exception e) {
Handler h = new Handler(Looper.getMainLooper());
h.post(new Runnable(){
public void run() {
Toast.makeText(sendMailActivityWeak.get(), "Failed to send order. Please try again later.",Toast.LENGTH_LONG).show();
}
});
}
return null;
}
这是我的AmazonSES代码:
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.widget.Toast;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.lang.ref.WeakReference;
import java.nio.ByteBuffer;
import java.security.Security;
import java.util.List;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.simpleemail.AmazonSimpleEmailService;
import com.amazonaws.services.simpleemail.AmazonSimpleEmailServiceClientBuilder;
import com.amazonaws.services.simpleemail.model.RawMessage;
import com.amazonaws.services.simpleemail.model.SendRawEmailRequest;
public class AmazonSES {
private FragCommunication mCallback;
static final private String FROM = "removed";
static final private String FROMNAME = "removed";
static final private String SMTP_USERNAME = "removed";
static final private String SMTP_PASSWORD = "removed";
static final private String HOST = "email-smtp.us-west-2.amazonaws.com";
static final private String PORT = "587";
private Properties props;
private Session session;
private MimeMessage message;
// private List<String> to;
private String subject;
private String body;
private WeakReference<Activity> activityWeak;
private String toEmail = "removed";
private static String CONFIGURATION_SET = "ConfigSet";
public AmazonSES(String subject, String body, FragCommunication mCallback) {
this.subject = subject;
this.body = body;
this.mCallback = mCallback;
props = System.getProperties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.port", PORT);
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");
Log.i("AmazonSES", "Mail server properties set.");
}
//
public MimeMessage createEmailMessage() throws AddressException,
MessagingException, UnsupportedEncodingException {
Log.i("Creating Message", "Beginning");
this.session = Session.getDefaultInstance(props);
message = new MimeMessage(this.session);
Log.i("Creating Message", "Setting sender/receiver");
message.setSubject(subject, "UTF-8");
message.setFrom(new InternetAddress(FROM, FROMNAME));
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(toEmail));
MimeMultipart msg_body = new MimeMultipart("alternative");
MimeBodyPart wrap = new MimeBodyPart();
Log.i("Creating Message", "Creating Body");
// Define the HTML part.
MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(body,"text/html");
msg_body.addBodyPart(htmlPart);
Log.i("Creating Message", "Adding body");
// Add the child container to the wrapper object.
wrap.setContent(msg_body);
MimeMultipart msg = new MimeMultipart("mixed");
message.setContent(msg);
msg.addBodyPart(wrap);
Log.i("Creating Message", "Creating attachment");
MimeBodyPart att = new MimeBodyPart();
DataSource fds = new FileDataSource(mCallback.getFilepath());
att.setDataHandler(new DataHandler(fds));
att.setFileName(fds.getName());
Log.i("Creating Message", "Adding attachment");
msg.addBodyPart(att);
Log.i("AmazonSES", "E-mail message created.");
return message;
}
public void sendEmail() {
Log.i("Send", "Message sending");
try {
System.out.println("Attempting to send an email through Amazon SES "
+"using the AWS SDK for Java...");
// Instantiate an Amazon SES client, which will make the service
// call with the supplied AWS credentials.
AmazonSimpleEmailService client =
AmazonSimpleEmailServiceClientBuilder.standard()
// Replace US_WEST_2 with the AWS Region you're using for
// Amazon SES.
.withRegion(Regions.US_WEST_2).build();
// Print the raw email content on the console
// PrintStream out = System.out;
// message.writeTo(out);
// Send the email.
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
message.writeTo(outputStream);
RawMessage rawMessage =
new RawMessage(ByteBuffer.wrap(outputStream.toByteArray()));
SendRawEmailRequest rawEmailRequest =
new SendRawEmailRequest(rawMessage)
.withConfigurationSetName(CONFIGURATION_SET);
client.sendRawEmail(rawEmailRequest);
System.out.println("Email sent!");
showEndFragment();
// Display an error if something goes wrong.
} catch (Exception ex) {
System.out.println("Email Failed");
System.err.println("Error message: " + ex.getMessage());
ex.printStackTrace();
}
}
public void showEndFragment() {
Fragment frag = new EndFragment();
mCallback.replaceFragment(frag);
}
}
我的依赖项:
dependencies {
def lifecycle_version = "1.1.1"
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support:design:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:support-vector-drawable:26.1.0'
implementation 'com.android.support:support-v4:26.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.android.support:cardview-v7:26.1.0'
implementation files('libs/activation.jar')
implementation files('libs/additionnal.jar')
implementation files('libs/mail.jar')
implementation "android.arch.lifecycle:extensions:$lifecycle_version"
implementation 'com.android.support:support-annotations:28.0.0'
implementation(
[group: 'org.apache.httpcomponents', name: 'httpclient-android', version: '4.3.5.1']
)
implementation(
[group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.9.7'],
[group: 'com.fasterxml.jackson.core', name: 'jackson-annotations', version: '2.9.7'],
[group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.9.7']
)
implementation files('/Users/user/Apps/AppName/gradle/aws-android-sdk-ses-2.7.4.jar')
}
我将不胜感激!