Send_emai_direct_msg_gmail_ma_jse
//send email vado che program ie me je email nakis aena mail ma msg poche jse
ie aa program ma maro emai id and pwd che to me je msg kris ae msg mara email id pr msg aavi jse
//aama fragment che ie drawer activity lai leve aema fragment lai levu and je fragment nu name hoy ae drawer parse krave devu(parse ie fragment manager vadp code)
//and pche gmail account open karvanu aema google account ma click karvanu then Sign in and Security pr click karvanu and
and last ma Allow less secure apps:ON kre devu
//aama library ma jai ne jar file nakhi deve =library ma jvu hoy to android pr click karvu aema pkg ma jvanu pche app pr click kre ne library aavse aama 3 jar file nakhi deve
//jar file nakha mate 1)file ma jvanu and project structure pr click karvu 2)app ma click karvu and
dependancy ma jvu 3)last ma green color na plus synbol pr click karvu and and jar dependancy ma add kre devu dependancy4) jar dependacy ma jai ne 3 jar file libs ma copy paste kre deve
*jar file drive mati lai leve name jar file nu:JavaMailApi_ideal
youtube:How to send email using java mail api in androd 7:57min
step1:fragment.xml and fragment.java
step2:send email ni java file leve and manifest ma inteernet ni permiision lai leve
step 1:fragment.xml
<LinearLayout 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"
android:orientation="vertical"
tools:context="demoproject.aalap.com.akk.abcFragment">
<EditText
android:ems="10"
android:id="@+id/edtEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Gmail Id"/>
<EditText
android:ems="10"
android:id="@+id/edtSubject"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Subject"/>
<EditText
android:ems="10"
android:id="@+id/edtDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Message Text"
android:inputType="textPostalAddress"/>
<Button
android:id="@+id/btnsubmit"
android:text="Send Mail"
android:layout_marginTop="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Step 2:fragment.java
/**
* A simple {@link Fragment} subclass.
*/
public class abcFragment extends Fragment implements View.OnClickListener{
View view;
String setemail;
private EditText edtEmail;
private EditText edtSubject;
private EditText edtDescription;
//Send button
private Button btnsubmit;
// private SessionManager session;
public abcFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view= inflater.inflate(R.layout.fragment_abc, container, false);
//session = new SessionManager(getActivity());//loadSession();
edtEmail = view.findViewById(R.id.edtEmail);
edtSubject = view.findViewById(R.id.edtSubject);
edtDescription = view.findViewById(R.id.edtDescription);
edtEmail.setText(setemail);
btnsubmit =view.findViewById(R.id.btnsubmit);
//Adding click listener
btnsubmit.setOnClickListener(this);
// Inflate the layout for this fragment
return view;
}
private void sendEmail() {
//Getting content for email
String email = edtEmail.getText().toString().trim();
String subject = edtSubject.getText().toString().trim();
String message = edtDescription.getText().toString().trim();
if(email.isEmpty() || subject.isEmpty() || message.isEmpty())
{
Toast.makeText(getActivity(),"Please fill all the fields",Toast.LENGTH_LONG).show();
} else if(!Patterns.EMAIL_ADDRESS.matcher(email).matches()){
Toast.makeText(getActivity(),"Please fill a valid email address", Toast.LENGTH_SHORT).show();
} else {
//Creating SendMail object
SendMail sm = new SendMail(getActivity(), email, subject, message);
//Executing sendmail to send email
sm.execute();
edtSubject.setText("");
edtDescription.setText("");
}
}
@Override
public void onClick(View view) {
sendEmail();
}
private void loadSession() {
// HashMap<String, String> user = session.getUserDetails();
//setemail = user.get(SessionManager.KEY_EMAIL);
}
}
Step 3:sendmail.java
public class SendMail extends AsyncTask<Void,Void,Void> {
//Declaring Variables
private Context context;
private Session session;
//Information to send email
private String email;
private String subject;
private String message;
//Progressdialog to show while sending email
private ProgressDialog progressDialog;
//Class Constructor
public SendMail(Context context, String email, String subject, String message){
//Initializing variables
this.context = context;
this.email = email;
this.subject = subject;
this.message = message;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
//Showing progress dialog while sending email
progressDialog = ProgressDialog.show(context,"Sending message","Please wait...",false,false);
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
//Dismissing the progress dialog
progressDialog.dismiss();
//Showing a success message
Toast.makeText(context,"Message Sent", Toast.LENGTH_LONG).show();
}
@Override
protected Void doInBackground(Void... params) {
//Creating properties
Properties props = new Properties();
//Configuring properties for gmail
//If you are not using gmail you may need to change the values
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");
//Creating a new session
session = Session.getDefaultInstance(props,
new Authenticator() {
//Authenticating the password
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("aalapmistry11@gmail.com", "aalapaalap");
}
});
try {
//Creating MimeMessage object
MimeMessage mm = new MimeMessage(session);
//Setting sender address
mm.setFrom(new InternetAddress("aalapmistry11@gmail.com"));
//Adding receiver
mm.addRecipient(Message.RecipientType.TO, new InternetAddress(email));
//Adding subject
mm.setSubject(subject);
//Adding message
mm.setText(message);
//Sending email
Transport.send(mm);
} catch (MessagingException e) {
e.printStackTrace();
}
return null;
}
}
ie aa program ma maro emai id and pwd che to me je msg kris ae msg mara email id pr msg aavi jse
//aama fragment che ie drawer activity lai leve aema fragment lai levu and je fragment nu name hoy ae drawer parse krave devu(parse ie fragment manager vadp code)
//and pche gmail account open karvanu aema google account ma click karvanu then Sign in and Security pr click karvanu and
and last ma Allow less secure apps:ON kre devu
//aama library ma jai ne jar file nakhi deve =library ma jvu hoy to android pr click karvu aema pkg ma jvanu pche app pr click kre ne library aavse aama 3 jar file nakhi deve
//jar file nakha mate 1)file ma jvanu and project structure pr click karvu 2)app ma click karvu and
dependancy ma jvu 3)last ma green color na plus synbol pr click karvu and and jar dependancy ma add kre devu dependancy4) jar dependacy ma jai ne 3 jar file libs ma copy paste kre deve
*jar file drive mati lai leve name jar file nu:JavaMailApi_ideal
youtube:How to send email using java mail api in androd 7:57min
step1:fragment.xml and fragment.java
step2:send email ni java file leve and manifest ma inteernet ni permiision lai leve
step 1:fragment.xml
<LinearLayout 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"
android:orientation="vertical"
tools:context="demoproject.aalap.com.akk.abcFragment">
<EditText
android:ems="10"
android:id="@+id/edtEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Gmail Id"/>
<EditText
android:ems="10"
android:id="@+id/edtSubject"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Subject"/>
<EditText
android:ems="10"
android:id="@+id/edtDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Message Text"
android:inputType="textPostalAddress"/>
<Button
android:id="@+id/btnsubmit"
android:text="Send Mail"
android:layout_marginTop="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Step 2:fragment.java
/**
* A simple {@link Fragment} subclass.
*/
public class abcFragment extends Fragment implements View.OnClickListener{
View view;
String setemail;
private EditText edtEmail;
private EditText edtSubject;
private EditText edtDescription;
//Send button
private Button btnsubmit;
// private SessionManager session;
public abcFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view= inflater.inflate(R.layout.fragment_abc, container, false);
//session = new SessionManager(getActivity());//loadSession();
edtEmail = view.findViewById(R.id.edtEmail);
edtSubject = view.findViewById(R.id.edtSubject);
edtDescription = view.findViewById(R.id.edtDescription);
edtEmail.setText(setemail);
btnsubmit =view.findViewById(R.id.btnsubmit);
//Adding click listener
btnsubmit.setOnClickListener(this);
// Inflate the layout for this fragment
return view;
}
private void sendEmail() {
//Getting content for email
String email = edtEmail.getText().toString().trim();
String subject = edtSubject.getText().toString().trim();
String message = edtDescription.getText().toString().trim();
if(email.isEmpty() || subject.isEmpty() || message.isEmpty())
{
Toast.makeText(getActivity(),"Please fill all the fields",Toast.LENGTH_LONG).show();
} else if(!Patterns.EMAIL_ADDRESS.matcher(email).matches()){
Toast.makeText(getActivity(),"Please fill a valid email address", Toast.LENGTH_SHORT).show();
} else {
//Creating SendMail object
SendMail sm = new SendMail(getActivity(), email, subject, message);
//Executing sendmail to send email
sm.execute();
edtSubject.setText("");
edtDescription.setText("");
}
}
@Override
public void onClick(View view) {
sendEmail();
}
private void loadSession() {
// HashMap<String, String> user = session.getUserDetails();
//setemail = user.get(SessionManager.KEY_EMAIL);
}
}
Step 3:sendmail.java
public class SendMail extends AsyncTask<Void,Void,Void> {
//Declaring Variables
private Context context;
private Session session;
//Information to send email
private String email;
private String subject;
private String message;
//Progressdialog to show while sending email
private ProgressDialog progressDialog;
//Class Constructor
public SendMail(Context context, String email, String subject, String message){
//Initializing variables
this.context = context;
this.email = email;
this.subject = subject;
this.message = message;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
//Showing progress dialog while sending email
progressDialog = ProgressDialog.show(context,"Sending message","Please wait...",false,false);
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
//Dismissing the progress dialog
progressDialog.dismiss();
//Showing a success message
Toast.makeText(context,"Message Sent", Toast.LENGTH_LONG).show();
}
@Override
protected Void doInBackground(Void... params) {
//Creating properties
Properties props = new Properties();
//Configuring properties for gmail
//If you are not using gmail you may need to change the values
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");
//Creating a new session
session = Session.getDefaultInstance(props,
new Authenticator() {
//Authenticating the password
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("aalapmistry11@gmail.com", "aalapaalap");
}
});
try {
//Creating MimeMessage object
MimeMessage mm = new MimeMessage(session);
//Setting sender address
mm.setFrom(new InternetAddress("aalapmistry11@gmail.com"));
//Adding receiver
mm.addRecipient(Message.RecipientType.TO, new InternetAddress(email));
//Adding subject
mm.setSubject(subject);
//Adding message
mm.setText(message);
//Sending email
Transport.send(mm);
} catch (MessagingException e) {
e.printStackTrace();
}
return null;
}
}
Comments
Post a Comment