Swift 3电子邮件表格无法发送

时间:2017-05-29 07:09:45

标签: ios swift3

我正在使用textfield和textview

进行表单

在我输入所有信息并单击提交按钮后,应用程序将关闭并跳转到我的viewcontroller.swift并显示如下照片 enter image description here

这是我的viewcontroller.swift

import UIKit
import MessageUI

class ContactViewController:UIViewController,MFMailComposeViewControllerDelegate,UITextViewDelegate,UITextFieldDelegate {

@IBOutlet weak var message: UITextView!
@IBOutlet weak var contactSubject: UITextField!
@IBOutlet weak var contactNumber: UITextField!
@IBOutlet weak var contactEmail: UITextField!
@IBOutlet weak var contactName: UITextField!
@IBOutlet weak var contactDes: UILabel!
@IBOutlet weak var contactTitle: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()

    contactTitle.text = "SEND US A MESSAGES"
    contactTitle.sizeToFit()
    contactDes.text = "Do you have anything in your mind to tell us?\nPlease don’t hesitate to get in touch to us via our contact form."
    contactDes.sizeToFit()

    //Contact Form

    contactName.delegate = self
    contactEmail.delegate = self
    contactNumber.delegate = self
    contactSubject.delegate = self
    message.delegate = self

    self.message.isUserInteractionEnabled = true
    self.message.isEditable = true

    let borderColor : UIColor = UIColor.init(red: 46.0/255.0, green: 49.0/255.0, blue: 146.0/255.0, alpha: 1)
    contactName.layer.borderWidth = 1
    contactName.layer.borderColor = borderColor.cgColor
    contactEmail.layer.borderWidth = 1
    contactEmail.layer.borderColor = borderColor.cgColor
    contactSubject.layer.borderWidth = 1
    contactSubject.layer.borderColor = borderColor.cgColor
    contactNumber.layer.borderWidth = 1
    contactNumber.layer.borderColor = borderColor.cgColor
    message.layer.borderWidth = 1
    message.layer.borderColor = borderColor.cgColor

}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {

    contactName.resignFirstResponder()
    contactEmail.resignFirstResponder()
    contactNumber.resignFirstResponder()
    contactSubject.resignFirstResponder()
    return true
}

@IBAction func submitbtn(_ sender: Any) {
    let mailVC = MFMailComposeViewController()
    mailVC.mailComposeDelegate = self
    mailVC.setSubject(contactSubject.text!)

    let email = contactEmail.text!.lowercased()
    let finalemail = email.trimmingCharacters(in: NSCharacterSet.whitespacesAndNewlines)

    let mailcontent = "Name :\(contactName.text!)\n\n Email:\(finalemail)\n\n Contact Numer :\(contactNumber.text!)\n\n Subject :\(contactSubject.text!)\n\n Message :\(message.text!)"

    mailVC.setMessageBody(mailcontent, isHTML: false)

    let toRecipent = "abc@example.com"

    mailVC.setToRecipients([toRecipent])

    self.present(mailVC, animated: true, completion: nil)
}


}

0 个答案:

没有答案