Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Context mainContext;
- String title;
- String text;
- String text1;
- String from;
- String where;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity);
- mainContext = this;
- Button ibut = (Button) findViewById(R.id.button1);
- ibut.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- sender_mail_async async_sending = new sender_mail_async();
- async_sending.execute();
- }
- });
- }
- private class sender_mail_async extends AsyncTask<Object, String, Boolean> {
- ProgressDialog WaitingDialog;
- @Override
- protected void onPreExecute() {
- WaitingDialog = ProgressDialog.show(ExtendedMail.this, "Отправляем письмо...", true);
- }
- @Override
- protected void onPostExecute(Boolean result) {
- WaitingDialog.dismiss();
- Toast.makeText(mainContext, "Отправка завершена!!!", Toast.LENGTH_LONG).show();
- ((Activity)mainContext).finish();
- }
- @Override
- protected Boolean doInBackground(Object... params) {
- try {
- title = ((EditText)findViewById(R.id.editText1)).getText().toString();
- text = ((EditText)findViewById(R.id.editText2)).getText().toString();
- text1 = ((EditText)findViewById(R.id.editText3)).getText().toString();
- from = "mymail@yandex.ru";
- where = "myappmail@yandex.ru";
- MailSenderClass sender = new MailSenderClass("mymail@yandex.ru", "******");
- sender.sendMail(title, text, text1, from, where);
- } catch (Exception e) {
- Toast.makeText(mainContext, "Ошибка отправки сообщения!", Toast.LENGTH_SHORT).show();
- }
- return false;
- }
- }
- private String mailhost = "smtp.yandex.ru";
- private String user;
- private String password;
- private Session session;
- private Multipart _multipart;
- static {
- Security.addProvider(new com.exampl1.projectisreal.JSSEProvider());
- }
- public MailSenderClass(String user, String password) {
- this.user = user;
- this.password = password;
- _multipart = new MimeMultipart();
- Properties props = new Properties();
- props.setProperty("mail.transport.protocol", "smtp");
- props.setProperty("mail.host", mailhost);
- props.put("mail.smtp.auth", "true");
- props.put("mail.smtp.port", "465");
- props.put("mail.smtp.socketFactory.port", "465");
- props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
- props.put("mail.smtp.socketFactory.fallback", "false");
- props.setProperty("mail.smtp.quitwait", "false");
- session = Session.getDefaultInstance(props, this);
- }
- protected PasswordAuthentication getPasswordAuthentication() {
- return new PasswordAuthentication(user, password);
- }
- public synchronized void sendMail(String subject, String body, String sender, String recipients, String filename) throws Exception {
- try {
- MimeMessage message = new MimeMessage(session);
- message.setSender(new InternetAddress(sender));
- message.setSubject(subject);
- if (recipients.indexOf(',') > 0)
- message.setRecipients(Message.RecipientType.TO,
- InternetAddress.parse(recipients));
- else
- message.setRecipient(Message.RecipientType.TO,
- new InternetAddress(recipients));
- BodyPart messageBodyPart = new MimeBodyPart();
- messageBodyPart.setText(body);
- _multipart.addBodyPart(messageBodyPart);
- if (!filename.equalsIgnoreCase("")) {
- BodyPart attachBodyPart = new MimeBodyPart();
- DataSource source = new FileDataSource(filename);
- attachBodyPart.setDataHandler(new DataHandler(source));
- attachBodyPart.setFileName(filename);
- _multipart.addBodyPart(attachBodyPart);
- }
- message.setContent(_multipart);
- Transport.send(message);
- } catch (Exception e) {
- Log.e("sendMail","Ошибка отправки! ");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement