我有一个关于iOS编程中MFMailComposeViewController的问题。我对这种语言还很陌生,我的目标是通过我正在创建的应用程序发送一封隐藏的电子邮件。我希望在用户不必按下弹出电子邮件对话框时出现的发送按钮的情况下发送电子邮件。如何在不显示对话框的情况下发送电子邮件?
以下是我的代码:
//Sending Mail
-(IBAction)sendemail:(id)sender
{
if ([MFMailComposeViewController canSendMail])
{
MFMailComposeViewController *mail = [[MFMailComposeViewController alloc] init];
mail.mailComposeDelegate = self;
[mail setSubject:@"Sample Subject"];
[mail setMessageBody:@"Here is some main text in the email!" isHTML:NO];
[mail setToRecipients:@[@"test@example.com"]];
[self presentViewController:mail animated:YES completion:NULL];
}
else
{
NSLog(@"This device cannot send email");
}
}
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
switch (result) {
case MFMailComposeResultSent:
NSLog(@"You sent the email.");
break;
case MFMailComposeResultSaved:
NSLog(@"You saved a draft of this email");
break;
case MFMailComposeResultCancelled:
NSLog(@"You cancelled sending this email.");
break;
case MFMailComposeResultFailed:
NSLog(@"Mail failed: An error occurred when trying to compose this email");
break;
default:
NSLog(@"An error occurred when trying to compose this email");
break;
}
[self dismissViewControllerAnimated:YES completion:NULL];
}
提前感谢所有回复的人。我真的很感激。
MFMailComposeViewController。。。。我的目标是发送一封隐藏的电子邮件
这些目标是相互矛盾的。如果您想从用户那里秘密发送电子邮件,则不要使用MFMailComposeViewController。你必须教你的应用程序SMTP,并使用低级网络自己发送电子邮件。(如果苹果发现你在这么做,我希望他们会立即将你的应用程序从商店中删除。)
Apple不允许您隐藏MFMailComposeViewController
。您需要使用web服务来发送电子邮件。
使用NSURLRequest
和NSURLConnection
向web服务器发送请求,web服务器接受表示要发送的电子邮件的JSON数据负载。然后,网络服务器将解析数据,进行任何必要的验证和处理,然后自行发送电子邮件或与第三方电子邮件服务交互。您也可以尝试切断中间人服务器,直接从应用程序与第三方电子邮件服务交互,但这种方法可能会面临一些挑战,即API令牌被烘焙到应用程序本身的安全性可能会使您的第三方帐户受到滥用。
Mailgun Objective-C SDK的示例代码:
Mailgun *mailgun = [Mailgun clientWithDomain:@"samples.mailgun.org" apiKey:@"key-3ax6xnjp29jd6fds4gc373sgvjxteol0"];
[mailgun sendMessageTo:@"Jay Baird <jay.baird@rackspace.com>"
from:@"Excited User <someone@sample.org>"
subject:@"Mailgun is awesome!"
body:@"A unicode snowman for you! ☃"];