Вы не можете напрямую использовать MFMailComposeViewController
, для чего вам нужна внешняя библиотека или программа, необходимая нам для отправки почты с использованием SMTP
здесь. Я упоминаю gmail через отправку почты без взаимодействия пользователей.
Код для .h файла
#import <UIKit/UIKit.h>
#import "SKPSMTPMessage.h"
@interface mailTransferViewController : UIViewController<SKPSMTPMessageDelegate>{
IBOutlet UITextField *emailField;
}
- (IBAction)sendMessageInBack:(id)anObject;
@end
Код для .m файл
- (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];
}
Нужно ли использовать правильный адрес электронной почты пользователя? – Droppy