Note that there are some explanatory texts on larger screens.

plurals
  1. POJavaMail, no exception is thrown if the receivers mail are wrong
    text
    copied!<p>This codesnippet, sends a mail:</p> <pre><code>if (m.send()) { Log.i("MAIL SENDER: ", "Succesfully"); Toast.makeText(getApplicationContext(), "The log file has been sent", Toast.LENGTH_LONG).show(); } else { throw new MailException(); } </code></pre> <p>If the receivers mail isn't correct, e.g <code>example@gmail.co</code> instead of <code>example@gmail.com</code> the code runs into the first if-block. Why does this happend when the receivers isn't correct. Could anyone help me out here? </p> <p>The mail settings is retrieved from SharedPreferences, and checked in this method:</p> <pre><code>public static boolean isValidEmailAddress(String email) { boolean result = true; try { InternetAddress emailAddr = new InternetAddress(email); emailAddr.validate(); } catch (AddressException ex) { result = false; } return result; } </code></pre> <p>When the user types in a email address in the textfield and hit Ok this is whats happening:</p> <pre><code>if (isValidEmailAddress(mail)) editor.putString("Tomail", mail); else Toast.makeText(getApplicationContext(), "Invalid", Toast.LENGHT_LONG).show(); </code></pre> <p>This will only check if the mail contains a <code>@</code> and therefor its valid. </p> <p>In my send method:</p> <pre><code>public SendMail(Context c) { this(); sharedPrefs = PreferenceManager.getDefaultSharedPreferences(c); _host = sharedPrefs.getString("host", null); _port = sharedPrefs.getString("port", null); _sport = sharedPrefs.getString("port", null); _user = sharedPrefs.getString("mail", null); _pass = sharedPrefs.getString("pw", null); } public boolean send() throws Exception { Properties props = _setProperties(); if (!_user.equals("") &amp;&amp; !_pass.equals("") &amp;&amp; _to.length &gt; 0 &amp;&amp; !_from.equals("") &amp;&amp; !_subject.equals("") &amp;&amp; !_body.equals("")) { Session session = Session.getInstance(props, new GMailAuthenticator(_user, _pass)); MimeMessage msg = new MimeMessage(session); msg.setFrom(new InternetAddress(_from)); InternetAddress[] addressTo = new InternetAddress[_to.length]; for (int i = 0; i &lt; _to.length; i++) { addressTo[i] = new InternetAddress(_to[i]); } msg.setRecipients(MimeMessage.RecipientType.TO, addressTo); msg.setSubject(_subject); msg.setSentDate(new Date()); // setup message body BodyPart messageBodyPart = new MimeBodyPart(); messageBodyPart.setText(_body); _multipart.addBodyPart(messageBodyPart); // Put parts in message msg.setContent(_multipart); // send email Transport.send(msg); return true; } else { return false; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload