Click On Button Then Automaticly Send Email In Android
Solution 1:
In my case I used Java Mail API and the code below you can use. Please add the necessary details like user name,password, email ids etc Use this tutorial http://www.jondev.net/articles/Sending_Emails_without_User_Intervention_%28no_Intents%29_in_Android
publicclassMailextendsjavax.mail.Authenticator {
private String _user;
private String _pass;
private String[] _to ;
private String _from;
private String _port;
private String _sport;
private String _host;
private String _subject;
private String _body;
privateboolean _auth;
privateboolean _debuggable;
private Multipart _multipart;
publicMail() {
_host = "smtp.gmail.com"; // default smtp server
_port = "465"; // default smtp port
_sport = "465"; // default socketfactory port
_user = "username"; // username
_pass = "password"; // password
_from = "emailsentfrom@gmail.com"; // email sent from
_to = newString[] {"toemail@gmail.com"};
_subject = "subject"; // email subject
_body = "test"; // email body
_debuggable = false; // debug mode on or off - default off
_auth = true; // smtp authentication - default on
_multipart = newMimeMultipart();
// There is something wrong with MailCap, javamail can not find a handler for the multipart/mixed part, so this bit needs to be added. MailcapCommandMapmc= (MailcapCommandMap) CommandMap.getDefaultCommandMap();
mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");
CommandMap.setDefaultCommandMap(mc);
}
publicMail(String user, String pass) {
_user = user;
_pass = pass;
}
publicbooleansend()throws Exception {
Propertiesprops= _setProperties();
if(!_user.equals("") && !_pass.equals("") && _to.length > 0 && !_from.equals("") && !_subject.equals("") && !_body.equals("")) {
Sessionsession= Session.getInstance(props, this);
MimeMessagemsg=newMimeMessage(session);
msg.setFrom(newInternetAddress(_from));
InternetAddress[] addressTo = newInternetAddress[_to.length];
for (inti=0; i < _to.length; i++) {
addressTo[i] = newInternetAddress(_to[i]);
}
msg.setRecipients(MimeMessage.RecipientType.TO, addressTo);
msg.setSubject(_subject);
msg.setSentDate(newDate());
// setup message body BodyPartmessageBodyPart=newMimeBodyPart();
messageBodyPart.setText(_body);
_multipart.addBodyPart(messageBodyPart);
// adding attachment
addAttachment("filename");//replace with file name u need// Put parts in message
msg.setContent(_multipart);
// send email Transporttransport= session.getTransport("smtps");
transport.connect(_host, 465,_user, _pass);
Transport.send(msg);
returntrue;
} else {
returnfalse;
}
}
publicvoidaddAttachment(String filename)throws Exception {
BodyPartmessageBodyPart=newMimeBodyPart();
DataSourcesource=newFileDataSource(filename);
messageBodyPart.setDataHandler(newDataHandler(source));
messageBodyPart.setFileName(filename);
_multipart.addBodyPart(messageBodyPart);
}
@Overridepublic PasswordAuthentication getPasswordAuthentication() {
returnnewPasswordAuthentication(_user, _pass);
}
private Properties _setProperties() {
Propertiesprops=newProperties();
props.put("mail.smtp.host", _host);
if(_debuggable) {
props.put("mail.debug", "true");
}
if(_auth) {
props.put("mail.smtp.auth", "true");
}
props.put("mail.smtp.port", _port);
props.put("mail.smtp.socketFactory.port", _sport);
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
props.put("mail.smtp.starttls.enable", "true");
return props;
}
// the getters and setters public String getBody() {
return _body;
}
publicvoidsetBody(String _body) {
this._body = _body;
}
// more of the getters and setters É..
}
and write an Async task to send mail
publicclassSendTaskextendsAsyncTask<String, Integer, Integer> {
privateProgressDialog dialog;
privateContext mContext;
publicSendTask(Context mContext){
this.mContext = mContext;
}
protectedvoidonPreExecute() {
this.dialog = newProgressDialog(mContext);
this.dialog.setCancelable(false);
this.dialog.setMessage("sending");
this.dialog.show();
}
protectedIntegerdoInBackground(String... ids) {
Mail mail = newMail();
try {
mail.send();
} catch (Exception e) {
e.printStackTrace();
}
return1;
}
}
Solution 2:
In the xml file for your button, define android:onClick="SendMail"
This will call the SendMail
function inside your activity when your button is clicked.
Next, get a reference to the EditText
view where the user enters the email body.
E.g.: If the resource id of your EditText
view is edittext1
, then in your activity you need to do the following:
EditText ed=(EditText)findViewById(R.id.edittext1);
String emailBody=ed.getText().toString();
Now the emailBody
string consists of the body of the mail that the user has typed in.
Next, inside your activity, you need to define a function SendMail()
as shown below:
voidSendMail(String body) {
String recipient="usrname@mail.com";
String subject="Your subject here";
Intenti=newIntent(Intent.ACTION_SEND);
i.setType("message/rfc822");
i.putExtra(Intent.EXTRA_EMAIL , recipient);
i.putExtra(Intent.EXTRA_SUBJECT, subject);
i.putExtra(Intent.EXTRA_TEXT , body);
try {
startActivity(Intent.createChooser(i, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(MyActivity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
}
}
This approach has helped me, hope it helps you also.
Post a Comment for "Click On Button Then Automaticly Send Email In Android"