Email sending example
As promised, here’s a simple example on how to use the SMTP library. If the username and the password are replaced with actually existing user credentials then this code snippet can send mails via Gmail.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | public void send(String from, String to, String subject, String text) throws IOException { client = new SMTPClient("UTF-8"); client.setDefaultTimeout(60 * 1000); client.setRequireStartTLS(true); // requires STARTTLS //client.setUseStartTLS(true); // tries STARTTLS, but falls back if not supported client.setUseAuth(true); // use SMTP AUTH //client.setAuthMechanisms(authMechanisms); // sets AUTH mechanisms e.g. LOGIN client.connect("smtp.gmail.com", 587); checkReply(client); client.login("localhost", "user@gmail.com", "passwd"); checkReply(client); client.setSender(from); checkReply(client); client.addRecipient(to); checkReply(client); Writer writer = client.sendMessageData(); if (writer != null) { SimpleSMTPHeader header = new SimpleSMTPHeader(from, to, subject); writer.write(header.toString()); writer.write(text); writer.close(); client.completePendingCommand(); checkReply(client); } client.logout(); client.disconnect(); } private void checkReply(SMTPClient sc) throws IOException { if (SMTPReply.isNegativeTransient(sc.getReplyCode())) { sc.disconnect(); throw new IOException("Transient SMTP error " + sc.getReplyCode()); } else if (SMTPReply.isNegativePermanent(sc.getReplyCode())) { sc.disconnect(); throw new IOException("Permanent SMTP error " + sc.getReplyCode()); } } |

Hi,
Found your sample code here and tried it on sdk 1.5.
I don’t have any errors, but am getting: “Conversion to Dalvik format failed with error 1″ despite trying Project > Clean and deleting R.java in eclipse, so no apk is generated.
Any clues why?
Thanks!