We write letters from the iOS application

  • Tutorial
Problem

I want to send a letter from the application for iOS without exiting the application (do not use mailto URL) and without opening an additional screen (do not use MFMailComposeViewController).

Solutions

Expensive: to raise a web service on your server that will be engaged in mailing letters, from the application to access it.
Cheap: use the SMTP client inside your application.

Problem

Need to write your SMTP client


Decision

Easy-to-use SMTP client for iOS - SKPSMTPmessage

Let's see how to send messages using it - we will create a simple application with a field for entering the text of the letter and the "Send" button.

Go to Xcode, create a new project View-based application for the iPhone. Let's call it EmailSender ( source code ).



We make a checkout of the SKPSMTPmessage sources
svn checkout skpsmtpmessage.googlecode.com/svn/trunk skpsmtpmessage-read-only
and copy the following files from there (you can simply drag and drop files from the Finder into the project tree in Xcode):
Base64Transcoder.h
Base64Transcoder.m
HSK_CFUtilities.h
HSK_CFUtilities.m
NSData + Base64Addition
NSData + Base64Additions.m
NSStream + SKPSMTPExtensions.h
NSStream + SKPSMTPExtensions.m
SKPSMTPMessage.h
SKPSMTPMessage.m





We also need to connect to the CFNetwork.framework project.





We need a field for the message text and a button to send the letter.
Let's edit our EmailSenderViewController.h file
#import 
 
@interface EmailSenderViewController: UIViewController {
 
    IBOutlet UITextView * txtMessage;
}
 
- (IBAction) sendEmail;
 
@end

Add to EmailSenderViewController.xib TextView (associate with txtMessage) and Button (hang sendEmail by clicking). We


implement the function of sending letters to EmailSenderViewController.m
- (IBAction) sendEmail
{
    SKPSMTPMessage * testMsg = [[SKPSMTPMessage alloc] init];
 
    testMsg.fromEmail = @ "your.sender@gmail.com"; // address from whom we send the letter
    testMsg.toEmail = @ "your.receiver@gmail.com"; // address where we send the letter
    testMsg.relayHost = @ "smtp.gmail.com"; // smtp server that you use
    testMsg.requiresAuth = YES; // whether authentication is
    required testMsg.login = @ "your.login@gmail.com"; // login to smtp servers
    testMsg.pass = @ "yourpassword"; // password for smtp server
    testMsg.subject = @ "Mail from habr"; // subject
    line testMsg.bccEmail = @ "";
    testMsg.wantsSecure = YES; // smtp.gmail.
 
 
 
    NSDictionary * plainPart = [NSDictionary dictionaryWithObjectsAndKeys: @ "text / plain",
                                                                         kSKPSMTPPartContentTypeKey,
                                                                         txtMessage.text,
                                                                         kSKPSMTPPartMessageKey,
                                                                         @ "8bit",
                                                                         kSKPSMTPPartContentTransferEncodingKey,
                                                                         n
 
    testMsg.parts = [NSArray arrayWithObjects: plainPart, nil];
 
 
    [testMsg send];
}
 


And do not forget to add the connection SKPSMTPMessage.h
#import "EmailSenderViewController.h"
#import "SKPSMTPMessage.h"
 

I advise you to create a separate gmail box for your application and use it to send letters (and your.sender = your.loging), as shown in the example. But, of course, you can use any other smtp server.

And finally, I’ll show you how to make our TextView a bit prettier (round corners).

We connect QuartzCore.h
#import "EmailSenderViewController.h"
#import "SKPSMTPMessage.h"
#import 

And in ViewDidLoad we add
- (void) viewDidLoad
{
    txtMessage.clipsToBounds = YES;
    txtMessage.layer.cornerRadius = 10.0f;
    [super viewDidLoad];
}


The source code of the sample application can be downloaded here.
Write letters! :)

Also popular now: