2016-06-16 18 views
5

Ho riscontrato problemi nell'invio della posta dalla mia applicazione osx swift. Per inviare la posta ho usato il codice qui sottoinvia e-mail dall'applicazione swift 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]) 
} 

} 

ho trovato formano l'origine questo link: https://www.veasoftware.com/posts/send-email-in-swift-xcode-62-os-x-1010-tutorial

Ma non funziona.

Ho anche cercato di inviare la posta da terminale seguendo queste istruzioni:

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

dice:

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

ti prego, aiutami.

Posso inviare manualmente la posta dalla mia mac mail app che è configurata.

Sto usando

xcode 7.3, osx el captain and swift 2.2 

risposta

9

questo funziona per me:

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

Usage:

SendEmail.send() 
+0

grazie mille per il vostro aiuto. Ma non funziona qui ... :(.... è necessario impostare qualcosa come postmail per farlo funzionare correttamente? –

+0

Funziona per me senza bisogno di ulteriori configurazioni. – 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"]) 
    } 
} 

Uso: SendEmail.send()