• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

android application has closed unexpecteadly forse closed

Status
Not open for further replies.

nishu3389

Lurker
i am trying to send an email by android app using java mail API programmatically without using default mail app, it compiles successfully but when i install it a forse close popup says that an unexpected exception occurred. i also write internet permission in manifest file and also added required external jars,

logcat error status is following:-

06-06 23:09:45.537: ERROR/AndroidRuntime(607): java.lang.RuntimeException: Unable to start activity ComponentInfo{sample.app/sample.app.first}: java.lang.NullPointerException

Actual code is as following:-

public class main extends Activity implements OnClickListener {

Button btn;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn.findViewById(R.id.button1);
btn.setOnClickListener(this);
}
public void sendEmail() throws AddressException, MessagingException
{
String host = "smtp.gmail.com";
String address = "vsvishalsharma16@gmail.com";
String from = "nishu3389@gmail.com";
String pass = "nishuuuu";
String to="vsvishalsharma16@gmail.com";
Multipart multiPart;
String finalString="";
Properties props = System.getProperties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", address);
props.put("mail.smtp.password", pass);
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
Log.i("Check", "done pops");
Session session = Session.getDefaultInstance(props, null);
DataHandler handler=new DataHandler(new ByteArrayDataSource(finalString.getBytes(),"text/plain" ));
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setDataHandler(handler);
Log.i("Check", "done sessions");
multiPart=new MimeMultipart();
InternetAddress toAddress;
toAddress = new InternetAddress(to);
message.addRecipient(Message.RecipientType.TO, toAddress);
Log.i("Check", "added recipient");
message.setSubject("Send Auto-Mail");
message.setContent(multiPart);
message.setText("Demo For Sending Mail in Android Automatically");
Log.i("check", "transport");
Transport transport = session.getTransport("smtp");
Log.i("check", "connecting");
transport.connect(host,address , pass);
Log.i("check", "wana send");
transport.sendMessage(message, message.getAllRecipients());
transport.close();
Log.i("check", "sent");
}
public void onClick(View v) {

try {
sendEmail();
} catch (Exception e) {

e.printStackTrace();
}
}
}


photo.php
 
Status
Not open for further replies.
Back
Top Bottom