आईओएस एसडीके ने अंतर्निहित एपीआई का उपयोग करके ईमेल भेजने में वास्तव में आसान बना दिया है। कोड की कुछ पंक्तियों के साथ, आप एक ही ईमेल इंटरफ़ेस को स्टॉक मेल ऐप के रूप में लॉन्च कर सकते हैं जो आपको एक ईमेल लिखने देता है। आप मेल संगीतकार फॉर्म पॉप अप कर सकते हैं, संदेश लिख सकते हैं और MFMailComposeViewController क्लास का उपयोग करके सादे मेल या फ़ाइल संलग्न मेल भेज सकते हैं। अधिक जानकारी के लिए: अपने आईओएस ऐप से ई-मेल भेजना
लेकिन, इस खंड में जो मैं समझाऊंगा वह मेल संगीतकार शीट यानी बिना ईमेल भेजने के बारे में है। पृष्ठभूमि में ईमेल भेजना। इस सुविधा के लिए, हम आईओएस देशी एमएफमेल कॉम्पोज़र क्लास का उपयोग नहीं कर सकते क्योंकि यह हमें पृष्ठभूमि में ईमेल भेजने की इजाजत नहीं देता है, इसके बजाय यह मेल संगीतकार दृश्य को पॉप अप करता है जहां से उपयोगकर्ता को "भेजें" बटन टैप करना होता है, इसलिए इस सेक्शन में मैं जा रहा हूं पृष्ठभूमि में ईमेल भेजने के लिए SKPSMTPMessage लाइब्रेरी का उपयोग करें, हालांकि इस विधि पर ईमेल खाते को हार्डकोड किया जाना चाहिए।
सीमाएं:
प्रेषक/रिसीवर ईमेल पता hardcoded किया जाना है या आप कुछ अपने अनुप्रयोग जहां उपयोगकर्ता इनपुट प्रेषक/रिसीवर ईमेल पते में प्रपत्र पॉप अप का उपयोग कर इसे हड़पने के लिए की है। इसके अलावा, प्रेषक खाता प्रमाण-पत्रों को भी हार्डकोड किया जाना चाहिए क्योंकि डिवाइस डिवाइस सेटिंग्स से इसे पकड़ने का कोई तरीका नहीं है।
विधि:
- आयात CFNetwork.framework अपनी परियोजना के लिए।
- #import शामिल करें "SKPSMTPMessage.h" #import "NSData + Base64Additions.h" // Base64 एन्कोडिंग के लिए
- अपने ViewController को शामिल करें
- डाउनलोड SKPSMTPMessage पुस्तकालय से
https://github.com/jetseven/skpsmtpmessage
खींचें और छोड़ें "SMTPLibrary" फ़ोल्डर जिसे आपने अपनी प्रोजेक्ट में डाउनलोड किया है।
आगे बढ़ने से पहले, आपको पता है कि मैं इस उदाहरण के लिए कोड में प्रेषक/रिसीवर ईमेल पता और प्रेषक पासवर्ड का उपयोग कर रहा हूं।लेकिन, आप उपयोगकर्ता से इस क्रेडेंशियल्स को पकड़ सकते हैं, जिससे उन्हें किसी प्रकार के रूपों में इनपुट करने की अनुमति मिलती है (UIViews का उपयोग करके)।
-(void) sendEmailInBackground {
NSLog(@"Start Sending");
SKPSMTPMessage *emailMessage = [[SKPSMTPMessage alloc] init];
emailMessage.fromEmail = @"[email protected]"; //sender email address
emailMessage.toEmail = @"[email protected]"; //receiver email address
emailMessage.relayHost = @"smtp.gmail.com";
//emailMessage.ccEmail [email protected]"your cc address";
//emailMessage.bccEmail [email protected]"your bcc address";
emailMessage.requiresAuth = YES;
emailMessage.login = @"[email protected]"; //sender email address
emailMessage.pass = @"Passwxxxx"; //sender email password
emailMessage.subject [email protected]"@"email subject header message";
emailMessage.wantsSecure = YES;
emailMessage.delegate = self; // you must include <SKPSMTPMessageDelegate> to your class
NSString *messageBody = @"your email body message";
//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...
}
अब, से निपटने के प्रतिनिधि विधि:
// सफलता पर
-(void)messageSent:(SKPSMTPMessage *)message{
NSLog(@"delegate - message sent");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Message sent." message:nil delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[alert show];
}
// विफलता
-(void)messageFailed:(SKPSMTPMessage *)message error:(NSError *)error{
// open an alert with just an OK button
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error!" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[alert show];
NSLog(@"delegate - error(%d): %@", [error code], [error localizedDescription]);
}
ठीक है पर, सभी कोडिंग की ओर से thats । उम्मीद है कि यह ट्यूटोरियल आपके लिए उपयोगी हो सकता है guyz
क्या आप कृपया अपने उत्तर पर अधिक जानकारी दे सकते हैं? इस पर कोई नमूने उपलब्ध है? मैं मेल भेजने में भी संघर्ष कर रहा हूं। धन्यवाद। – Gopinath
जैसा कि मैंने कहा है, उपयोगकर्ताओं की सहमति के बिना आप आईओएस उपकरणों से ईमेल भेज सकते हैं। तो आप और क्या चाहते हैं? –
हाँ मैं समझ गया हूँ। धन्यवाद। – Gopinath