android studio mail gönderirken hata
Arkadaslar kodlarım aşağıda. mail göndermeye çalışıyorum. çalışırken tam göndereceği sırada durduruldu şeklinde runtime hatası alıyorum.
hatayı tam olarak verdiği yer mail.php de
messageBodyPart.setText(_body);
satırı
bu satıra gelince uygulama patlıyo.
kodlarım şöyle ;
mail.php
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
public class Mail extends javax.mail.Authenticator {
private String _user,_pass,_from,_sport,_host,_subject,_body,_port,json;
private String[] _to; private boolean _auth;
private boolean _debuggable;
private Multipart _multipart;
public Mail() {
_host = "mx1.hostinger.web.tr"; // varsayılan smtp sunucusu
_port = "587"; // varsayılan smtp port numarası
_sport = "587"; // varsayılan socketfactory port numarası
_user = ""; // kullanıcı adı
_pass = ""; // parola
_from = "info@temsilidir.xyz"; // e-posta gönderen kişinin adresi
_subject = ""; // email konusu
_body = ""; // email gövde kısmı
_debuggable = false; // debug modunu açmak için -varsayılan off
_auth = true; // smtp kimlik doğrulama - varsayılan on
_multipart = new MimeMultipart(); }
public Mail(String user, String pass) { this(); _user = user; _pass = pass; }
public void setTo(String[] too) { _to = too; } // get ve set fonksiyonları
public String getBody() { return _body; }
public void setBody(String _body) { this._body = _body; }
public String getSubject() { return _subject; }
public void setSubject(String _subject) { this._subject = _subject; }
public boolean send() throws Exception {
Properties props = _setProperties();
if (!_user.equals("") && !_pass.equals("") && _to.length > 0 && !_from.equals("") && !_subject.equals("") && !_body.equals("")) {
Session session = Session.getInstance(props, this);
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(_from));
InternetAddress[] addressTo = new InternetAddress[_to.length];
for (int i = 0; i < _to.length; i++)
{ addressTo[i] = new InternetAddress(_to[i]); }
msg.setRecipients(MimeMessage.RecipientType.TO, addressTo);
msg.setSubject(_subject);
msg.setSentDate(new Date());
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(_body);
_multipart.addBodyPart(messageBodyPart);
msg.setContent(_multipart);
Transport.send(msg);
return true; }
else { return false; } }
@Override
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(_user, _pass); }
// _setProperties fonksiyonuna bakacak olursak:
private Properties _setProperties() {
Properties props = new Properties(); props.put("mail.smtp.host", _host);
if (_debuggable) { props.put("mail.debug", "true"); }
if (_auth) { props.put("mail.smtp.auth", "true"); }
props.put("mail.smtp.port", _port);
props.put("mail.smtp.socketFactory.port", _sport); props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory"); props.put("mail.smtp.socketFactory.fallback", "false");
return props; } }
maili gönderen kodum
public class maildenemejava extends Fragment {
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub
View view = inflater.inflate(R.layout.maildenemelayout, container,false);
Button gonder = (Button) view.findViewById(R.id.button9);
gonder.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { buttonClicked(v); } });
return view; }
public void buttonClicked (View view)
{
Mail m = new Mail("info@temsilidir.xyz", "sifre");
String[] toArr = { "temsili@temsili.com" };
m.setTo(toArr);
m.setSubject("konu");
m.setBody("mesaj"); ;
try { if (m.send()) {
Toast.makeText(getActivity(), "gönderili", Toast.LENGTH_LONG).show(); }
else { Toast.makeText(getActivity(), "olmadı", Toast.LENGTH_LONG).show(); }
} catch (Exception e) { Log.e("MailApp", "Could not send email", e); } } }
logcatda bu şekilde
01-13 12:54:10.429 2441-2441/hop.com.meto E/AndroidRuntime: FATAL EXCEPTION: main
Process: hop.com.meto, PID: 2441
java.lang.NoClassDefFoundError: javax.activation.DataHandler
at javax.mail.internet.MimeBodyPart.setContent(MimeBodyPart.java:740)
at javax.mail.internet.MimeBodyPart.setText(MimeBodyPart.java:1219)
at javax.mail.internet.MimeBodyPart.setText(MimeBodyPart.java:782)
at javax.mail.internet.MimeBodyPart.setText(MimeBodyPart.java:765)
at hop.com.meto.Mail.send(Mail.java:102)
at hop.com.meto.maildenemejava.buttonClicked(maildenemejava.java:97)
at hop.com.meto.maildenemejava$1.onClick(maildenemejava.java:64)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)