2016-06-16 19 views
5

Tôi đang gặp sự cố khi gửi thư từ ứng dụng nhanh chóng của mình. Để gửi thư tôi sử dụng mã dưới đâygửi email từ ứng dụng nhanh chóng của osx

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]) 
} 

} 

tôi đã tìm thấy nguồn gốc hình thành liên kết này: https://www.veasoftware.com/posts/send-email-in-swift-xcode-62-os-x-1010-tutorial

Nhưng nó không phải đang làm việc.

Tôi cũng đã cố gắng để gửi thư từ thiết bị đầu cuối theo hướng dẫn này:

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

nó nói:

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

hãy giúp tôi.

Tôi có thể gửi thư từ ứng dụng thư mac của mình theo cách thủ công được định cấu hình.

Tôi đang sử dụng

xcode 7.3, osx el captain and swift 2.2 

Trả lời

9

này làm việc cho tôi:

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."]) 
    } 
} 

Cách sử dụng:

SendEmail.send() 
+0

cảm ơn rất nhiều sự giúp đỡ của bạn. Nhưng nó không hoạt động ở đây ...: (.... là điều này cần thiết để thiết lập bất cứ điều gì như postmail để chạy thành công này? –

+0

Làm việc cho tôi mà không cần thiết lập thêm. – dawid

2

Swift 4:

class SendEmail: NSObject { 
static func send() { 
    let service = NSSharingService(named: NSSharingService.Name.composeEmail)! 
    service.recipients = ["[email protected]"] 
    service.subject = "Email Subject" 

    service.perform(withItems: ["Email Content"]) 
    } 
} 

Cách sử dụng: SendEmail.send()

Các vấn đề liên quan