Create New Android Project and after that add below code into your MainActivity.java and activity_main.xml file.
MainActivity.java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button login = (Button) findViewById(R.id.mBtnSubmit);
login.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("dipakkeshariya@android.com", "dipakkeshariya");
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("dipak.keshariya@android.com"));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("dipak.keshariya@mobileappdeveloper.com"));
message.setSubject("Testing Subject");
message.setContent("Hi Dipak Keshariya (Android Developer)", "text/html; charset=utf-8");
Transport.send(message);
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
});
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world" />
<Button
android:id="@+id/mBtnSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dip"
android:text="Send Mail" />
</RelativeLayout>
Download below 3 jar file and add into project's build path.
1) activation.jar
2) additionnal.jar
3) mail.jar
Give Following uses-permission into your Android Manifest file
1) <uses-permission android:name="android.permission.INTERNET"/>
Now Run Your Project.
Enjoy :-)
Don’t forget to provide feedback or follow this blog, if you find this blog is useful.
MainActivity.java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button login = (Button) findViewById(R.id.mBtnSubmit);
login.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("dipakkeshariya@android.com", "dipakkeshariya");
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("dipak.keshariya@android.com"));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("dipak.keshariya@mobileappdeveloper.com"));
message.setSubject("Testing Subject");
message.setContent("Hi Dipak Keshariya (Android Developer)", "text/html; charset=utf-8");
Transport.send(message);
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
});
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world" />
<Button
android:id="@+id/mBtnSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dip"
android:text="Send Mail" />
</RelativeLayout>
Download below 3 jar file and add into project's build path.
1) activation.jar
2) additionnal.jar
3) mail.jar
Give Following uses-permission into your Android Manifest file
1) <uses-permission android:name="android.permission.INTERNET"/>
Now Run Your Project.
Enjoy :-)
Don’t forget to provide feedback or follow this blog, if you find this blog is useful.
From where we should download and add these three jar files
ReplyDelete1) activation.jar
2) additionnal.jar
3) mail.jar
can u share these three jar files
I did not received Email on Gmail Account, what can i do plz rply me urgently.
DeleteHello Naveed,
DeleteI updated my post, now check it.
still there is no link man..
DeleteHello Serhat,
DeleteYou are asking about which type of link?
Its work for me thanks bro.
ReplyDeleteBut i have a question for u. I want to send mail using Edittext fields like your first post. when i message u. plz resolve problem in your first post.
GMailSender.java
SendMail.java
JSSEProvider.java
In these classes.
Hello Naveed,
DeletePass Edittext Value of UserName, FromId, ToId, Password instead of default value.
plz ans how to send my own server like my companies own xyz.xxx@companyanem.com
ReplyDeleteHi how r you bro. Hope u will b fine...
ReplyDeleteI have a question?
I want to use this whole code in a single function in my forgot password activity, And i m using Async task with SOAP web service. when i call this function in do in background its does not send mail and give error that "class not found and further my project pakage name".
What i ma doing in class?
when user forgotpassword and enter email address in edittext field, email during in my app registration form in edittext field.
when press enter user will received new password on account that he or she enter in edittext. like gmail, yahoo, hotmail accounts.
Wating for your rply.................
Hi i get following error, send the correct code
ReplyDelete03-01 07:53:38.906: E/AndroidRuntime(1711): FATAL EXCEPTION: main
03-01 07:53:38.906: E/AndroidRuntime(1711): android.os.NetworkOnMainThreadException
03-01 07:53:38.906: E/AndroidRuntime(1711): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
03-01 07:53:38.906: E/AndroidRuntime(1711): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
03-01 07:53:38.906: E/AndroidRuntime(1711): at java.net.InetAddress.getLocalHost(InetAddress.java:365)
03-01 07:53:38.906: E/AndroidRuntime(1711): at javax.mail.internet.InternetAddress.getLocalAddress(InternetAddress.java:517)
03-01 07:53:38.906: E/AndroidRuntime(1711): at javax.mail.internet.UniqueValue.getUniqueMessageIDValue(UniqueValue.java:99)
03-01 07:53:38.906: E/AndroidRuntime(1711): at javax.mail.internet.MimeMessage.updateMessageID(MimeMessage.java:2054)
03-01 07:53:38.906: E/AndroidRuntime(1711): at javax.mail.internet.MimeMessage.updateHeaders(MimeMessage.java:2076)
03-01 07:53:38.906: E/AndroidRuntime(1711): at javax.mail.internet.MimeMessage.saveChanges(MimeMessage.java:2042)
03-01 07:53:38.906: E/AndroidRuntime(1711): at javax.mail.Transport.send(Transport.java:117)
03-01 07:53:38.906: E/AndroidRuntime(1711): at com.my.mail.MainActivity$1.onClick(MainActivity.java:49)
03-01 07:53:38.906: E/AndroidRuntime(1711): at android.view.View.performClick(View.java:4202)
03-01 07:53:38.906: E/AndroidRuntime(1711): at android.view.View$PerformClick.run(View.java:17340)
03-01 07:53:38.906: E/AndroidRuntime(1711): at android.os.Handler.handleCallback(Handler.java:725)
03-01 07:53:38.906: E/AndroidRuntime(1711): at android.os.Handler.dispatchMessage(Handler.java:92)
03-01 07:53:38.906: E/AndroidRuntime(1711): at android.os.Looper.loop(Looper.java:137)
03-01 07:53:38.906: E/AndroidRuntime(1711): at android.app.ActivityThread.main(ActivityThread.java:5039)
03-01 07:53:38.906: E/AndroidRuntime(1711): at java.lang.reflect.Method.invokeNative(Native Method)
03-01 07:53:38.906: E/AndroidRuntime(1711): at java.lang.reflect.Method.invoke(Method.java:511)
03-01 07:53:38.906: E/AndroidRuntime(1711): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-01 07:53:38.906: E/AndroidRuntime(1711): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-01 07:53:38.906: E/AndroidRuntime(1711): at dalvik.system.NativeStart.main(Native Method)
You have to do the networking stuff in a background thread. See AsyncTask. :)
Deletethanks..its working...if i want to attache file how can i attach???
ReplyDeletethe app stoops unexpectedly in my phone and getting the following error please help.Thank in advance........(am a beginner in android)...
ReplyDelete04-01 12:32:41.822: E/AndroidRuntime(9616): FATAL EXCEPTION: main
04-01 12:32:41.822: E/AndroidRuntime(9616): java.lang.NoClassDefFoundError: com.tpppp.tsend.MainActivity$1$1
04-01 12:32:41.822: E/AndroidRuntime(9616): at com.tpppp.tsend.MainActivity$1.onClick(MainActivity.java:37)
04-01 12:32:41.822: E/AndroidRuntime(9616): at android.view.View.performClick(View.java:2532)
04-01 12:32:41.822: E/AndroidRuntime(9616): at android.view.View$PerformClick.run(View.java:9277)
04-01 12:32:41.822: E/AndroidRuntime(9616): at android.os.Handler.handleCallback(Handler.java:587)
04-01 12:32:41.822: E/AndroidRuntime(9616): at android.os.Handler.dispatchMessage(Handler.java:92)
04-01 12:32:41.822: E/AndroidRuntime(9616): at android.os.Looper.loop(Looper.java:143)
04-01 12:32:41.822: E/AndroidRuntime(9616): at android.app.ActivityThread.main(ActivityThread.java:4196)
04-01 12:32:41.822: E/AndroidRuntime(9616): at java.lang.reflect.Method.invokeNative(Native Method)
04-01 12:32:41.822: E/AndroidRuntime(9616): at java.lang.reflect.Method.invoke(Method.java:507)
04-01 12:32:41.822: E/AndroidRuntime(9616): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-01 12:32:41.822: E/AndroidRuntime(9616): at dalvik.system.NativeStart.main(Native Method)
my application creshed when I click on Send Email Button.
ReplyDeletelog error is- java.lang.NoClassDefFoundError: javax.activation.DataHandler.
nice, could you send me the link for the attached libraries?thanks!
ReplyDeletewhile running the app it throws exception. log error is android.os.NetworkOnMainThreadException.
ReplyDeleteif someone has the same problem that I follow:
ReplyDeleteproblem with:
Session session = Session.getDefaultInstance (props, new javax.mail.Authenticator () {
protected PasswordAuthentication getPasswordAuthentication () {
return new PasswordAuthentication ("dipakkeshariya@android.com", "dipakkeshariya");
}
});
problem solved:
Session session = Session.getDefaultInstance (props, new javax.mail.Authenticator () {
protected javax.mail.PasswordAuthentication getPasswordAuthentication () {
return new javax.mail.PasswordAuthentication ("dipakkeshariya@android.com", "dipakkeshariya);
}
});
import javax.mail.PasswordAuthentication;
Deletenot work
ReplyDeleteGETTING THIS ERROR !!
ReplyDeleteDon't know what to do !!
If any one know how to crack this , please share your view !
08-19 09:10:26.335: W/dalvikvm(1036): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
08-19 09:10:26.355: E/AndroidRuntime(1036): FATAL EXCEPTION: main
08-19 09:10:26.355: E/AndroidRuntime(1036): java.lang.NoClassDefFoundError: com.android.app.frgoto.Forgotpassword$2
08-19 09:10:26.355: E/AndroidRuntime(1036): at com.android.app.frgoto.Forgotpassword.sendEMail(Forgotpassword.java:128)
08-19 09:10:26.355: E/AndroidRuntime(1036): at com.android.app.frgoto.Forgotpassword.sendMail(Forgotpassword.java:71)
08-19 09:10:26.355: E/AndroidRuntime(1036): at com.android.app.frgoto.Forgotpassword$1.onClick(Forgotpassword.java:51)
08-19 09:10:26.355: E/AndroidRuntime(1036): at android.view.View.performClick(View.java:4084)
08-19 09:10:26.355: E/AndroidRuntime(1036): at android.view.View$PerformClick.run(View.java:16966)
08-19 09:10:26.355: E/AndroidRuntime(1036): at android.os.Handler.handleCallback(Handler.java:615)
08-19 09:10:26.355: E/AndroidRuntime(1036): at android.os.Handler.dispatchMessage(Handler.java:92)
08-19 09:10:26.355: E/AndroidRuntime(1036): at android.os.Looper.loop(Looper.java:137)
08-19 09:10:26.355: E/AndroidRuntime(1036): at android.app.ActivityThread.main(ActivityThread.java:4745)
08-19 09:10:26.355: E/AndroidRuntime(1036): at java.lang.reflect.Method.invokeNative(Native Method)
08-19 09:10:26.355: E/AndroidRuntime(1036): at java.lang.reflect.Method.invoke(Method.java:511)
08-19 09:10:26.355: E/AndroidRuntime(1036): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
08-19 09:10:26.355: E/AndroidRuntime(1036): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-19 09:10:26.355: E/AndroidRuntime(1036): at dalvik.system.NativeStart.main(Native Method)
08-19 09:10:28.245: I/Process(1036): Sending signal. PID: 1036 SIG: 9
And the problem arises when the pointer reaches to this section of the code (I tired to debug it)
ReplyDeleteSession session = Session.getDefaultInstance (props, new javax.mail.Authenticator () {
protected PasswordAuthentication getPasswordAuthentication () {
return new PasswordAuthentication ("dipakkeshariya@android.com", "dipakkeshariya");
}
});
import javax.mail.PasswordAuthentication;
Deletegetting an exception at transport.send(message) .... how to resolve this problem
ReplyDelete01-16 10:05:24.462: E/AndroidRuntime(4926): FATAL EXCEPTION: main
ReplyDelete01-16 10:05:24.462: E/AndroidRuntime(4926): android.os.NetworkOnMainThreadException
01-16 10:05:24.462: E/AndroidRuntime(4926): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1125)
01-16 10:05:24.462: E/AndroidRuntime(4926): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
01-16 10:05:24.462: E/AndroidRuntime(4926): at java.net.InetAddress.getLocalHost(InetAddress.java:365)
01-16 10:05:24.462: E/AndroidRuntime(4926): at javax.mail.internet.InternetAddress.getLocalAddress(InternetAddress.java:517)
01-16 10:05:24.462: E/AndroidRuntime(4926): at javax.mail.internet.UniqueValue.getUniqueMessageIDValue(UniqueValue.java:99)
01-16 10:05:24.462: E/AndroidRuntime(4926): at javax.mail.internet.MimeMessage.updateMessageID(MimeMessage.java:2054)
01-16 10:05:24.462: E/AndroidRuntime(4926): at javax.mail.internet.MimeMessage.updateHeaders(MimeMessage.java:2076)
01-16 10:05:24.462: E/AndroidRuntime(4926): at javax.mail.internet.MimeMessage.saveChanges(MimeMessage.java:2042)
01-16 10:05:24.462: E/AndroidRuntime(4926): at javax.mail.Transport.send(Transport.java:117)
01-16 10:05:24.462: E/AndroidRuntime(4926): at com.example.emailsample.MainActivity$1.onClick(MainActivity.java:52)
01-16 10:05:24.462: E/AndroidRuntime(4926): at android.view.View.performClick(View.java:4091)
01-16 10:05:24.462: E/AndroidRuntime(4926): at android.view.View$PerformClick.run(View.java:17072)
01-16 10:05:24.462: E/AndroidRuntime(4926): at android.os.Handler.handleCallback(Handler.java:615)
01-16 10:05:24.462: E/AndroidRuntime(4926): at android.os.Handler.dispatchMessage(Handler.java:92)
01-16 10:05:24.462: E/AndroidRuntime(4926): at android.os.Looper.loop(Looper.java:153)
01-16 10:05:24.462: E/AndroidRuntime(4926): at android.app.ActivityThread.main(ActivityThread.java:5086)
01-16 10:05:24.462: E/AndroidRuntime(4926): at java.lang.reflect.Method.invokeNative(Native Method)
01-16 10:05:24.462: E/AndroidRuntime(4926): at java.lang.reflect.Method.invoke(Method.java:511)
01-16 10:05:24.462: E/AndroidRuntime(4926): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
01-16 10:05:24.462: E/AndroidRuntime(4926): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
01-16 10:05:24.462: E/AndroidRuntime(4926): at dalvik.system.NativeStart.main(Native Method)
It is giving me unfortunately stopped on clicking send
ReplyDeletesend mails good...
ReplyDeletewhat about receiving?
I want to add email address to reply to path via edit text
ReplyDeleteI don’t think many of websites provide this type of information.
ReplyDeleteGmail Technical Support
plzzz give me zip file of this code with jar files..
ReplyDeleten tell where to paste these files??
showing an error while running
ReplyDeleteTransport.send(message);
Hello there,
ReplyDeleteThis code is absolutely great! How would I be able to attach a file with the mail? The file would be internal for example...
Thanks again for the help :)
I m getting error in line:
ReplyDeleteSession session = Session.getDefaultInstance(props, new javax.mail.Authenticator()
in Session
pls help
THis is great! How can I a pre-written emaiil without using any buttons or user inputting any data. I just want to send a gmail or yahoo message in the background every so often and then retrieve the response sent by the server in the background as well. So how do I modify your code to remove the buttons and lets say I want to send an email every 5 minutes get the response from the server.
ReplyDelete