发送电子邮件 - 如何?
Sending emails - how to?
所以我设法让应用程序发送电子邮件,但是当用弹出内容填充对话框时,发送按钮不再起作用(当用户多次打开应用程序时,我尝试做一个反馈它显示他弹出一些问题)
我的邮件代码:在里面(PopUp.java):
public class PopUp extends AppCompatActivity implements View.OnClickListener{
private EditText editTextEmail;
private EditText editTextSubject;
private EditText editTextMessage;
private Button buttonSend;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pop_up);
editTextEmail = (EditText) findViewById(R.id.editTextEmail);
editTextSubject = (EditText) findViewById(R.id.editTextSubject);
editTextMessage = (EditText) findViewById(R.id.editTextMessage);
buttonSend = (Button) findViewById(R.id.buttonSend);
//this is the button for sending the email
buttonSend.setOnClickListener(this);
}
private void sendEmail() {
String email = editTextEmail.getText().toString().trim();
String subject = editTextSubject.getText().toString().trim();
String message = editTextMessage.getText().toString().trim();
SendMail sm = new SendMail(this, email, subject, message);
sm.execute();
}
@Override
public void onClick(View v) {
sendEmail();
}
}
这是我的 FeedBack.java(我在其中为对话框设置充气器并计算应用程序被打开的次数),这就是问题所在(对话框显示每个 button/edit 文本) 但是当我按下按钮时它不发送邮件 :
private SharedPreferences prefs;
private SharedPreferences.Editor editor;
private int totalCount;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_feed_back);
prefs = getPreferences(Context.MODE_PRIVATE);
editor = prefs.edit();
totalCount = prefs.getInt("counter", 0);
totalCount++;
editor.putInt("counter", totalCount);
editor.commit();
if (totalCount == 2)
{
dialog();
}
}
private void dialog() {
final View viewPop = LayoutInflater.from(FeedBack.this).inflate(R.layout.activity_pop_up , null);
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(this);
alertBuilder.setMessage("Feedback!")
.setView(viewPop)
.setPositiveButton("Send!", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//i've tryed to call the function here but i dropped the idea because i didn't know how to call an method from another java file :(
}
})
.setNegativeButton("Cancel!", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(getApplicationContext(), "Canceled!", Toast.LENGTH_LONG).show();
}
}).show();
}
这是发送电子邮件的代码:
public class SendMail extends AsyncTask<Void,Void,Void> {
//Declaring Variables
private Context context;
private Session session;
//Information to send email
private String email;
private String subject;
private String message;
private String nume;
//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.nume = nume;
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;
}
现在可以使用了,我在 .setPosstiveButton
中进行了调用,在我这样做的方法中:
private void sendMail() {
//Getting content for email
String email = editTextEmailValue.getText().toString().trim();
String subject = editTextSubjectValue.getText().toString().trim();
String message = editTextMessageValue.getText().toString().trim();
//Creating SendMail object
SendMail sm = new SendMail(this, email, subject, message);
//Executing sendmail to send email
sm.execute();
}
我添加了这个:
final View viewPop = LayoutInflater.from(FeedBack.this).inflate(R.layout.activity_pop_up, null);
editTextEmailValue = (EditText) viewPop.findViewById(R.id.editTextEmail);
editTextSubjectValue = (EditText) viewPop.findViewById(R.id.editTextSubject);
editTextMessageValue = (EditText) viewPop.findViewById(R.id.editTextMessage);
在对话框方法中
并声明:
private EditText editTextEmailValue;
private EditText editTextSubjectValue;
private EditText editTextMessageValue;
所以我设法让应用程序发送电子邮件,但是当用弹出内容填充对话框时,发送按钮不再起作用(当用户多次打开应用程序时,我尝试做一个反馈它显示他弹出一些问题)
我的邮件代码:在里面(PopUp.java):
public class PopUp extends AppCompatActivity implements View.OnClickListener{
private EditText editTextEmail;
private EditText editTextSubject;
private EditText editTextMessage;
private Button buttonSend;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pop_up);
editTextEmail = (EditText) findViewById(R.id.editTextEmail);
editTextSubject = (EditText) findViewById(R.id.editTextSubject);
editTextMessage = (EditText) findViewById(R.id.editTextMessage);
buttonSend = (Button) findViewById(R.id.buttonSend);
//this is the button for sending the email
buttonSend.setOnClickListener(this);
}
private void sendEmail() {
String email = editTextEmail.getText().toString().trim();
String subject = editTextSubject.getText().toString().trim();
String message = editTextMessage.getText().toString().trim();
SendMail sm = new SendMail(this, email, subject, message);
sm.execute();
}
@Override
public void onClick(View v) {
sendEmail();
}
}
这是我的 FeedBack.java(我在其中为对话框设置充气器并计算应用程序被打开的次数),这就是问题所在(对话框显示每个 button/edit 文本) 但是当我按下按钮时它不发送邮件 :
private SharedPreferences prefs;
private SharedPreferences.Editor editor;
private int totalCount;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_feed_back);
prefs = getPreferences(Context.MODE_PRIVATE);
editor = prefs.edit();
totalCount = prefs.getInt("counter", 0);
totalCount++;
editor.putInt("counter", totalCount);
editor.commit();
if (totalCount == 2)
{
dialog();
}
}
private void dialog() {
final View viewPop = LayoutInflater.from(FeedBack.this).inflate(R.layout.activity_pop_up , null);
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(this);
alertBuilder.setMessage("Feedback!")
.setView(viewPop)
.setPositiveButton("Send!", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//i've tryed to call the function here but i dropped the idea because i didn't know how to call an method from another java file :(
}
})
.setNegativeButton("Cancel!", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(getApplicationContext(), "Canceled!", Toast.LENGTH_LONG).show();
}
}).show();
}
这是发送电子邮件的代码:
public class SendMail extends AsyncTask<Void,Void,Void> {
//Declaring Variables
private Context context;
private Session session;
//Information to send email
private String email;
private String subject;
private String message;
private String nume;
//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.nume = nume;
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;
}
现在可以使用了,我在 .setPosstiveButton
中进行了调用,在我这样做的方法中:
private void sendMail() {
//Getting content for email
String email = editTextEmailValue.getText().toString().trim();
String subject = editTextSubjectValue.getText().toString().trim();
String message = editTextMessageValue.getText().toString().trim();
//Creating SendMail object
SendMail sm = new SendMail(this, email, subject, message);
//Executing sendmail to send email
sm.execute();
}
我添加了这个:
final View viewPop = LayoutInflater.from(FeedBack.this).inflate(R.layout.activity_pop_up, null);
editTextEmailValue = (EditText) viewPop.findViewById(R.id.editTextEmail);
editTextSubjectValue = (EditText) viewPop.findViewById(R.id.editTextSubject);
editTextMessageValue = (EditText) viewPop.findViewById(R.id.editTextMessage);
在对话框方法中
并声明:
private EditText editTextEmailValue;
private EditText editTextSubjectValue;
private EditText editTextMessageValue;