Last active
March 9, 2016 07:26
-
-
Save sharish/f42e3e88abbbaa197497 to your computer and use it in GitHub Desktop.
Send Email Animation Along with sending of mail.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.cooltechworks.envelopeanimation; | |
import android.content.Intent; | |
import android.os.AsyncTask; | |
import android.os.Handler; | |
import android.support.v7.app.ActionBarActivity; | |
import android.os.Bundle; | |
import android.support.v7.app.AppCompatActivity; | |
import android.util.Log; | |
import android.view.Menu; | |
import android.view.MenuItem; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.view.animation.Animation; | |
import android.view.animation.AnimationSet; | |
import android.view.animation.RotateAnimation; | |
import android.view.animation.ScaleAnimation; | |
import android.view.animation.TranslateAnimation; | |
import android.widget.RelativeLayout; | |
import android.widget.TextView; | |
import com.sun.mail.smtp.SMTPTransport; | |
import java.util.Date; | |
import java.util.Properties; | |
import javax.mail.Message; | |
import javax.mail.MessagingException; | |
import javax.mail.Session; | |
import javax.mail.internet.InternetAddress; | |
import javax.mail.internet.MimeMessage; | |
public class MainActivity extends AppCompatActivity { | |
private static final int EMAIL_ACTIVITY_REQUEST = 1; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.act_main); | |
findViewById(R.id.show_dialog_btn).setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
Intent intent = new Intent(MainActivity.this, EnvelopeActivity.class); | |
startActivityForResult(intent, EMAIL_ACTIVITY_REQUEST); | |
} | |
}); | |
} | |
public void onActivityResult(int requestCode, int resultCode, Intent data) { | |
if(requestCode == EMAIL_ACTIVITY_REQUEST && resultCode == RESULT_OK) { | |
String[] to = data.getStringArrayExtra(Intent.EXTRA_EMAIL); | |
String subject = data.getStringExtra(Intent.EXTRA_SUBJECT); | |
String msg = data.getStringExtra(Intent.EXTRA_TEXT); | |
Log.d(MainActivity.class.getSimpleName(),"To:"+to[0]); | |
Log.d(MainActivity.class.getSimpleName(),"Subject:"+subject); | |
Log.d(MainActivity.class.getSimpleName(),"Msg:"+msg); | |
sendEmail(to[0],subject,msg); | |
} | |
} | |
public void sendEmail(String toAddress, String subject, String msgBody) { | |
String fromAddress = "[email protected]"; | |
String smtpHost = "<your SMTP Host>"; | |
String smtpLogin = "<your SMTP Login>"; | |
String smtpPassword = "<your SMTP Password>"; | |
new AsyncTask<String, Void, Void>() { | |
@Override | |
protected Void doInBackground(String... params) { | |
String fromAddress = params[0]; | |
String toAddress = params[1]; | |
String subject = params[2]; | |
String msgBody = params[3]; | |
String smtpLogin = params[4]; | |
String smtpPassword = params[5]; | |
String smtpHost = params[6]; | |
try { | |
Properties props = System.getProperties(); | |
props.put("mail.smtps.host", smtpHost); | |
props.put("mail.smtps.auth", "true"); | |
Session session = Session.getInstance(props, null); | |
Message msg = new MimeMessage(session); | |
msg.setFrom(new InternetAddress(fromAddress)); | |
msg.setRecipients(Message.RecipientType.TO, | |
InternetAddress.parse(toAddress, false)); | |
msg.setSubject(subject); | |
msg.setText(msgBody); | |
msg.setSentDate(new Date()); | |
SMTPTransport t = | |
(SMTPTransport) session.getTransport("smtps"); | |
t.connect(smtpHost, smtpLogin, smtpPassword); | |
t.sendMessage(msg, msg.getAllRecipients()); | |
System.out.println("Response: " + t.getLastServerResponse()); | |
t.close(); | |
} catch (MessagingException e) { | |
e.printStackTrace(); | |
} | |
return null; | |
} | |
}.execute(fromAddress, toAddress, subject, msgBody,smtpLogin,smtpPassword,smtpHost ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment