如何通过代码发送邮件而无需在目标c中打开MailComposer或新视图

时间:2016-06-20 13:05:14

标签: ios objective-c xib

-(IBAction)addbtnClick:(id)sender
{
    if([MFMailComposeViewController canSendMail]) {
        MFMailComposeViewController *mailCont = [[MFMailComposeViewController alloc] init];
          mailCont.mailComposeDelegate = self;

        [mailCont setSubject:@"yo!"];
        [mailCont setToRecipients:@[@"address@example.com"]];
       [mailCont setMessageBody:@"Don't ever want to give you up" isHTML:YES];

        [self presentViewController:mailCont animated:YES completion:nil];

    }
}
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
    //handle any error
    [controller dismissViewControllerAnimated:YES completion:nil];
}

我有这样的代码可以使用。但我不想打开MFMailComposer。我只想直接点击按钮发送邮件。这段代码打开MFMailComposer。我不想打开它。请帮助。

1 个答案:

答案 0 :(得分:1)

检查以下链接: -

SKPSMTPMessage

这个用于在后台发送邮件而不进行任何用户交互......

使用下面的代码来使用此库...

SKPSMTPMessage *emailMessage = [[SKPSMTPMessage alloc]init];
emailMessage.fromEmail = @"SENDER MAIL ID";
emailMessage.toEmail = @"RECEIPENT MAIL ID";
emailMessage.relayHost = @"smtp.gmail.com";
emailMessage.requiresAuth = YES;

您的电子邮件的登录凭证发送电子邮件

emailMessage.login = @"YOUR EMAIL ID";
emailMessage.pass = @"PASSWORD OF YOUR MAIL ACCOUNT";
emailMessage.subject = @"YOUR SUBJECT TO SEND MAIL";
emailMessage.wantsSecure = YES;
emailMessage.delegate = self;

NSString *password = @"PASSWORD";

NSString *message = [NSString stringWithFormat:@"Your password is : %@", password];

NSDictionary *plainPart = [NSDictionary dictionaryWithObjectsAndKeys:@"text/plain", kSKPSMTPPartContentTypeKey, message, kSKPSMTPPartMessageKey, @"8bit" , kSKPSMTPPartContentTransferEncodingKey, nil];

[emailMessage setParts:[NSArray arrayWithObjects:plainPart, nil]];
[emailMessage send];

希望这能帮到你......

您必须提供您要发送邮件的公司电子邮件的登录电子邮件和密码...

emailMessage.login = @"YOUR EMAIL ID";
emailMessage.pass = @"PASSWORD OF YOUR MAIL ACCOUNT";