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)
thanks..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)