我正在制作一个应用程序,当用户在不打开gmail的情况下按“提交”按钮但该应用程序在图片中的屏幕刚崩溃时会向我的群组发送电子邮件:-
我在这里声明按下按钮时会发生什么:-
Button submit=(Button) findViewById(R.id.rSubmit);
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String email,subject,message;
email="the_email_id_of_receiver";
subject="Test";
message="This is a test";
SendMail sm = new SendMail(RequestBlood.this, email, subject, message);
sm.execute();
}
});
The是处理发送邮件任务的类:-
package org.bloodconnect.bloodconnect;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.widget.Toast;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
//Class is extending AsyncTask because this class is going to perform a networking operation
public class SendMail extends AsyncTask<Void,Void,Void> {
private Context context;
private Session session;
//Information to send email
private String email;
private String subject;
private String message;
//Progressdialog to show while sending email
private ProgressDialog progressDialog;
//Class Constructor
public SendMail(Context context, String email, String subject, String message){
//Initializing variables
this.context = context;
this.email = email;
this.subject = subject;
this.message = message;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
//Showing progress dialog while sending email
progressDialog = ProgressDialog.show(context,"Sending message","Please wait...",false,false);
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
//Dismissing the progress dialog
progressDialog.dismiss();
//Showing a success message
Toast.makeText(context,"Message Sent",Toast.LENGTH_LONG).show();
}
@Override
protected Void doInBackground(Void... params) {
//Creating properties
Properties props = new Properties();
//Configuring properties for gmail
//If you are not using gmail you may need to change the values
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
//Creating a new session
session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
//Authenticating the password
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(Config.EMAIL, Config.PASSWORD);
}
});
try {
//Creating MimeMessage object
MimeMessage mm = new MimeMessage(session);
//Setting sender address
mm.setFrom(new InternetAddress(Config.EMAIL));
//Adding receiver
mm.addRecipient(Message.RecipientType.TO, new InternetAddress(email));
//Adding subject
mm.setSubject(subject);
//Adding message
mm.setText(message);
//Sending email
Transport.send(mm);
} catch (MessagingException e) {
e.printStackTrace();
}
return null;
}
}
这是我声明ID和密码的类:-
package org.bloodconnect.bloodconnect;
public class Config {
public static final String EMAIL ="my_email_id";
public static final String PASSWORD ="password";
}
从图片中可以看出,在SendMail类中统一onPreExecute方法,一切运行良好(我希望如此)。我对android很陌生,所以请帮助我解决此错误。 在此先感谢:)
Logcat在第二张照片中。
答案 0 :(得分:0)
使用官方JavaMail for Android版本,而不使用其他答案中引用的旧分叉版本。
答案 1 :(得分:-1)
有一个Android友好版本的Java Mail。这避免了对Java AWT的间接依赖,而Java AWT并未包含在Android运行时中。
更多详细信息here。