使用Swift实现应用内电子邮件失败

我想用swift来实现应用内电子邮件。 当我点击button时,popup电子邮件窗口。 但是,我无法发送我的电子邮件。 而且,当我点击取消 – 删除草稿后,我不能回到原来的画面。

import UIkit import MessageUI class Information : UIViewController, MFMailComposeViewControllerDelegate{ var myMail: MFMailComposeViewController! @IBAction func sendReport(sender : AnyObject) { if(MFMailComposeViewController.canSendMail()){ myMail = MFMailComposeViewController() //myMail.mailComposeDelegate // set the subject myMail.setSubject("My report") //To recipients var toRecipients = ["lipeilin@gatech.edu"] myMail.setToRecipients(toRecipients) //CC recipients var ccRecipients = ["tzhang85@gatech.edu"] myMail.setCcRecipients(ccRecipients) //CC recipients var bccRecipients = ["tzhang85@gatech.edu"] myMail.setBccRecipients(ccRecipients) //Add some text to the message body var sentfrom = "Email sent from my app" myMail.setMessageBody(sentfrom, isHTML: true) //Include an attachment var image = UIImage(named: "Gimme.png") var imageData = UIImageJPEGRepresentation(image, 1.0) myMail.addAttachmentData(imageData, mimeType: "image/jped", fileName: "image") //Display the view controller self.presentViewController(myMail, animated: true, completion: nil) } else{ var alert = UIAlertController(title: "Alert", message: "Your device cannot send emails", preferredStyle: UIAlertControllerStyle.Alert) alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil)) self.presentViewController(alert, animated: true, completion: nil) } } func mailComposeController(controller: MFMailComposeViewController!, didFinishWithResult result: MFMailComposeResult, error: NSError!){ switch(result.value){ case MFMailComposeResultSent.value: println("Email sent") default: println("Whoops") } self.dismissViewControllerAnimated(true, completion: nil) } } 

由于您没有将当前视图控制器设置为mailComposeDelegatemyMail ,因此未调用mailComposeController:didFinishWithResult方法。 在初始化myMail ,请确保添加:

 myMail.mailComposeDelegate = self 

你会很好走

如果有人正在寻找一个非MFMailCompose选项,以下是我使用Gmail的SMTP服务器发送的内容。

  1. 下载这个回购的邮编: https : //github.com/jetseven/skpsmtpmessage
  2. 将SMTPLibrary下的文件拖放到XCode项目中
  3. 创build一个新的头文件 – MyApp-Briding-Header.h
  4. 用这个replace新的头文件:
 #import "Base64Transcoder.h" #import "HSK_CFUtilities.h" #import "NSData+Base64Additions.h" #import "NSStream+SKPSMTPExtensions.h" #import "SKPSMTPMessage.h" 
  1. 转到项目(目标>左侧的MyApp)/构build设置/ Swift编译器 – 代码生成
  2. Objective-C Briding Header – > Debug下添加头文件的path(即MyApp/MyApp-Bridging-Header.h
  3. 转到项目/构build阶段/编译源
  4. select所有.m文件,然后单击input。 键入-fno-objc-arc并回车。

  5. 使用此代码发送电子邮件:

 var mail = SKPSMTPMessage() mail.fromEmail = "fromemail@gmail.com" mail.toEmail = "tomail@gmail.com" mail.requiresAuth = true mail.login = "fromemail@gmail.com" mail.pass = "password" mail.subject = "test subject" mail.wantsSecure = true mail.relayHost = "smtp.gmail.com" mail.relayPorts = [587] var parts: NSDictionary = [ "kSKPSMTPPartContentTypeKey": "text/plain; charset=UTF-8", "kSKPSMTPPartMessageKey": "test message", ] mail.parts = [parts] mail.send() 

希望它可以帮助别人。 我不想使用MFMailCompose选项,因为我不想提示用户。

这是我如何用附加的PDF文档文件编写我的电子邮件

为了testing这个例子,你需要拖放一个名为“All_about_tax.pdf”的样本PDF

 @IBAction func sendEmail(sender: UIButton) { //Check to see the device can send email. if( MFMailComposeViewController.canSendMail() ) { print("Can send email.") let mailComposer = MFMailComposeViewController() mailComposer.mailComposeDelegate = self //Set to recipients mailComposer.setToRecipients(["your email id here"]) //Set the subject mailComposer.setSubject("Tax info document pdf") //set mail body mailComposer.setMessageBody("This is what they sound like.", isHTML: true) if let filePath = NSBundle.mainBundle().pathForResource("All_about_tax", ofType: "pdf") { print("File path loaded.") if let fileData = NSData(contentsOfFile: filePath) { print("File data loaded.") mailComposer.addAttachmentData(fileData, mimeType: "application/pdf", fileName: "All_about_tax.pdf") } } //this will compose and present mail to user self.presentViewController(mailComposer, animated: true, completion: nil) } else { print("email is not supported") } } func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) { self.dismissViewControllerAnimated(true, completion: nil) }