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

Posted

技术标签:

【中文标题】ios使用mailcore发送电子邮件不起作用【英文标题】:ios Sending e mails using mailcore not working 【发布时间】:2013-04-17 09:24:40 【问题描述】:

我正在尝试使用 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");
    

【问题讨论】:

【参考方案1】:

这是您可以使用的示例代码:

MCOSMTPSession *smtpSession = [[MCOSMTPSession alloc] init];
smtpSession.hostname = @"smtp.gmail.com";
smtpSession.port = 465;
smtpSession.username = USERNAME;
smtpSession.password = PASSWORD;
smtpSession.connectionType = MCOConnectionTypeTLS;

MCOMessageBuilder * builder = [[MCOMessageBuilder alloc] init];
[[builder header] setFrom:[MCOAddress addressWithDisplayName:nil mailbox:USERNAME]];
NSMutableArray *to = [[NSMutableArray alloc] init];
for(NSString *toAddress in RECIPIENTS) 
    MCOAddress *newAddress = [MCOAddress addressWithMailbox:toAddress];
    [to addObject:newAddress];

[[builder header] setTo:to];
NSMutableArray *cc = [[NSMutableArray alloc] init];
for(NSString *ccAddress in CC) 
    MCOAddress *newAddress = [MCOAddress addressWithMailbox:ccAddress];
    [cc addObject:newAddress];

[[builder header] setCc:cc];
NSMutableArray *bcc = [[NSMutableArray alloc] init];
for(NSString *bccAddress in BCC) 
    MCOAddress *newAddress = [MCOAddress addressWithMailbox:bccAddress];
    [bcc addObject:newAddress];

[[builder header] setBcc:bcc];
[[builder header] setSubject:SUBJECT];
[builder sethtmlBody:BODY];
NSData * rfc822Data = [builder data];

MCOSMTPSendOperation *sendOperation = [smtpSession sendOperationWithData:rfc822Data];
[sendOperation start:^(NSError *error) 
    if(error) 
        NSLog(@"%@ Error sending email:%@", USERNAME, error);
     else 
        NSLog(@"%@ Successfully sent email!", USERNAME);
    

您可以在此处找到此示例代码的参考 (Sample Code)。

【讨论】:

以上是关于ios使用mailcore发送电子邮件不起作用的主要内容,如果未能解决你的问题,请参考以下文章

使用 Mandrill 发送邮件。异步不起作用

使用 html Mailto 向多个收件人发送电子邮件:不起作用

使用 jquery 发送电子邮件不起作用:PHP

使用 AJAX 提交时发送电子邮件不起作用

使用 Python 从 Outlook 发送电子邮件不起作用

laravel - 发送邮件不起作用