Hello Friends I am new in android. I am developing one application of Daily Report.in this application i want to send an Email automatically in every 24 hours to user. i found how t send email by using my application without Intent.but i could not get a solution that how to send an email in every 24 hours please help me..
public class SendAttachment
{
public static void main()
{
String to="emailaddress";
final String user="emailaddress";//change accordingly
final String password="password";//change accordingly
MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
System.out.println("111111111111111111111111111111111111111111111111");
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);
Properties properties = System.getProperties();
properties.put("mail.smtp.port", "465");
properties.put("mail.smtp.host", "smtp.gmail.com");
properties.put("mail.smtp.socketFactory.port", "465");
properties.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.port", "465");
javax.mail.Session session=javax.mail.Session.getDefaultInstance(properties,new Authenticator()
{
protected javax.mail.PasswordAuthentication getPasswordAuthentication(){
return new javax.mail.PasswordAuthentication(user, password);
}
});
try
{
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(user));
message.addRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress(to));
message.setSubject("text");
//3) create MimeBodyPart object and set your message content
BodyPart messageBodyPart1 = new MimeBodyPart();
messageBodyPart1.setText("Daily ConstructionReport");
//4) create new MimeBodyPart object and set DataHandler object to this object
MimeBodyPart messageBodyPart2 = new MimeBodyPart();
//Location of file to be attached
String filename = Environment.getExternalStorageDirectory().getPath()+"/text/tedt_unu.pdf";//change accordingly
DataSource source = new FileDataSource(filename);
messageBodyPart2.setDataHandler(new DataHandler(source));
messageBodyPart2.setFileName("Report");
//5) create Multipart object and add MimeBodyPart objects to this object
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart1);
multipart.addBodyPart(messageBodyPart2);
//6) set the multiplart object to the message object
message.setContent(multipart);
//7) send message
Transport.send(message);
System.out.println("MESSAGE SENT....");
}
catch (MessagingException ex)
{
ex.printStackTrace();
}
}
}
This is My Send Attachment class when i want to send email i had just call a method SendAttachment.main(). and now where i could put this method so that i can send an email in every 24 hours..
public class PollReceiver extends BroadcastReceiver
{
private static final int PERIOD=5000;
@Override
public void onReceive(Context ctxt, Intent i)
{
scheduleAlarms(ctxt);
}
static void scheduleAlarms(Context ctxt)
{
AlarmManagermgr=(AlarmManager)ctxt.getSystemService(Context.ALARM_SERVICE);
Intent i=new Intent(ctxt, ScheduledService.class);
PendingIntent pi=PendingIntent.getService(ctxt, 0, i, 0);
mgr.setRepeating(AlarmManager.ELAPSED_REALTIME,
SystemClock.elapsedRealtime() + 5000,PERIOD, pi);
}
}
This is my Intent service class
public class ScheduledService extends IntentService
{
public ScheduledService()
{
super("ScheduledService");
}
@Override
protected void onHandleIntent(Intent intent)
{
SendAttachment.main();
Log.d(getClass().getSimpleName(), "I ran!");
}
And this is MY Main Activity
CheckBox ch1=(CheckBox)findViewById(R.id.checkBox1);
stopService(new Intent(getApplicationContext(), ScheduledService.class));
ch1.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
// TODO Auto-generated method stub
if(isChecked)
{
PollReceiver.scheduleAlarms(ScheduledServiceDemoActivity.this);
}
else
{
}
});