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());
	}
}

Write a Comment

Take a moment to comment and tell us what you think. Some basic HTML is allowed for formatting.

Reader Comments

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!

Hi,

My bad; the problem was with the manifest.

Works great now!

I see you are using SMTPClient and SimpleSMTPHeader to send a simple message. Looking at the source, I do not see any support for attachments and that is something I need.

It is possible all I need to do is add the EmailAttachment and MultiPartEmail classes from the org.apache.commons.mail package.

Have you tried this?

Thanks!

Hi there,

you’re right, MIME and thus attachments/multipart messages are not supported (yet). The problem with org.apache.commons.mail is that it is built on top of Java Mail, so you need again the activation framework and AWT for it — making it basically a no-go for Android.

Nevertheless, I see a few possible solutions. One of them is to take and use the com.android.email.mail.internet package from the Android built-in Email application (you can get via git from git://android.git.kernel.org/platform/packages/apps/Email.git).

[...] Email Sending Example [...]

Hi, i’ve a little problem.
The SMTPClient class is in Apache SDK?

Richard, yep, but it is also available in the patched version of the library (see previous articles). Use the latter if you would like to send mails on Android.

Hi,Nilvec,
The SMTPClient class and SMTPReply class is in jdk ?

Thanks!
SMTPClient,SimpleSMTPHeader,SMTPReply belong to
org.apache.commons.net.smtp

Thanks a lot for example.

I have tried to run it and it runs fine without displaying any errors. Buy it never actually sends an email! Do I have to add anything to the project or this code should run without any modifications (other than the username and password of course).

Nice work thanks! Oh and for the last commenter, don’t forget to set the Andriod permission to use the internet in your manifest ()

Do you see anything in the log? It should emit quite a few debug messages.

Hello Every One .. i have tried to instantiate the SMTPClient by
SMTPClient client= new SMTPClient(“UTF-8″);
But its giving an error saying that application has stopped unexpectedly

Hi Sasi,

can you check the log on your android device and paste the relevant parts here? That should shed some more light on what is wrong.

Hi,

Great snippet! I’ve been able to send an email without all the JavaMail junk thanks to this.

But one question though, can this send an email to multiple receivers? e.g. (cc: user1@try.com,user2@try.com,user3@try.com) ? If it’s possible, how would you implement it?

I’ve use this snippet fine.except I got the ConnectionException: smtp.gmail.com/72.14.213.109:587 – Connection refused

Anything conf need to be change here?