This code is use for the sending a mail to another mail id  using gmail account for that you need to import the frame work which name is CFNetwork.framework.

For that you need to implement following code into your project.

1) Create new project and select view-based application.

2) Following code is copy/paste into your project header file like viewcontroller.h file

1
2
3
4
5
6
7
#import <UIKit/UIKit.h>
#import "SKPSMTPMessage.h"
@interface mailTransferViewController : UIViewController <SKPSMTPMessageDelegate>{
    IBOutlet UITextField *emailField;
}
- (IBAction)sendMessageInBack:(id)anObject;
@end

3) Implement following code into your .m file like  viewcontroller.m file

  • For that create the new method called “sendMessageInBack”
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
- (IBAction)sendMessageInBack:(id)anObject{
    NSLog(@"Start Sending");
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"sample.pdf"];
    NSData *dataObj = [NSData dataWithContentsOfFile:writableDBPath];
    SKPSMTPMessage *testMsg = [[SKPSMTPMessage alloc] init];
    testMsg.fromEmail = @"Yours mail ids";
    testMsg.toEmail = emailField.text;//sender mail id
    testMsg.relayHost = @"smtp.gmail.com";
    testMsg.requiresAuth = YES;
    testMsg.login = @"Your mail ids";
    testMsg.pass = @"Mail id password";
    testMsg.subject = @"Test application ";
    testMsg.wantsSecure = YES; // smtp.gmail.com doesn't work without TLS!
    // Only do this for self-signed certs!
    // testMsg.validateSSLChain = NO;
    testMsg.delegate = self;
    NSDictionary *plainPart = [NSDictionary dictionaryWithObjectsAndKeys:@"text/plain",kSKPSMTPPartContentTypeKey,@"Some text to include in body",kSKPSMTPPartMessageKey,@"8bit",kSKPSMTPPartContentTransferEncodingKey,nil];
    //Logic for attach file.
//  NSDictionary *vcfPart = [NSDictionary dictionaryWithObjectsAndKeys:@"text/directory;\r\n\tx-unix-mode=0644;\r\n\tname=\"sample.pdf\"",kSKPSMTPPartContentTypeKey,@"attachment;\r\n\tfilename=\"sample.pdf\"",kSKPSMTPPartContentDispositionKey,[dataObj encodeBase64ForData],kSKPSMTPPartMessageKey,@"base64",kSKPSMTPPartContentTransferEncodingKey,nil];
//    NSLog(@"%@",vcfPart);
//  testMsg.parts = [NSArray arrayWithObjects:plainPart,vcfPart,nil];
//    testMsg.parts = [NSArray arrayWithObjects:plainPart,vcfPart,nil];
    testMsg.parts = [NSArray arrayWithObjects:plainPart,nil];
    [testMsg send];
}
  • If message/mail transfer successfully then this method to call.
1
2
3
4
-(void)messageSent:(SKPSMTPMessage *)message{
    [message release];
    NSLog(@"delegate - message sent");
}
  • If message/mail transfer unsuccessfully then this method to call.
1
2
3
4
5
6
7
8
-(void)messageFailed:(SKPSMTPMessage *)message error:(NSError *)error{
    [message release];
    // open an alert with just an OK button
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Unable to send email" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
    [alert show];
    [alert release];
    NSLog(@"delegate - error(%d): %@", [error code], [error localizedDescription]);
}

4) Following files copy/paste into your project.

neccesary_files

 

5) Run the application and insert the sender email address into text box.

Output

 

Grab the sample code from here.

Thanks & Best Regards,
Nimit Parekh

Advertisements