我尝试发送2封电子邮件:一封是应用,一封是后台。我已在背景中使用https://github.com/yesidlazaro/GmailBackground作为电子邮件。
应用程序的电子邮件按钮有效。我在尝试其他按钮时发送邮件时收到错误消息getSlotFromBufferLocked:未知缓冲区:0xb2bba960。我不确定我的其他按钮出了什么问题。我改变了一点&它仍然没有工作&我已经证明了这些错误。我有什么想法吗?
public class Sad extends AppCompatActivity {
Button button2;
Button button3;
final Context context = this;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sad);
}
public void onClick (View v) {
switch (v.getId()) {
case R.id.button2:
Intent h = new Intent(Intent.ACTION_SEND);
h.setType("message/rfc822");
h.putExtra(Intent.EXTRA_EMAIL , new String[]{"email"});
h.putExtra(Intent.EXTRA_SUBJECT, "Something bothers me");
h.putExtra(Intent.EXTRA_TEXT , "body of email");
try {
startActivity(Intent.createChooser(h, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(Sad.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
}
break;
case R.id.button3:
BackgroundMail.newBuilder(this)
.withUsername("*****@gmail.com")
.withPassword("p/w")
.withMailto("*********")
.withSubject("Something bothers me")
.withBody("this is the body")
.withOnSuccessCallback(new BackgroundMail.OnSuccessCallback() {
@Override
public void onSuccess() {
//do some magic
}
})
.withOnFailCallback(new BackgroundMail.OnFailCallback() {
@Override
public void onFail() {
//do some magic
}
})
.send();
}
;
}}
这一点真的令人困惑,它似乎有助于两种不同的方式来发送电子邮件&一些代码未使用。
public class BackgroundMail {
String TAG = "BackgroundMail";
private String username;
private String password;
private String mailto;
private String subject;
private String body;
private String sendingMessage;
private String sendingMessageSuccess;
private String sendingMessageError;
private boolean processVisibility = true;
private ArrayList<String> attachments = new ArrayList<>();
private Context mContext;
private OnSuccessCallback onSuccessCallback;
private OnFailCallback onFailCallback;
public interface OnSuccessCallback {
void onSuccess();
}
public interface OnFailCallback {
void onFail();
}
public BackgroundMail(Fragment fragment) {
this(fragment.getActivity().getApplicationContext());
}
public BackgroundMail(Context context) {
this.mContext = context;
this.sendingMessage = context.getString(R.string.msg_sending_email);
this.sendingMessageSuccess = context.getString(R.string.msg_email_sent_successfully);
this.sendingMessageError=context.getString(R.string.msg_error_sending_email);
}
private BackgroundMail(Builder builder) {
mContext = builder.context;
attachments = builder.attachments;
username = builder.username;
password = builder.password;
mailto = builder.mailto;
subject = builder.subject;
body = builder.body;
setSendingMessage(builder.sendingMessage);
setSendingMessageSuccess(builder.sendingMessageSuccess);
setSendingMessageError(builder.sendingMessageError);
processVisibility = builder.processVisibility;
setOnSuccessCallback(builder.onSuccessCallback);
setOnFailCallback(builder.onFailCallback);
}
public static Builder newBuilder(Context context) {
return new Builder(context);
}
public static Builder newBuilder(Fragment fragment) {
return new Builder(fragment.getActivity().getApplicationContext());
}
public void setGmailUserName(@NonNull String string) {
this.username = string;
}
public void setGmailUserName(@StringRes int strRes) {
this.username = mContext.getResources().getString(strRes);
}
@NonNull
public String getGmailUserName() {
return username;
}
public void setGmailPassword(@NonNull String string) {
this.password = string;
}
public void setGmailPassword(@StringRes int strRes) {
this.password = mContext.getResources().getString(strRes);
}
@NonNull
public String getGmailPassword() {
return password;
}
public void showVisibleProgress(boolean state) {
this.processVisibility = state;
}
public boolean isProgressVisible() {
return processVisibility;
}
public void setMailTo(@NonNull String string) {
this.mailto = string;
}
public void setMailTo(@StringRes int strRes) {
this.mailto = mContext.getResources().getString(strRes);
}
@NonNull
public String getMailTo() {
return mailto;
}
public void setFormSubject(@NonNull String string) {
this.subject = string;
}
public void setFormSubject(@StringRes int strRes) {
this.subject = mContext.getResources().getString(strRes);
}
@NonNull
public String getFormSubject() {
return subject;
}
public void setFormBody(@NonNull String string) {
this.body = string;
}
public void setFormBody(@StringRes int strRes) {
this.body = mContext.getResources().getString(strRes);
}
@NonNull
public String getFormBody() {
return body;
}
public void setSendingMessage(@NonNull String string) {
this.sendingMessage = string;
}
public void setSendingMessage(@StringRes int strRes) {
this.sendingMessage = mContext.getResources().getString(strRes);
}
@NonNull
public String getSendingMessage() {
return sendingMessage;
}
public void setSendingMessageSuccess(@Nullable String string) {
this.sendingMessageSuccess = string;
}
public void setSendingMessageSuccess(@StringRes int strRes) {
this.sendingMessageSuccess = mContext.getResources().getString(strRes);
}
@Nullable
public String getSendingMessageSuccess() {
return sendingMessageSuccess;
}
public void setSendingMessageError(@Nullable String string) {
this.sendingMessageError = string;
}
public void setSendingMessageError(@StringRes int strRes) {
this.sendingMessageError = mContext.getResources().getString(strRes);
}
@Nullable
public String getSendingMessageError() {
return sendingMessageError;
}
public void setOnSuccessCallback(OnSuccessCallback onSuccessCallback) {
this.onSuccessCallback = onSuccessCallback;
}
public void setOnFailCallback(OnFailCallback onFailCallback) {
this.onFailCallback = onFailCallback;
}
public void send() {
new SendEmailTask().execute();
}
public class SendEmailTask extends AsyncTask<String, Void, Boolean> {
private ProgressDialog progressDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
if (processVisibility) {
progressDialog = new ProgressDialog(mContext);
progressDialog.setMessage(sendingMessage);
progressDialog.setCancelable(false);
progressDialog.show();
}
}
@Override
protected Boolean doInBackground(String... arg0) {
try {
GmailSender sender = new GmailSender(username, password);
if (!attachments.isEmpty()) {
for (int i = 0; i < attachments.size(); i++) {
if (!attachments.get(i).isEmpty()) {
sender.addAttachment(attachments.get(i));
}
}
}
sender.sendMail(subject, body, username, mailto);
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
if (processVisibility) {
progressDialog.dismiss();
if (result) {
if (!TextUtils.isEmpty(sendingMessageSuccess)) {
Toast.makeText(mContext, sendingMessageSuccess, Toast.LENGTH_SHORT).show();
}
if (onSuccessCallback != null) {
onSuccessCallback.onSuccess();
}
}else {
if (!TextUtils.isEmpty(sendingMessageError)) {
Toast.makeText(mContext, sendingMessageError, Toast.LENGTH_SHORT).show();
}
if (onFailCallback != null) {
onFailCallback.onFail();
}
}
}
}
}
public static final class Builder {
private Context context;
private String username;
private String password;
private String mailto;
private String subject;
private String body;
private ArrayList<String> attachments = new ArrayList<>();
private String sendingMessage;
private String sendingMessageSuccess;
private String sendingMessageError;
private boolean processVisibility;
private OnSuccessCallback onSuccessCallback;
private OnFailCallback onFailCallback;
private Builder(Context context) {
this.context = context;
this.sendingMessage = context.getString(R.string.msg_sending_email);
this.sendingMessageSuccess = context.getString(R.string.msg_email_sent_successfully);
this.sendingMessageError=context.getString(R.string.msg_error_sending_email);
}
public Builder withUsername(@NonNull String username) {
this.username = username;
return this;
}
public Builder withUsername(@StringRes int usernameRes) {
this.username = context.getResources().getString(usernameRes);
return this;
}
public Builder withPassword(@NonNull String password) {
this.password = password;
return this;
}
public Builder withPassword(@StringRes int passwordRes) {
this.password = context.getResources().getString(passwordRes);
return this;
}
public Builder withMailto(@NonNull String mailto) {
this.mailto = mailto;
return this;
}
public Builder withMailto(@StringRes int mailtoRes) {
this.mailto = context.getResources().getString(mailtoRes);
return this;
}
public Builder withSubject(@NonNull String subject) {
this.subject = subject;
return this;
}
public Builder withSubject(@StringRes int subjectRes) {
this.subject = context.getResources().getString(subjectRes);
return this;
}
public Builder withBody(@NonNull String body) {
this.body = body;
return this;
}
public Builder withBody(@StringRes int bodyRes) {
this.body = context.getResources().getString(bodyRes);
return this;
}
public Builder withAttachments(@NonNull ArrayList<String> attachments) {
this.attachments.addAll(attachments);
return this;
}
public Builder withAttachments(String...attachments) {
this.attachments.addAll(Arrays.asList(attachments));
return this;
}
public Builder withAttachments(@ArrayRes int attachmentsRes) {
this.attachments.addAll(Arrays.asList(context.getResources().getStringArray(attachmentsRes)));
return this;
}
public Builder withSendingMessage(@NonNull String sendingMessage) {
this.sendingMessage = sendingMessage;
return this;
}
public Builder withSendingMessage(@StringRes int sendingMessageRes) {
this.sendingMessage = context.getResources().getString(sendingMessageRes);
return this;
}
public Builder withSendingMessageSuccess(@Nullable String sendingMessageSuccess) {
this.sendingMessageSuccess = sendingMessageSuccess;
return this;
}
public Builder withSendingMessageSuccess(@StringRes int sendingMessageSuccessRes) {
this.sendingMessageSuccess = context.getResources().getString(sendingMessageSuccessRes);
return this;
}
public Builder withSendingMessageError(@Nullable String sendingMessageError) {
this.sendingMessageError = sendingMessageError;
return this;
}
public Builder withSendingMessageError(@StringRes int sendingMessageError) {
this.sendingMessageError = context.getResources().getString(sendingMessageError);
return this;
}
public Builder withProcessVisibility(boolean processVisibility) {
this.processVisibility = processVisibility;
return this;
}
public Builder withOnSuccessCallback(OnSuccessCallback onSuccessCallback) {
this.onSuccessCallback = onSuccessCallback;
return this;
}
public Builder withOnFailCallback(OnFailCallback onFailCallback) {
this.onFailCallback = onFailCallback;
return this;
}
public BackgroundMail build() {
return new BackgroundMail(this);
}
public BackgroundMail send() {
BackgroundMail backgroundMail = build();
backgroundMail.send();
return backgroundMail;
}
}
}
Gradle build
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "queens.feedback"
minSdkVersion 15
targetSdkVersion 23
versionCode 7
versionName "1.7"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:23.0.0'
}
这是完整的错误
07-05 02:56:33.782 2189-2480/queens.feedback W/System.err﹕ at javax.mail.Service.connect(Service.java:169)
07-05 02:56:33.782 2189-2480/queens.feedback W/System.err﹕ at javax.mail.Service.connect(Service.java:118)
07-05 02:56:33.782 2189-2480/queens.feedback W/System.err﹕ at javax.mail.Transport.send0(Transport.java:188)
07-05 02:56:33.783 2189-2480/queens.feedback W/System.err﹕ at javax.mail.Transport.send(Transport.java:118)
07-05 02:56:33.783 2189-2480/queens.feedback W/System.err﹕ at queens.feedback.GmailSender.sendMail(GmailSender.java:84)
07-05 02:56:33.783 2189-2480/queens.feedback W/System.err﹕ at queens.feedback.BackgroundMail$SendEmailTask.doInBackground(BackgroundMail.java:233)
07-05 02:56:33.783 2189-2480/queens.feedback W/System.err﹕ at queens.feedback.BackgroundMail$SendEmailTask.doInBackground(BackgroundMail.java:208)
07-05 02:56:33.783 2189-2480/queens.feedback W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:295)
07-05 02:56:33.783 2189-2480/queens.feedback W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237)
07-05 02:56:33.783 2189-2480/queens.feedback W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
07-05 02:56:33.783 2189-2480/queens.feedback W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
07-05 02:56:33.841 2189-2480/queens.feedback W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
07-05 02:56:33.841 2189-2480/queens.feedback W/System.err﹕ at java.lang.Thread.run(Thread.java:818)
07-05 02:56:33.956 2189-2196/queens.feedback W/art﹕ Suspending all threads took: 14.286ms
07-05 02:56:34.040 2189-2203/queens.feedback W/EGL_emulation﹕ eglSurfaceAttrib not implemented
07-05 02:56:34.040 2189-2203/queens.feedback W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xac335600, error=EGL_SUCCESS
07-05 02:56:34.141 2189-2203/queens.feedback E/Surface﹕ getSlotFromBufferLocked: unknown buffer: 0xaa392020
07-05 02:56:34.681 2189-2480/queens.feedback W/System.err﹕ javax.mail.AuthenticationFailedException
07-05 02:56:34.682 2189-2480/queens.feedback W/System.err﹕ at javax.mail.Service.connect(Service.java:319)
07-05 02:56:34.682 2189-2480/queens.feedback W/System.err﹕ at javax.mail.Service.connect(Service.java:169)
07-05 02:56:34.682 2189-2480/queens.feedback W/System.err﹕ at javax.mail.Service.connect(Service.java:118)
07-05 02:56:34.682 2189-2480/queens.feedback W/System.err﹕ at javax.mail.Transport.send0(Transport.java:188)
07-05 02:56:34.682 2189-2480/queens.feedback W/System.err﹕ at javax.mail.Transport.send(Transport.java:118)
07-05 02:56:34.682 2189-2480/queens.feedback W/System.err﹕ at queens.feedback.GmailSender.sendMail(GmailSender.java:84)
07-05 02:56:34.682 2189-2480/queens.feedback W/System.err﹕ at queens.feedback.BackgroundMail$SendEmailTask.doInBackground(BackgroundMail.java:233)
07-05 02:56:34.682 2189-2480/queens.feedback W/System.err﹕ at queens.feedback.BackgroundMail$SendEmailTask.doInBackground(BackgroundMail.java:208)
07-05 02:56:34.682 2189-2480/queens.feedback W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:295)
07-05 02:56:34.682 2189-2480/queens.feedback W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237)
07-05 02:56:34.682 2189-2480/queens.feedback W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
07-05 02:56:34.682 2189-2480/queens.feedback W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
07-05 02:56:34.682 2189-2480/queens.feedback W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
07-05 02:56:34.683 2189-2480/queens.feedback W/System.err﹕ at java.lang.Thread.run(Thread.java:818)
07-05 02:56:35.251 1304-1335/system_process I/InputReader﹕ Reconfiguring input devices. changes=0x00000010
07-05 02:56:35.474 2544-2544/? W/dex2oat﹕ Unexpected CPU variant for X86 using defaults: x86
07-05 02:56:35.475 2544-2544/? W/dex2oat﹕ Mismatch between dex2oat instruction set features (ISA: X86 Feature string: smp,-ssse3,-sse4.1,-sse4.2,-avx,-avx2) and those of dex2oat executable (ISA: X86 Feature string: smp,ssse3,-sse4.1,-sse4.2,-avx,-avx2) for the command line:
07-05 02:56:35.475 2544-2544/? W/dex2oat﹕ /system/bin/dex2oat --runtime-arg -classpath --runtime-arg --compiler-filter=interpret-only --instruction-set=x86 --instruction-set-features=smp,ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/user/0/com.google.android.gms/app_fb/f.apk --oat-file=/data/user/0/com.google.android.gms/app_fb/f.dex
07-05 02:56:35.475 2544-2544/? I/dex2oat﹕ /system/bin/dex2oat --compiler-filter=interpret-only --dex-file=/data/user/0/com.google.android.gms/app_fb/f.apk --oat-file=/data/user/0/com.google.android.gms/app_fb/f.dex
07-05 02:56:35.539 2189-2480/queens.feedback W/System.err﹕ javax.mail.AuthenticationFailedException
07-05 02:56:35.540 2189-2480/queens.feedback W/System.err﹕ at javax.mail.Service.connect(Service.java:319)
07-05 02:56:35.540 2189-2480/queens.feedback W/System.err﹕ at javax.mail.Service.connect(Service.java:169)
07-05 02:56:35.540 2189-2480/queens.feedback W/System.err﹕ at javax.mail.Service.connect(Service.java:118)
07-05 02:56:35.540 2189-2480/queens.feedback W/System.err﹕ at javax.mail.Transport.send0(Transport.java:188)
07-05 02:56:35.540 2189-2480/queens.feedback W/System.err﹕ at javax.mail.Transport.send(Transport.java:118)
07-05 02:56:35.540 2189-2480/queens.feedback W/System.err﹕ at queens.feedback.GmailSender.sendMail(GmailSender.java:84)
07-05 02:56:35.540 2189-2480/queens.feedback W/System.err﹕ at queens.feedback.BackgroundMail$SendEmailTask.doInBackground(BackgroundMail.java:233)
07-05 02:56:35.540 2189-2480/queens.feedback W/System.err﹕ at queens.feedback.BackgroundMail$SendEmailTask.doInBackground(BackgroundMail.java:208)
07-05 02:56:35.540 2189-2480/queens.feedback W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:295)
07-05 02:56:35.540 2189-2480/queens.feedback W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237)
07-05 02:56:35.540 2189-2480/queens.feedback W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
07-05 02:56:35.540 2189-2480/queens.feedback W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
07-05 02:56:35.540 2189-2480/queens.feedback W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
07-05 02:56:35.540 2189-2480/queens.feedback W/System.err﹕ at java.lang.Thread.run(Thread.java:818)
07-05 02:56:35.569 2544-2544/? I/dex2oat﹕ dex2oat took 94.241ms (threads: 1) arena alloc=0B java alloc=34KB native alloc=674KB free=349KB
07-05 02:56:35.572 2458-2471/com.google.android.gms.unstable W/System﹕ ClassLoader referenced unknown path:
07-05 02:56:35.589 1591-1591/com.google.android.gms.persistent V/GmsNetworkLocationProvi﹕ DISABLE
07-05 02:56:35.598 1591-1591/com.google.android.gms.persistent I/GCoreNlp﹕ shouldConfirmNlp, NLP off. Ensuring opt-in disabled
07-05 02:56:35.624 2458-2471/com.google.android.gms.unstable D/﹕ HostConnection::get() New Host Connection established 0x9c4bd7d0, tid 2471
Gmailsender类
public class GmailSender extends javax.mail.Authenticator
{
private String mailhost = "smtp.gmail.com";
private String user;
private String password;
private Session session;
private Multipart _multipart;
static
{
Security.addProvider(new JSSEProvider());
}
public GmailSender(String user, String password)
{
this.user = user;
this.password = password;
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", mailhost);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.quitwait", "false");
session = Session.getDefaultInstance(props, this);
_multipart = new MimeMultipart();
}
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(user, password);
}
public synchronized void sendMail(String subject, String body, String sender, String recipients) throws Exception
{
MimeMessage message = new MimeMessage(session);
DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain"));
message.setSender(new InternetAddress(sender));
message.setSubject(subject);
message.setText(body);
message.setDataHandler(handler);
if(_multipart.getCount() > 0) {
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(body);
_multipart.addBodyPart(messageBodyPart);
message.setContent(_multipart);
}
if (recipients.indexOf(',') > 0)
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
else
message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
Transport.send(message);
}
public void addAttachment(String filename) throws Exception
{
BodyPart messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
_multipart.addBodyPart(messageBodyPart);
}
public class ByteArrayDataSource implements DataSource
{
private byte[] data;
private String type;
public ByteArrayDataSource(byte[] data, String type)
{
super();
this.data = data;
this.type = type;
}
public ByteArrayDataSource(byte[] data)
{
super();
this.data = data;
}
public void setType(String type)
{
this.type = type;
}
public String getContentType()
{
if (type == null)
return "application/octet-stream";
else
return type;
}
public InputStream getInputStream() throws IOException
{
return new ByteArrayInputStream(data);
}
public String getName()
{
return "ByteArrayDataSource";
}
public OutputStream getOutputStream() throws IOException
{
throw new IOException("Not Supported");
}
}
}
答案 0 :(得分:0)
07-05 02:56:35.539 2189-2480 / queens.feedback W / System.err:javax.mail.AuthenticationFailedException
看起来您无法进行身份验证。如果你肯定不是这种情况,你会想要抓住这个错误,看看你是否可以从中获得更多细节。也许GMail不允许您使用基本身份验证,而是需要安全身份验证。