Guest User

Untitled

a guest
May 26th, 2016
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.04 KB | None | 0 0
  1. Context mainContext;
  2.  
  3. String title;
  4. String text;
  5. String text1;
  6. String from;
  7. String where;
  8.  
  9. @Override
  10. public void onCreate(Bundle savedInstanceState) {
  11. super.onCreate(savedInstanceState);
  12. setContentView(R.layout.activity);
  13.  
  14. mainContext = this;
  15.  
  16. Button ibut = (Button) findViewById(R.id.button1);
  17. ibut.setOnClickListener(new OnClickListener() {
  18.  
  19. @Override
  20. public void onClick(View v) {
  21. sender_mail_async async_sending = new sender_mail_async();
  22. async_sending.execute();
  23. }
  24. });
  25. }
  26.  
  27. private class sender_mail_async extends AsyncTask<Object, String, Boolean> {
  28. ProgressDialog WaitingDialog;
  29.  
  30. @Override
  31. protected void onPreExecute() {
  32. WaitingDialog = ProgressDialog.show(ExtendedMail.this, "Отправляем письмо...", true);
  33. }
  34.  
  35. @Override
  36. protected void onPostExecute(Boolean result) {
  37. WaitingDialog.dismiss();
  38. Toast.makeText(mainContext, "Отправка завершена!!!", Toast.LENGTH_LONG).show();
  39. ((Activity)mainContext).finish();
  40. }
  41.  
  42. @Override
  43. protected Boolean doInBackground(Object... params) {
  44.  
  45. try {
  46. title = ((EditText)findViewById(R.id.editText1)).getText().toString();
  47. text = ((EditText)findViewById(R.id.editText2)).getText().toString();
  48. text1 = ((EditText)findViewById(R.id.editText3)).getText().toString();
  49.  
  50. where = "[email protected]";
  51.  
  52. MailSenderClass sender = new MailSenderClass("[email protected]", "******");
  53.  
  54. sender.sendMail(title, text, text1, from, where);
  55. } catch (Exception e) {
  56. Toast.makeText(mainContext, "Ошибка отправки сообщения!", Toast.LENGTH_SHORT).show();
  57. }
  58.  
  59. return false;
  60. }
  61. }
  62.  
  63. private String mailhost = "smtp.yandex.ru";
  64. private String user;
  65. private String password;
  66. private Session session;
  67.  
  68. private Multipart _multipart;
  69.  
  70. static {
  71. Security.addProvider(new com.exampl1.projectisreal.JSSEProvider());
  72. }
  73.  
  74. public MailSenderClass(String user, String password) {
  75. this.user = user;
  76. this.password = password;
  77.  
  78. _multipart = new MimeMultipart();
  79.  
  80. Properties props = new Properties();
  81. props.setProperty("mail.transport.protocol", "smtp");
  82. props.setProperty("mail.host", mailhost);
  83. props.put("mail.smtp.auth", "true");
  84. props.put("mail.smtp.port", "465");
  85. props.put("mail.smtp.socketFactory.port", "465");
  86. props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
  87. props.put("mail.smtp.socketFactory.fallback", "false");
  88. props.setProperty("mail.smtp.quitwait", "false");
  89.  
  90. session = Session.getDefaultInstance(props, this);
  91. }
  92.  
  93. protected PasswordAuthentication getPasswordAuthentication() {
  94. return new PasswordAuthentication(user, password);
  95. }
  96.  
  97. public synchronized void sendMail(String subject, String body, String sender, String recipients, String filename) throws Exception {
  98. try {
  99. MimeMessage message = new MimeMessage(session);
  100.  
  101.  
  102. message.setSender(new InternetAddress(sender));
  103.  
  104. message.setSubject(subject);
  105.  
  106. if (recipients.indexOf(',') > 0)
  107. message.setRecipients(Message.RecipientType.TO,
  108. InternetAddress.parse(recipients));
  109. else
  110. message.setRecipient(Message.RecipientType.TO,
  111. new InternetAddress(recipients));
  112.  
  113.  
  114. BodyPart messageBodyPart = new MimeBodyPart();
  115. messageBodyPart.setText(body);
  116. _multipart.addBodyPart(messageBodyPart);
  117.  
  118.  
  119. if (!filename.equalsIgnoreCase("")) {
  120. BodyPart attachBodyPart = new MimeBodyPart();
  121. DataSource source = new FileDataSource(filename);
  122. attachBodyPart.setDataHandler(new DataHandler(source));
  123. attachBodyPart.setFileName(filename);
  124.  
  125. _multipart.addBodyPart(attachBodyPart);
  126. }
  127.  
  128. message.setContent(_multipart);
  129.  
  130. Transport.send(message);
  131. } catch (Exception e) {
  132. Log.e("sendMail","Ошибка отправки! ");
  133. }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment