2016-06-16 9 views
5

Mam problem z wysyłaniem poczty z mojej aplikacji osx swift. Aby wysłać pocztę użyłem poniższy kodwysyłanie wiadomości e-mail z aplikacji do szybkiego 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]) 
} 

} 

znalazłem źródło tworząc w ten link: https://www.veasoftware.com/posts/send-email-in-swift-xcode-62-os-x-1010-tutorial

Ale to nie działa.

Próbowałem też wysłać mail z terminalu po tej instrukcji:

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

mówi:

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

proszę mi pomóc.

Mogę ręcznie wysyłać pocztę z mojej aplikacji poczty mac, która jest skonfigurowana.

Używam

xcode 7.3, osx el captain and swift 2.2 

Odpowiedz

9

Działa to dla mnie:

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

Zastosowanie:

SendEmail.send() 
+0

bardzo dziękuję za pomoc. Ale to nie działa tutaj ... :(.... czy konieczne jest skonfigurowanie czegoś takiego jak postmail do pomyślnego uruchomienia? –

+0

Działa dla mnie bez dodatkowej konfiguracji. – 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"]) 
    } 
} 

Zastosowanie: SendEmail.send()

Powiązane problemy