gpt4 book ai didi

ios 使用 mailcore 发送电子邮件不起作用

转载 作者:行者123 更新时间:2023-11-28 17:54:42 28 4
gpt4 key购买 nike

我正在尝试使用 MailCore 发送电子邮件,但未发送电子邮件。如果我做错了什么,请提出建议。发送电子邮件的代码如下:

-(void) sendMail{
NSLog(@"entered");
CTCoreMessage *testMsg = [[CTCoreMessage alloc] init];
[testMsg setTo:[NSSet setWithObject:[CTCoreAddress addressWithName:@"recipients name" email:@"recipient@gmail.com"]]];
[testMsg setFrom:[NSSet setWithObject:[CTCoreAddress addressWithName:@"sender name(me)" email:@"my_e_mail@google.com"]]];
[testMsg setBody:@"This is a test message!"];
[testMsg setSubject:@"This is a subject"];
NSLog(@"Init values");
NSError *error;
BOOL success = [CTSMTPConnection sendMessage:testMsg
server:@"smtp.gmail.com"
username:@"my_e_mail"
password:@"pwd"
port:587
connectionType:CTSMTPConnectionTypeStartTLS
useAuth:YES
error:&error];
NSLog(@"Success is %c", success);
if (!success) {
// Present the error
NSLog(@"Mail not sent");
}
}

最佳答案

Here it's the working solution..!!

let smtpSession = MCOSMTPSession()
smtpSession.hostname = "smtp.gmail.com"
smtpSession.username = "XXX@gmail.com"
smtpSession.password = "xxxxxxx"
smtpSession.port = 465 // Gmail port number
smtpSession.authType = MCOAuthType.xoAuth2
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: "Vishnu", mailbox: senderEMail) as Any]
builder.header.from = MCOAddress(displayName: SingletonClass.sharedInstance.userName, mailbox: SingletonClass.sharedInstance.userEmail)
builder.header.subject = "MyMessage"
builder.htmlBody = "Hey Buddy, this is a test message!"

let rfc822Data = builder.data()
let sendOperation = smtpSession.sendOperation(with: rfc822Data!)
sendOperation?.start { (error) -> Void in
if (error != nil) {
print("Error sending email: \(error?.localizedDescription)")
NSLog("Error sending email: \(error?.localizedDescription)")
} else {
print("Successfully sent email!")
NSLog("Successfully sent email!")
}
}

关于ios 使用 mailcore 发送电子邮件不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16181622/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com