Hej, kiedyś udało mi się napisać taki kod (jeszcze w eclipse) który wysyła za pomocą JavaMail maila z załącznikiem ale teraz ten kod nie działa ?
Zabrałem się za to na nowo i napisałem coś takiego:
public class MainActivity extends AppCompatActivity {
private EditText etMail;
private EditText etTemat;
private EditText etTresc;
private Button btGaleria;
private Button btWyslij;
private ImageView ivZdjecie;
private ProgressBar progressBar;
private String mMail;
private String mHaslo;
private Uri mUri;
private static final int WYBRANE_ZDJECIE = 123;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etMail = (EditText) findViewById(R.id.et_mail);
etTemat = (EditText) findViewById(R.id.et_temat);
etTresc = (EditText) findViewById(R.id.et_tresc);
btGaleria = (Button) findViewById(R.id.b_galeria);
btWyslij = (Button) findViewById(R.id.b_wyslij);
ivZdjecie = (ImageView) findViewById(R.id.iv_zdjecie);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
mMail = "xxxxxxx@gmail.com";
mHaslo = "yyyyyy";
btWyslij.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Properties properties = new Properties();
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.host", "smtp.gmail.com");
properties.put("mail.smtp.port", "587");
Session session = Session.getInstance(properties, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(mMail, mHaslo);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(mMail));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(etMail.getText().toString().trim()));
message.setSubject(etTemat.getText().toString().trim());
message.setText(etTresc.getText().toString().trim());
MimeBodyPart messageBodyPart = new MimeBodyPart();
Multipart multipart = new MimeMultipart();
messageBodyPart = new MimeBodyPart();
String file = mUri.toString();
String filename = "załącznik";
DataSource source = new FileDataSource(file);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
new SendMail().execute(message);
} catch (MessagingException e) {
e.printStackTrace();
}
}
});
btGaleria.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, WYBRANE_ZDJECIE);
}
});
}
private class SendMail extends AsyncTask<Message, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
progressBar.setVisibility(View.VISIBLE);
}
@Override
protected String doInBackground(Message... messages) {
try {
Transport.send(messages[0]);
return "Success";
} catch (MessagingException e) {
e.printStackTrace();
return "Error";
}
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
progressBar.setVisibility(View.GONE);
if (s.equals("Success")) {
Toast.makeText(MainActivity.this, "Wysłało!!!", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(MainActivity.this, "Nie wysłało", Toast.LENGTH_LONG).show();
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case WYBRANE_ZDJECIE:
if (resultCode == RESULT_OK) {
mUri = data.getData();
RequestOptions option = new RequestOptions().circleCrop();
Glide.with(this).load(mUri).apply(option).into(ivZdjecie);
}
}
}
}
Jeśli usunę tą część kodu:
MimeBodyPart messageBodyPart = new MimeBodyPart();
Multipart multipart = new MimeMultipart();
messageBodyPart = new MimeBodyPart();
String file = mUri.toString();
String filename = "załącznik";
DataSource source = new FileDataSource(file);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
to wysyła maila bez problemu, oczywiście bez załącznika - zdjęcia.
Siedzę nad tym już chyba z tydzień i nie daje rady ?
Pomocy!!! 😉