Hej, kiedyś pytałem o wysyłanie maila z poziomu aplikacji, rozwiązaniem okazał się JavaMail. Moja apka wysyła maile, wszystko działa ale nie mam bladego pojęcia jak dołączyć jakiś załącznik (dokładnie zdjęcie) do maila.
Albo już nie mój poziom albo mam jakaś blokadę, chciałbym skończyć apkę do końca tygodnia dlatego muszę prosić Was o pomoc 🙂
Apka to 3 edit texty, dwa buttony jeden text view i jeden image view
do edit textów wpisujemy adres na który ma być wysłany mail, temat i treść maila, jeden buton otwiera galerię z której wybieramy zdjęcie. Zdjęcie wyświetla się w imageView a w text view wypisuje się ścieżka do zdjęcia.
Drugi button wysyła maila.
Oto kod:
package com.example.mail;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.MediaStore;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
Session session = null;
Session session2 = null;
ProgressDialog pdialog = null;
Context context = null;
private EditText mail;
private EditText temat;
private EditText tresc;
String adres, topic, wiad;
Drawable d;
ImageView obrazek;
private static final int SELECTED_PICTURE=1;
TextView obrazekSciezka;
Uri uri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bindViews();
}
private void bindViews() {
// TODO Auto-generated method stub
mail = (EditText) findViewById(R.id.editText1);
temat = (EditText) findViewById(R.id.editText2);
tresc = (EditText) findViewById(R.id.editText3);
obrazekSciezka = (TextView) findViewById(R.id.textView1);
findViewById(R.id.button1).setOnClickListener(this);
findViewById(R.id.button2).setOnClickListener(this);
obrazek = (ImageView) findViewById(R.id.imageView1);
context = this;
}
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
if (view.getId() == R.id.button1){
wyslij();
return;
}
if (view.getId() == R.id.button2){
dodaj(view);
return;
}
}
public void dodaj(View view) {
// TODO Auto-generated method stub
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, SELECTED_PICTURE);
}
public void wyslij() {
// TODO Auto-generated method stub
adres = mail.getText().toString();
topic = temat.getText().toString();
wiad = tresc.getText().toString();
Properties props = new Properties();
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");
session = Session.getDefaultInstance(props, new Authenticator(){
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("adresMailaZktoregoWysylamyWiadomosc@gmail.com", "hasłoDoMaila");
}
});
pdialog = ProgressDialog.show(context, "", "Wysyłam...", true);
RetreiveFeedTask task = new RetreiveFeedTask();
task.execute();
}
class RetreiveFeedTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
try{
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("adresMailaZktoregoWysylamyWiadomosc@gmail.com"));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(adres));
message.setSubject(topic);
message.setContent(wiad, "text/html; charset=utf-8");
Transport.send(message);
} catch(MessagingException e) {
e.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
pdialog.dismiss();
mail.setText("");
temat.setText("");
tresc.setText("");
Toast.makeText(getApplicationContext(), "wiadomosc wyslana", Toast.LENGTH_LONG).show();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case SELECTED_PICTURE:
if(resultCode==RESULT_OK){
uri = data.getData();
String[]projection={MediaStore.Images.Media.DATA};
Cursor cursor=getContentResolver().query(uri, projection, null, null, null);
cursor.moveToFirst();
int columnIndex=cursor.getColumnIndex(projection[0]);
String filename=cursor.getString(columnIndex);
cursor.close();
Bitmap yourSelectedImage=BitmapFactory.decodeFile(filename);
d=new BitmapDrawable(yourSelectedImage);
obrazek.setBackground(d);
uri = Uri.parse(filename);
obrazekSciezka.setText(filename);
}
break;
default:
break;
}
}
}
Będę bardzo wdzięczny za pomoc.
PS prosze o jasne wskazówki bo jestem dopiero uczącym się programistą 😉