使用对话框发送电子邮件

时间:2018-11-06 16:05:18

标签: android dialog email-integration

我正在尝试发送一封电子邮件,其中用户单击按钮,然后弹出一个对话框以输入他的电子邮件,它应该向他发送一封电子邮件,但是我收到错误消息:Expression expected ';' never used 我想知道是否是因为对话框被声明为公共对话框,并且我试图将发送邮件用作私有邮件

这是我的对话代码:

    Button Emailbtn = (Button) dialog.findViewById(R.id.btnEmail);
            Emailbtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    v.setSelected(true);
                    final Dialog dialog = new Dialog(ViewQuotesD.this);
                    dialog.getWindow();
                    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                    dialog.setContentView(R.layout.dialemail);
                    dialog.show();

                  final EditText  emaill = (EditText) findViewById(R.id.edtEmail);
                    private void sendEmail() {
                        //Getting content for email
                        String email = emaill.getText().toString().trim();
                        String subject = "Your Login Credentials";
                        String message ="Hi "+SlectedName+"\n" +"Your Grand Prics is:" + " " + SelectedPrice + "\n" + "Your Installation cost:" + " " + SelectedInstall;

                        //Creating SendMail object
                        SendMail sm = new SendMail(ViewQuotesD.this, email, subject, message);

                        //Executing sendmail to send email
                        sm.execute();
                    }
                }
            });

这是我的发送邮件类:

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;

//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

}

@Override
protected void onPostExecute(Void aVoid) {
    super.onPostExecute(aVoid);
    //Dismissing the progress dialog
    //Showing a success message

}

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

1 个答案:

答案 0 :(得分:0)

您不能拥有这样的嵌套函数。

要么将sendEmail()用作类函数,要么仅将函数的代码放入这样:

Button Emailbtn = (Button) dialog.findViewById(R.id.btnEmail);
Emailbtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        v.setSelected(true);
        final Dialog dialog = new Dialog(ViewQuotesD.this);
        dialog.getWindow();
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.dialemail);
        dialog.show();

        final EditText  emaill = (EditText) findViewById(R.id.edtEmail);

        //Getting content for email
        String email = emaill.getText().toString().trim();
        String subject = "Your Login Credentials";
        String message ="Hi "+SlectedName+"\n" +"Your Grand Prics is:" + " " + SelectedPrice + "\n" + "Your Installation cost:" + " " + SelectedInstall;

        //Creating SendMail object
        SendMail sm = new SendMail(ViewQuotesD.this, email, subject, message);

        //Executing sendmail to send email
        sm.execute();
    }
});