Download the SKPSMTP library and import
#import "SKPSMTPMessage.h" #import "NSData+Base64Additions.h" -(IBAction)btnRecoverClicked:(id)Sender;
Then we implement the method of sending mail in the background.
-(IBAction) btnRecoverClicked:(id)sender { NSString *str=@"Your password is:"; NSString *strUserPassword=[NSString stringWithFormat:@"%@ %@",str,struserPassword]; NSLog(@"Start Sending"); SKPSMTPMessage *emailMessage = [[SKPSMTPMessage alloc] init]; emailMessage.fromEmail = @"XXXXX"; //sender email address emailMessage.toEmail = struserEmail; //receiver email address emailMessage.relayHost = @"smtp.gmail.com"; //emailMessage.ccEmail =@"your cc address"; //emailMessage.bccEmail =@"your bcc address"; emailMessage.requiresAuth = YES; emailMessage.login = @"xxxxxxxx"; //sender email address emailMessage.pass = @"XXXXXXX"; //sender email password emailMessage.subject =@"Password Recovery"; emailMessage.wantsSecure = YES; emailMessage.delegate = self; // you must include <SKPSMTPMessageDelegate> to your class NSString *messageBody = [NSString stringWithFormat:@"Your password is: %@",struserPassword] ; //for example : NSString *messageBody = [NSString stringWithFormat:@"Tour Name: %@\nName: %@\nEmail: %@\nContact No: %@\nAddress: %@\nNote: %@",selectedTour,nameField.text,emailField.text,foneField.text,addField.text,txtView.text]; // Now creating plain text email message NSDictionary *plainMsg = [NSDictionary dictionaryWithObjectsAndKeys:@"text/plain",kSKPSMTPPartContentTypeKey, messageBody,kSKPSMTPPartMessageKey,@"8bit",kSKPSMTPPartContentTransferEncodingKey,nil]; emailMessage.parts = [NSArray arrayWithObjects:plainMsg,nil]; //in addition : Logic for attaching file with email message. /* NSString *filePath = [[NSBundle mainBundle] pathForResource:@"filename" ofType:@"JPG"]; NSData *fileData = [NSData dataWithContentsOfFile:filePath]; NSDictionary *fileMsg = [NSDictionary dictionaryWithObjectsAndKeys:@"text/directory;\r\n\tx- unix-mode=0644;\r\n\tname=\"filename.JPG\"",kSKPSMTPPartContentTypeKey,@"attachment;\r\n\tfilename=\"filename.JPG\"",kSKPSMTPPartContentDispositionKey,[fileData encodeBase64ForData],kSKPSMTPPartMessageKey,@"base64",kSKPSMTPPartContentTransferEncodingKey,nil]; emailMessage.parts = [NSArray arrayWithObjects:plainMsg,fileMsg,nil]; //including plain msg and attached file msg */ [emailMessage send]; // sending email- will take little time to send so its better to use indicator with message showing sending... }
To cope with success and failed to use
-(void)messageSent:(SKPSMTPMessage *)message{ NSLog(@"delegate - message sent"); UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Message sent to your mail." message:nil delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil]; [alert show]; }
and
-(void)messageFailed:(SKPSMTPMessage *)message error:(NSError *)error{
sudhakkar k
source share