Hello. I have a snippet and a button. How do I correctly call the sendMail() method when I click a button? And why the Mail instance creation is highlighted (Unreachable statment)?
Here is the code:
public class TabFragment1 extends Fragment { private Mail m ; private ImageButton imgSend; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.tab_fragment_1, container, false); m = new Mail("********@gmail.com", "*******"); } public void sendEmail(View view){ String[] toArr = {"toemail1@domain1.tld"}; // This is an array, you can add more emails, just separate them with a coma m.setTo(toArr); // load array to setTo function m.setFrom("fromEmail@domain.tld"); // who is sending the email m.setSubject("subject"); m.setBody("your message goes here"); try { m.addAttachment("/sdcard/myPicture.jpg"); // path to file you want to attach if(m.send()) { // success Toast.makeText(getActivity(), "Email was sent successfully.", Toast.LENGTH_LONG).show(); } else { // failure Toast.makeText(getActivity() ,"Email was not sent.", Toast.LENGTH_LONG).show(); } } catch(Exception e) { // some other problem Toast.makeText(getActivity(), "There was a problem sending the email.", Toast.LENGTH_LONG).show(); } }
returnwill never be executed. So put everything afterreturnbefore it. - Yuriy SPb ♦