Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static {
- Security.addProvider(new com.example.myapp.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("Ошибка отправки!");
- }
- }
- Context mainContext;
- String title;
- String text;
- String from;
- String where;
- String text1;
- String text2;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_feedback);
- mainContext = this;
- setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
- ((Button)findViewById(R.id.button1)).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();
- text2 = ((EditText)findViewById(R.id.editText4)).getText().toString();
- from = "mymaill@yandex.ru";
- where = "myappmaill@yandex.ru";
- MailSenderClass sender = new MailSenderClass("mymaill@yandex.ru", "password");
- sender.sendMail(title, text, text1, text2, from);
- } catch (Exception e) {
- Toast.makeText(mainContext, "Ошибка отправки сообщения!", Toast.LENGTH_SHORT).show();
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement