import UIKit
import MessageUI
class ViewController : UIViewController , UINavigationControllerDelegate ,
MFMailComposeViewControllerDelegate {
override func viewDidLoad() {
super .viewDidLoad()
//首先要判断设备具不具备发送邮件功能
if MFMailComposeViewController .canSendMail(){
let controller = MFMailComposeViewController ()
//设置代理
controller.mailComposeDelegate = self
//设置主题
controller.setSubject( "我是邮件标题" )
//设置收件人
controller.setToRecipients([ "[email protected]" , "[email protected]" ])
//设置抄送人
controller.setCcRecipients([ "[email protected]" , "[email protected]" ])
//设置密送人
controller.setBccRecipients([ "[email protected]" , "[email protected]" ])
//添加图片附件
var path = NSBundle .mainBundle().pathForResource( "hangge.png" , ofType: "" )
var myData = NSData (contentsOfFile: path!)
controller.addAttachmentData(myData, mimeType: "image/png" , fileName: "swift.png" )
//设置邮件正文内容(支持html)
controller.setMessageBody( "我是邮件正文" , isHTML: false )
//打开界面
self .presentViewController(controller, animated: true , completion: nil )
} else {
println ( "本设备不能发送邮件" )
}
}
//发送邮件代理方法
func mailComposeController(controller: MFMailComposeViewController !,
didFinishWithResult result: MFMailComposeResult , error: NSError !) {
controller.dismissViewControllerAnimated( true , completion: nil )
switch result.value{
case MFMailComposeResultSent .value:
println ( "邮件已发送" )
case MFMailComposeResultCancelled .value:
println ( "邮件已取消" )
case MFMailComposeResultSaved .value:
println ( "邮件已保存" )
case MFMailComposeResultFailed .value:
println ( "邮件发送失败" )
default :
println ( "邮件没有发送" )
break
}
}
override func didReceiveMemoryWarning() {
super .didReceiveMemoryWarning()
}
}
|