Javamail不起作用,但是我找不到问题。 (验证失败)

时间:2019-05-18 14:25:28

标签: java android javamail

我尝试通过电子邮件向我发送字符串。我使用javamail。但是,当我在我的应用程序中单击发送按钮时,它将告诉该应用程序已发送,但我不会收到任何邮件。 由于外部应用程序的权限,Google首次发送警报。更改权限后,似乎什么也没有发生。我仍然没有收到任何邮件。

在调试中,似乎认证失败。

我有3个主菜单,用于更改发件人的配置和用于实际语法的SendMail。 我真的很感谢您的帮助,或者有人可以说出更好的方法来帮助您。

以下是调试:

V / ViewRootImpl:目录绘制完成:正在发送消息

W / SplitWindow:更新焦点...

E /编辑器:hideClipTrayIfNeeded()TextView被聚焦!! hideClipTray()

W / System.err:javax.mail.AuthenticationFailedException

W / System.err:位于javax.mail.Service.connect(Service.java:319)

    at javax.mail.Service.connect(Service.java:169)

    at javax.mail.Service.connect(Service.java:118)

    at javax.mail.Transport.send0(Transport.java:188)

    at javax.mail.Transport.send(Transport.java:118)

    at com.example.m2.SendMail.doInBackground(SendMail.java:96)

    at com.example.m2.SendMail.doInBackground(SendMail.java:21)

    at android.os.AsyncTask$2.call(AsyncTask.java:295)

    at java.util.concurrent.FutureTask.run(FutureTask.java:237)

    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)

    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)

    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)

    at java.lang.Thread.run(Thread.java:818)

W / SplitWindow:更新焦点...

主要活动:

公共类MainActivity扩展了AppCompatActivity实现的View.OnClickListener {

private Button exitBtn;
private Button sendButton;
private EditText wText;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_main);

    exitBtn = (Button)findViewById(R.id.exitButton);
    exitBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            moveTaskToBack(true);
            android.os.Process.killProcess(Process.myPid());
            System.exit(1);
        }
    });

    sendButton = (Button)findViewById(R.id.sendButton);
    wText      = (EditText)findViewById(R.id.wInput);

    sendButton.setOnClickListener(this);
}

private void sendEmail() {
    //Getting content for email
    String email = "(mytargetmail)";
    String subject = wText.getText().toString().trim();
    String message = "Mamas neuer Wunsch";

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

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

}`enter code here`

@Override
public void onClick(View v) {
    sendEmail();
}

}

配置:

public class Config {
public static final String EMAIL ="my mail";
public static final String PASSWORD ="and password";

}

发送电子邮件:

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

}

0 个答案:

没有答案