Macos 如何在mac应用程序的xcode上启动电子邮件

Macos 如何在mac应用程序的xcode上启动电子邮件,macos,xcode6,Macos,Xcode6,我正在开发一个需要电子邮件访问的mac应用程序 这是我在ios上试过的代码 -(IBAction)Contact us:(id)sender { //Email Subject NSString *emailTitle = @""; //Email Content NSString *messageBody = @""; //Email Address NSArray *toRecipients =[NSArray arrayWithObject:@"info@mandarin-espeak.c

我正在开发一个需要电子邮件访问的mac应用程序

这是我在ios上试过的代码

-(IBAction)Contact us:(id)sender {
//Email Subject
NSString *emailTitle = @"";
//Email Content
NSString *messageBody = @"";
//Email Address
NSArray *toRecipients =[NSArray arrayWithObject:@"info@mandarin-espeak.com"];

MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;
[mc setSubject:emailTitle];
[mc setMessageBody:messageBody isHTML:YES];
[mc setToRecipients:toRecipients];

}

如何在mac应用程序上打开MacOS中的默认电子邮件客户端?您可以轻松发送如下电子邮件:

class SendEmail: NSObject {
    static func send() {
        let service = NSSharingService(named: NSSharingServiceNameComposeEmail)!
        service.recipients = ["abc@dom.com"]
        service.subject = "Vea software"

        service.performWithItems(["This is an email for auto testing through code."])
    }
}
用法:

SendEmail.send()

我最终选择了AppleScript解决方案。这是一个一次性应用程序,不适用于商店,所以我关闭了沙箱。我还将
Privacy-AppleEvents发送使用说明
添加到
info.plist
中,并在
签名和功能下的
强化运行时
中选中
苹果事件

我的电子邮件代码应该不会太难写出我独特的东西:

func emailUser(_ thisUser:NSManagedObject) {

        let dictSIPResponse = checkSIPforAppIdentifier("com.microsoft.Outlook")
        if let bIsSIPEnabled = dictSIPResponse["isSIPEnabled"] as? Bool, let strSIPMessage =  dictSIPResponse["sipMessage"] as? String{
            if bIsSIPEnabled {
                if let thisWriter = thisUser as? Writer, let strEmailAddress = thisWriter.email {
                    let outlookScript = """
                            if application "Outlook" is running then
                                tell application "Outlook"
                                    set newMessage to make new outgoing message with properties {subject:"Hooray for automation"}
                                    make new recipient at newMessage with properties {email address:{name:"Steve Suranie", address:"steven.suranie@xandr.com"}}
                                    send newMessage
                                end tell
                            end if
                        """

                    var out: NSAppleEventDescriptor?

                    if let scriptObject = NSAppleScript(source: outlookScript) {
                        var errorDict: NSDictionary? = nil
                        out = scriptObject.executeAndReturnError(&errorDict)

                        if let error = errorDict {
                            print(error)
                        }
                    }

                }
            } else {
                print(strSIPMessage)
            }
        }

    }
在尝试使用AppleScript发送电子邮件之前,我检查以确保已设置权限:

func checkSIPforAppIdentifier(_ sipIdentifier:String) -> Dictionary<String, Any> {

        var dictSIPResponse = [String:Any]()
        var targetAppEventDescriptor = NSAppleEventDescriptor(bundleIdentifier: sipIdentifier)
        var status = AEDeterminePermissionToAutomateTarget(targetAppEventDescriptor.aeDesc, typeWildCard, typeWildCard, true);

        switch (status) {
                case -600: //procNotFound
                    dictSIPResponse["isSIPEnabled"] = false
                    dictSIPResponse["sipMessage"] = "Not running app with id \(sipIdentifier)"
                    break;

                case 0: // noErr
                    dictSIPResponse["isSIPEnabled"] = true
                    dictSIPResponse["sipMessage"] = "SIP check successfull for app with id \(sipIdentifier)"
                    break;

                case -1744: // errAEEventWouldRequireUserConsent
                    // This only appears if you send false for askUserIfNeeded
                    dictSIPResponse["isSIPEnabled"] = false
                    dictSIPResponse["sipMessage"] = "User consent required for app with id \(sipIdentifier)"
                    break;

                case -1743: //errAEEventNotPermitted

                    dictSIPResponse["isSIPEnabled"] = false
                    dictSIPResponse["sipMessage"] = "User didn't allow usage for app with id \(sipIdentifier)"

                    // Here you should present a dialog with a tutorial on how to activate it manually
                    // This can be something like
                    // Go to system preferences > security > privacy
                    // Choose automation and active [APPNAME] for [APPNAME]


                default:
                    break;
            }

        return dictSIPResponse

    }
func checkSIPforAppIdentifier(uSIPidentifier:String)->字典{
var dictSIPResponse=[String:Any]()
var targetAppEventDescriptor=NSAppleEventDescriptor(bundleIdentifier:sipIdentifier)
var状态=自动目标的AEDETERMINEPERMICESSIONTO(targetAppEventDescriptor.aeDesc,类型通配符,类型通配符,true);
开关(状态){
案例-600://procNotFound
dictSIPResponse[“IsisPenabled”]=false
dictSIPResponse[“sipMessage”]=“未运行id为\(sipIdentifier)的应用程序”
打破
案例0://noErr
dictSIPResponse[“IsisPenabled”]=真
dictSIPResponse[“sipMessage”]=“id为\(sipIdentifier)的应用的SIP检查成功”
打破
案例-1744://勘误表
//仅当您为askUserIfNeeded发送false时,此选项才会出现
dictSIPResponse[“IsisPenabled”]=false
dictSIPResponse[“sipMessage”]=“id为\(sipIdentifier)的应用程序需要用户同意”
打破
案例-1743://勘误表
dictSIPResponse[“IsisPenabled”]=false
dictSIPResponse[“sipMessage”]=“用户不允许使用id为\(sipIdentifier)的应用程序”
//在这里,您应该提供一个对话框,其中包含如何手动激活它的教程
//这可能是
//转到系统首选项>安全性>隐私
//为[APPNAME]选择自动和活动[APPNAME]
违约:
打破
}
返回dictsip响应
}

您可以在这里找到答案。。有没有一种不用打开邮件就能完成的方法?我想在后台发送电子邮件。苹果公司的新员工?苹果将尽可能地阻止你的软件在后台运行。电子邮件被认真对待:如果你想利用苹果内置的电子邮件发送服务,用户将永远拥有最终决定权,用户必须按下“发送”键。如果你想在后台自动发送它们,你可能需要在你的应用程序中实现客户端SMTP协议。根据你的使用情况,这可能是一个巨大的杀伤力,可能不符合苹果的指导方针,所以如果你走这条路,要小心。不,我已经为MacOSX和AppleScript编程了一段时间,我正在构建一个只在一台机器上运行的应用程序,所以我不需要担心任何版本的沙盒。我将运行一个AppleScript,从我的应用程序调用Outlook并发送电子邮件。我想我也可以命令行它,但这台机器没有设置为邮件服务器,我正在寻找一个快速的解决方案。谢谢,我得到了它的工作。也许我应该发布它来帮助其他人,并获得一些关于代码的建设性批评。