在 Swift 中发送 Mailcore2 普通电子邮件
Sending Mailcore2 Plain Emails in Swift
我最近将 MailCore2 合并到我的 Objective-C 项目中,它运行良好。现在,我正在将应用程序中的代码转换为 Swift。我已成功将 MailCore2 API 导入到我的 swift 项目中,但我没有看到有关如何打开将工作 Objective-C 代码转换为 Swift 代码:
MCOSMTPSession *smtpSession = [[MCOSMTPSession alloc] init];
smtpSession.hostname = @"smtp.gmail.com";
smtpSession.port = 465;
smtpSession.username = @"matt@gmail.com";
smtpSession.password = @"password";
smtpSession.authType = MCOAuthTypeSASLPlain;
smtpSession.connectionType = MCOConnectionTypeTLS;
MCOMessageBuilder *builder = [[MCOMessageBuilder alloc] init];
MCOAddress *from = [MCOAddress addressWithDisplayName:@"Matt R"
mailbox:@"matt@gmail.com"];
MCOAddress *to = [MCOAddress addressWithDisplayName:nil
mailbox:@"hoa@gmail.com"];
[[builder header] setFrom:from];
[[builder header] setTo:@[to]];
[[builder header] setSubject:@"My message"];
[builder setHTMLBody:@"This is a test message!"];
NSData * rfc822Data = [builder data];
MCOSMTPSendOperation *sendOperation =
[smtpSession sendOperationWithData:rfc822Data];
[sendOperation start:^(NSError *error) {
if(error) {
NSLog(@"Error sending email: %@", error);
} else {
NSLog(@"Successfully sent email!");
}
}];
有谁知道如何使用 API 在 Swift 中成功发送电子邮件?提前感谢所有回复的人。
我是这样做的:
步骤1)导入mailcore2,我用的是cocoapods
pod 'mailcore2-ios'
步骤 2) 将 mailcore2 添加到您的桥接头:Project-Bridging-Header.h
#import <MailCore/MailCore.h>
步骤 3) 转换为 swift
var smtpSession = MCOSMTPSession()
smtpSession.hostname = "smtp.gmail.com"
smtpSession.username = "matt@gmail.com"
smtpSession.password = "xxxxxxxxxxxxxxxx"
smtpSession.port = 465
smtpSession.authType = MCOAuthType.SASLPlain
smtpSession.connectionType = MCOConnectionType.TLS
smtpSession.connectionLogger = {(connectionID, type, data) in
if data != nil {
if let string = NSString(data: data, encoding: NSUTF8StringEncoding){
NSLog("Connectionlogger: \(string)")
}
}
}
var builder = MCOMessageBuilder()
builder.header.to = [MCOAddress(displayName: "Rool", mailbox: "itsrool@gmail.com")]
builder.header.from = MCOAddress(displayName: "Matt R", mailbox: "matt@gmail.com")
builder.header.subject = "My message"
builder.htmlBody = "Yo Rool, this is a test message!"
let rfc822Data = builder.data()
let sendOperation = smtpSession.sendOperationWithData(rfc822Data)
sendOperation.start { (error) -> Void in
if (error != nil) {
NSLog("Error sending email: \(error)")
} else {
NSLog("Successfully sent email!")
}
}
备注
您的 google 帐户可能需要一个特殊的应用密码。参见 https://support.google.com/accounts/answer/185833
要在 swift 中将图片作为附件发送,只需添加:
var dataImage: NSData?
dataImage = UIImageJPEGRepresentation(image, 0.6)!
var attachment = MCOAttachment()
attachment.mimeType = "image/jpg"
attachment.filename = "image.jpg"
attachment.data = dataImage
builder.addAttachment(attachment)
对于Swift 3 只需复制此
let smtpSession = MCOSMTPSession()
smtpSession.hostname = "smtp.gmail.com"
smtpSession.username = "sanipcr7@gmail.com"
smtpSession.password = "xxxxxxx"
smtpSession.port = 465
smtpSession.authType = MCOAuthType.saslPlain
smtpSession.connectionType = MCOConnectionType.TLS
smtpSession.connectionLogger = {(connectionID, type, data) in
if data != nil {
if let string = NSString(data: data!, encoding: String.Encoding.utf8.rawValue){
NSLog("Connectionlogger: \(string)")
}
}
}
let builder = MCOMessageBuilder()
builder.header.to = [MCOAddress(displayName: "Rool", mailbox: "sanipcr7@gmail.com")]
builder.header.from = MCOAddress(displayName: "Matt R", mailbox: "matt@gmail.com")
builder.header.subject = "My message"
builder.htmlBody = "Yo Rool, this is a test message!"
let rfc822Data = builder.data()
let sendOperation = smtpSession.sendOperation(with: rfc822Data!)
sendOperation?.start { (error) -> Void in
if (error != nil) {
NSLog("Error sending email: \(error)")
} else {
NSLog("Successfully sent email!")
}
}
我会在答案下方的评论中添加这个,但有这么多。
接受的答案中的步骤特别有效,特别是使用 link 到您的 google 帐户的特殊应用程序密码。
但是@RoolPaap 使用的桥接头与我使用的不同。我用了 #include "Mailcore.h"
我关注了这个youtube video
#ifndef MyProjectName_Bridging_Header_h
#define MyProjectName_Bridging_Header_h
#include "Mailcore.h"
#endif /* MyProjectName_Bridging_Header_h */
我最近将 MailCore2 合并到我的 Objective-C 项目中,它运行良好。现在,我正在将应用程序中的代码转换为 Swift。我已成功将 MailCore2 API 导入到我的 swift 项目中,但我没有看到有关如何打开将工作 Objective-C 代码转换为 Swift 代码:
MCOSMTPSession *smtpSession = [[MCOSMTPSession alloc] init];
smtpSession.hostname = @"smtp.gmail.com";
smtpSession.port = 465;
smtpSession.username = @"matt@gmail.com";
smtpSession.password = @"password";
smtpSession.authType = MCOAuthTypeSASLPlain;
smtpSession.connectionType = MCOConnectionTypeTLS;
MCOMessageBuilder *builder = [[MCOMessageBuilder alloc] init];
MCOAddress *from = [MCOAddress addressWithDisplayName:@"Matt R"
mailbox:@"matt@gmail.com"];
MCOAddress *to = [MCOAddress addressWithDisplayName:nil
mailbox:@"hoa@gmail.com"];
[[builder header] setFrom:from];
[[builder header] setTo:@[to]];
[[builder header] setSubject:@"My message"];
[builder setHTMLBody:@"This is a test message!"];
NSData * rfc822Data = [builder data];
MCOSMTPSendOperation *sendOperation =
[smtpSession sendOperationWithData:rfc822Data];
[sendOperation start:^(NSError *error) {
if(error) {
NSLog(@"Error sending email: %@", error);
} else {
NSLog(@"Successfully sent email!");
}
}];
有谁知道如何使用 API 在 Swift 中成功发送电子邮件?提前感谢所有回复的人。
我是这样做的:
步骤1)导入mailcore2,我用的是cocoapods
pod 'mailcore2-ios'
步骤 2) 将 mailcore2 添加到您的桥接头:Project-Bridging-Header.h
#import <MailCore/MailCore.h>
步骤 3) 转换为 swift
var smtpSession = MCOSMTPSession()
smtpSession.hostname = "smtp.gmail.com"
smtpSession.username = "matt@gmail.com"
smtpSession.password = "xxxxxxxxxxxxxxxx"
smtpSession.port = 465
smtpSession.authType = MCOAuthType.SASLPlain
smtpSession.connectionType = MCOConnectionType.TLS
smtpSession.connectionLogger = {(connectionID, type, data) in
if data != nil {
if let string = NSString(data: data, encoding: NSUTF8StringEncoding){
NSLog("Connectionlogger: \(string)")
}
}
}
var builder = MCOMessageBuilder()
builder.header.to = [MCOAddress(displayName: "Rool", mailbox: "itsrool@gmail.com")]
builder.header.from = MCOAddress(displayName: "Matt R", mailbox: "matt@gmail.com")
builder.header.subject = "My message"
builder.htmlBody = "Yo Rool, this is a test message!"
let rfc822Data = builder.data()
let sendOperation = smtpSession.sendOperationWithData(rfc822Data)
sendOperation.start { (error) -> Void in
if (error != nil) {
NSLog("Error sending email: \(error)")
} else {
NSLog("Successfully sent email!")
}
}
备注 您的 google 帐户可能需要一个特殊的应用密码。参见 https://support.google.com/accounts/answer/185833
要在 swift 中将图片作为附件发送,只需添加:
var dataImage: NSData?
dataImage = UIImageJPEGRepresentation(image, 0.6)!
var attachment = MCOAttachment()
attachment.mimeType = "image/jpg"
attachment.filename = "image.jpg"
attachment.data = dataImage
builder.addAttachment(attachment)
对于Swift 3 只需复制此
let smtpSession = MCOSMTPSession()
smtpSession.hostname = "smtp.gmail.com"
smtpSession.username = "sanipcr7@gmail.com"
smtpSession.password = "xxxxxxx"
smtpSession.port = 465
smtpSession.authType = MCOAuthType.saslPlain
smtpSession.connectionType = MCOConnectionType.TLS
smtpSession.connectionLogger = {(connectionID, type, data) in
if data != nil {
if let string = NSString(data: data!, encoding: String.Encoding.utf8.rawValue){
NSLog("Connectionlogger: \(string)")
}
}
}
let builder = MCOMessageBuilder()
builder.header.to = [MCOAddress(displayName: "Rool", mailbox: "sanipcr7@gmail.com")]
builder.header.from = MCOAddress(displayName: "Matt R", mailbox: "matt@gmail.com")
builder.header.subject = "My message"
builder.htmlBody = "Yo Rool, this is a test message!"
let rfc822Data = builder.data()
let sendOperation = smtpSession.sendOperation(with: rfc822Data!)
sendOperation?.start { (error) -> Void in
if (error != nil) {
NSLog("Error sending email: \(error)")
} else {
NSLog("Successfully sent email!")
}
}
我会在答案下方的评论中添加这个,但有这么多。
接受的答案中的步骤特别有效,特别是使用 link 到您的 google 帐户的特殊应用程序密码。
但是@RoolPaap 使用的桥接头与我使用的不同。我用了 #include "Mailcore.h"
我关注了这个youtube video
#ifndef MyProjectName_Bridging_Header_h
#define MyProjectName_Bridging_Header_h
#include "Mailcore.h"
#endif /* MyProjectName_Bridging_Header_h */