Thursday 8 March 2012

how to send Mail.........

-(void)sendMail{
   
    if ([MFMailComposeViewController canSendMail]){
        MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
        picker.mailComposeDelegate = self;
       
        NSDate *date = [NSDate date];
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"MM/dd/yyyy HH:mm:ss a"];
        NSString *strDate = [formatter stringFromDate:date];
       
        [picker setSubject:[NSString stringWithFormat:@"Contact Backup : %@", strDate]];
       
        // Set up recipients
        NSArray *toRecipients = [NSArray arrayWithObject:@""];

        [picker setToRecipients:toRecipients];
       
       Support/iPhone Simulator/5.0/Applications/8FFDED4D-0FEF-4E3C-9B43-3677E6B4881D/Documents/contacts.vcf"];
      
        NSURL *theURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"contacts.vcf"];
       
        NSData *myData = [NSData dataWithContentsOfURL:theURL];
        // NSData *myData = [NSData dataWithContentsOfFile:path];
        [picker addAttachmentData:myData mimeType:@"text/vcard" fileName:@"contacts.vcf"];
       
        // Fill out the email body text
        NSString *emailBody = @"Here I am sending Contact Backup. \n \nNote: Send this email to yourself and when you lost contact use this email and restore contacts.";
        [picker setMessageBody:emailBody isHTML:NO];
        picker.mailComposeDelegate=self;
        [self presentModalViewController:picker animated:YES];
        [picker release];
        btnBackup.tag=1;
        [btnBackup setImage:[UIImage imageNamed:@"back up.png"] forState:UIControlStateNormal];
        [btnBackup removeTarget:self action:@selector(sendMail) forControlEvents:UIControlEventTouchUpInside];
        [btnBackup addTarget:self action:@selector(btnBackupClicked:) forControlEvents:UIControlEventTouchUpInside];
        progressCount = 0;
        progressView.progress=progressCount;
    }
   
}

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
    lblContact.textAlignment = UITextAlignmentCenter;
    lblContact.font = [UIFont boldSystemFontOfSize:18];
    lblContact.hidden = NO;
    // Notifies users about errors associated with the interface
    switch (result)
    {
        case MFMailComposeResultCancelled:
            lblContact.text = @"Mail Sending cancelled";
            break;
        case MFMailComposeResultSaved:
            lblContact.text = @"Mail saved";
            break;
        case MFMailComposeResultSent:
            lblContact.text = @"Mail sent successfully";
            break;
        case MFMailComposeResultFailed:
            lblContact.text = @"Sending failed";
            break;
        default:
            lblContact.text = @"Mail not sent";
            break;
    }
    [self dismissModalViewControllerAnimated:YES];
}

No comments:

Post a Comment