Swift 发送邮件框架 Hedwig
Posted swift语言
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Swift 发送邮件框架 Hedwig相关的知识,希望对你有一定的参考价值。
Hedwig 是一个可以发送邮件到任何 SMTP 服务器的 Swift 跨平台框架。
发送文本邮件:
01 |
let hedwig = Hedwig(hostName: "smtp.example.com" , user: "foo@bar.com" , password: "password" ) |
03 |
text: "Across the great wall we can reach every corner in the world." , |
04 |
from: "onev@onevcat.com" , |
06 |
subject: "Hello World" |
09 |
hedwig.send(mail) { error in |
10 |
if error != nil { /* Error happened */ } |
发送html邮件:
01 |
let hedwig = Hedwig(hostName: "smtp.example.com" , user: "foo@bar.com" , password: "password" ) |
02 |
let attachement = Attachment(htmlContent: "<html><body><h1>Title</h1><p>Content</p></body></html>" ) |
04 |
text: "Fallback text" , |
05 |
from: "onev@onevcat.com" , |
08 |
attachments: [attachement] |
10 |
hedwig.send(mail) { error in |
11 |
if error != nil { /* Error happened */ } |
抄送和暗抄送:
01 |
let hedwig = Hedwig(hostName: "smtp.example.com" , user: "foo@bar.com" , password: "password" ) |
03 |
text: "Across the great wall we can reach every corner in the world." , |
04 |
from: "onev@onevcat.com" , |
06 |
cc: "Wei Wang <onev@onevcat.com>, tom@example.com" // Addresses will be parsed for you |
07 |
bcc: "My Group: onev@onevcat.com, foo@bar.com;" // Even with group syntax |
08 |
subject: "Hello World" |
10 |
hedwig.send(mail) { error in |
11 |
if error != nil { /* Error happened */ } |
SMTP设置:
02 |
hostName: "smtp.example.com" , |
05 |
port: 1234 , // Determined from secure layer by default |
06 |
secure: .plain, // .plain (Port 25) | .ssl (Port 465) | .tls (Port 587) (default) |
07 |
validation: . default , // You can set your own certificate/cipher/protocols |
08 |
domainName: "onevcat.com" , // Used when saying hello to STMP Server |
09 |
authMethods: [.plain, .login] // Default: [.plain, .cramMD5, .login, .xOauth2] |
发送图片和附件:
01 |
let imagePath = "/tmp/image.png" |
02 |
// You can create an attachment from a local file path. |
03 |
let imageAttachment = Attachment( |
06 |
// Add "Content-ID" if you need to embed this image to another attachment. |
07 |
additionalHeaders: [ "Content-ID" : "hedwig-image" ] |
09 |
let html = Attachment( |
10 |
htmlContent: "<html><body>A photo <img src=\"cid:hedwig-image\"/></body></html>" , |
11 |
// If imageAttachment only used embeded in HTML, I recommend to set it as related. |
12 |
related: [imageAttachment] |
15 |
// You can also create attachment from raw data. |
16 |
let data = "{\"key\": \"hello world\"}" .data(using: .utf8)! |
17 |
let json = Attachment( |
19 |
mime: "application/json" , |
21 |
inline: false // Send as standalone attachment. |
25 |
text: "Fallback text" , |
26 |
from: "onev@onevcat.com" , |
28 |
subject: "Check the photo and json file!" , |
29 |
attachments: [html, json] |
30 |
hedwig.send(mail) { error in |
31 |
if error != nil { /* Error happened */ } |
发送给多个收件人:
01 |
let mail1: Mail = //... |
02 |
let mail2: Mail = //... |
04 |
hedwig.send([mail1, mail2], |
05 |
progress: { (mail, error) in |
07 |
print( "\(mail) failed. Error: \(error)" ) |
10 |
completion: { (sent, failed) in |
12 |
print( "Sent mail: \(mail.messageId)" ) |
15 |
for (mail, error) in failed { |
16 |
print( "Mail \(mail.messageId) errored: \(error)" ) |
开源代码主页 ❤
文章来自:51swift.com
以上是关于Swift 发送邮件框架 Hedwig的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 Swift 代码 (macOS) 发送电子邮件背景
在 Swift 中发送 Mailcore2 普通电子邮件
从 swift osx 应用程序发送电子邮件
尝试使用 swift mailer、gmail smtp、php 发送邮件
尝试从应用内发送电子邮件但不起作用 - Swift (iOS)
发送没有GUI的电子邮件[关闭]