【问题标题】:Send Email Using a dialog box使用对话框发送电子邮件
【发布时间】:2018-11-07 00:45:40
【问题描述】:

我正在尝试发送一封电子邮件,用户单击一个按钮并弹出一个对话框以输入他的电子邮件,它应该向他发送一封电子邮件,但是我收到一个错误: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;
}

【问题讨论】:

    标签: android dialog email


    【解决方案1】:

    你不能有这样的嵌套函数。

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

    【讨论】:

    • 我在尝试这个时收到以下错误,似乎在打开窗口输入电子邮件之前尝试发送电子邮件Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
    • 确保您的 xml 布局中有一个 ID 为 btnEmail 的按钮。也许你应该看看一些关于 Android 的初学者教程?
    猜你喜欢
    • 2013-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-14
    • 2015-12-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多