简体   繁体   English

发送电子邮件-如何?

[英]Sending emails - how to?

So I managed to make the app to send an email but when inflating the dialog with the popup content the send button doesn't work anymore (i try to do a feedback when the user opens the app several times it shows him the popup whit some questions) 因此,我设法使该应用程序发送电子邮件,但是当使用弹出内容填充对话框时,“发送”按钮不再起作用(我尝试在用户多次打开该应用程序以向其显示弹出窗口时给出一些反馈,问题)

My mail code: it is inside (PopUp.java): 我的邮件代码:位于(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();
}
}

this is my FeedBack.java(where I do the inflator for the dialog and count how many times the app was opened) and here is the problem (the dialog shows up whit every button/edit text ) but when I press the button it doesn't send the mail : 这是我的FeedBack.java(我在其中对对话框进行充气,并计算打开应用的次数),这就是问题所在(对话框显示每个按钮/编辑文本),但是当我按下按钮时,它没有不发送邮件:

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();
}

And this is the code for sending the email : 这是发送电子邮件的代码:

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;
    }

Now it works i put a call in .setPosstiveButton and in the method i did like this : 现在它可以工作了,我在.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();
}

and i've added this : 并且我添加了这个:

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);

in the dialog method 在对话框中

and declared : 并声明:

private EditText editTextEmailValue;
private EditText editTextSubjectValue;
private EditText editTextMessageValue;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM