ईमेल में अटैचमेंट भेजना जानना बहुत महत्वपूर्ण है क्योंकि अधिकांश एप्लिकेशन में शेयरिंग फीचर होते हैं।
इस पोस्ट में हम देखेंगे कि Swift का उपयोग करके मेल में अटैचमेंट कैसे भेजें।
इसके लिए हम MFMailComposeViewController का उपयोग करेंगे, जो एक स्टैंडर्ड व्यू कंट्रोलर है जिसका इंटरफेस उपयोगकर्ता को ईमेल मैसेज प्रबंधित, संपादित और भेजने देता है।
चरण 1: नया Xcode प्रोजेक्ट बनाएं
Xcode खोलें → Single View Application → नाम "EmailAttachment" रखें।
चरण 2: स्टोरीबोर्ड में बटन जोड़ें
Main.storyboard खोलें और एक बटन जोड़ें, नाम "Send Mail" रखें।

चरण 3: बटन के लिए IBAction बनाएं
@IBAction func btnSendMail(_ sender: Any) { }
चरण 4: MessageUI इम्पोर्ट करें
import MessageUI
चरण 5: क्लास को MFMailComposeViewControllerDelegate से कन्फर्म करें
class ViewController: UIViewController, MFMailComposeViewControllerDelegate
चरण 6: अटैचमेंट फाइल प्रोजेक्ट में जोड़ें

चरण 7: btnSendMail में कोड लिखें
@IBAction func btnSendMail(_ sender: Any) {
if MFMailComposeViewController.canSendMail() {
let mail = MFMailComposeViewController()
mail.setToRecipients(["test@gmail.com"])
mail.setSubject("GREETING")
mail.setMessageBody("Welcome to Tutorials Point!", isHTML: true)
mail.mailComposeDelegate = self
// अटैचमेंट जोड़ें
if let filePath = Bundle.main.path(forResource: "sampleData", ofType: "json") {
if let data = NSData(contentsOfFile: filePath) {
mail.addAttachmentData(data as Data, mimeType: "application/json", fileName: "sampleData.json")
}
}
present(mail, animated: true)
} else {
print("Email cannot be sent")
}
}
चरण 8: डिलीगेट मेथड इम्प्लीमेंट करें
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
if let _ = error {
self.dismiss(animated: true, completion: nil)
}
switch result {
case .cancelled:
print("Cancelled")
break
case .sent:
print("Mail sent successfully")
break
case .failed:
print("Sending mail failed")
break
default:
break
}
controller.dismiss(animated: true, completion: nil)
}
चरण 9: वास्तविक डिवाइस पर चलाएं

पूरा कोड
import UIKit
import MessageUI
class ViewController: UIViewController, MFMailComposeViewControllerDelegate {
override func viewDidLoad() { }
@IBAction func btnSendMail(_ sender: Any) {
if MFMailComposeViewController.canSendMail() {
let mail = MFMailComposeViewController()
mail.setToRecipients(["test@gmail.com"])
mail.setSubject("GREETING")
mail.setMessageBody("Welcome to Tutorials Point!", isHTML: true)
mail.mailComposeDelegate = self
if let filePath = Bundle.main.path(forResource: "sampleData", ofType: "json") {
if let data = NSData(contentsOfFile: filePath) {
mail.addAttachmentData(data as Data, mimeType: "application/json", fileName: "sampleData.json")
}
}
present(mail, animated: true)
} else {
print("Email cannot be sent")
}
}
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
if let _ = error {
self.dismiss(animated: true, completion: nil)
}
switch result {
case .cancelled: print("Cancelled"); break
case .sent: print("Mail sent successfully"); break
case .failed: print("Sending mail failed"); break
default: break
}
controller.dismiss(animated: true, completion: nil)
}
}