xcode – 从swift osx应用程序发送电子邮件

从我的osx swift应用程序发送邮件时遇到问题.要发送邮件我使用下面的代码
import Foundation
import Cocoa


class sendemail : NSObject,nssharingserviceDelegate{

func sendEmail()  throws
{
    print("enter email sending")
    let body = "This is an email for auto testing throug code."
    let shareItems = [body] as NSArray

    let service = nssharingservice(named: nssharingserviceNameComposeEmail)

    service?.delegate = self
    service?.recipients = ["[email protected]"]

    let subject = "Vea Software"
    service?.subject = subject

    service?.performWithItems(shareItems as [AnyObject])
}

}

我找到了此链接的源代码https://www.veasoftware.com/posts/send-email-in-swift-xcode-62-os-x-1010-tutorial

但它没有用.

我还尝试按照以下说明从终端发送邮件

http://www.developerfiles.com/how-to-send-emails-from-localhost-mac-os-x-el-capitan/

它说 :

postfix/postfix-script: fatal: the Postfix mail system is not running

请帮我.

我可以手动从我的mac邮件应用程序发送邮件.

我在用

xcode 7.3,osx el captain and swift 2.2

解决方法

这对我有用:
class SendEmail: NSObject {
    static func send() {
        let service = nssharingservice(named: nssharingserviceNameComposeEmail)!
        service.recipients = ["[email protected]"]
        service.subject = "Vea software"

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

用法

SendEmail.send()

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...