7.5. Класс LoginActivity
public class LoginActivity extends AppCompatActivity { EditText edLogin,edPass,edSmtpServer,edMailFrom; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); edLogin=(EditText)findViewById(R.id.editTextLogin); edPass=(EditText)findViewById(R.id.editTextPass); edSmtpServer=(EditText)findViewById(R.id.editTextSmtpServer); edMailFrom=(EditText)findViewById(R.id.editTextFromEmail); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String login=edLogin.getText().toString(); String pass=edPass.getText().toString(); String smtp=edSmtpServer.getText().toString(); String mailFrom=edMailFrom.getText().toString(); if(!login.isEmpty()&&!pass.isEmpty()&&!smtp.isEmpty()&&mailFrom.contains("@")) { DATA.username = login; DATA.password = pass; DATA.smtpServer = smtp; DATA.fromMail = mailFrom; boolean result = false; ConnectToServer con = new ConnectToServer(); con.execute(); try { result = con.get(); } catch (Exception e) { } if (result) { Toast.makeText(LoginActivity.this, "Успешное соединение!", Toast.LENGTH_SHORT).show(); Intent intent = new Intent(LoginActivity.this, MainActivity.class); startActivity(intent); } else Snackbar.make(view, "Ошибка подключения", Snackbar.LENGTH_LONG).show(); } else { Snackbar.make(view, "Заполните все поля!", Snackbar.LENGTH_LONG).show(); } } }); } private class ConnectToServer extends AsyncTask<Void, Void, Boolean>{ @Override protected Boolean doInBackground(Void... voids){ Properties props = new Properties(); props.put("mail.smtp.host", DATA.smtpServer); props.put("mail.smtp.socketFactory.port", "465"); props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); props.put("mail.smtp.auth", "false"); props.put("mail.smtp.port", "465"); Session session = Session.getDefaultInstance(props); Transport t = null; try { try { t = session.getTransport("smtp"); } catch (NoSuchProviderException e) { e.printStackTrace(); } t.connect(DATA.smtpServer, DATA.username,DATA.password); } catch (MessagingException e) { e.printStackTrace(); return false; } DATA.transport=t; DATA.session=session; return true; } } }
