Xcode 4 / iOS - 从我的应用程序内部使用 SMTP 发送电子邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7087199/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Xcode 4 / iOS - Send an email using SMTP from inside my app
提问by Alex Godbehere
I've been looking around for a framework to simply allow me to send an email from inside my app. I have tried MailCore, Pantomime and SKPSMTP all with no luck. I can't get them to compile in Xcode, so I presumed they were outdated. Is there any way I can do this? If so, how? Thanks.
我一直在寻找一个框架来简单地允许我从我的应用程序内部发送电子邮件。我尝试过 MailCore、Pantomime 和 SKPSMTP,但都没有成功。我无法让它们在 Xcode 中编译,所以我认为它们已经过时了。有什么办法可以做到这一点吗?如果是这样,如何?谢谢。
回答by Srikar Appalaraju
You can easily send emails from your iOS device. No need to implement SMTP and all. Best thing about using inbuilt emailing facilities in iOS is it gives you access to the address book! So it auto-completes names, email addresses. Yaaiiii!!
您可以轻松地从 iOS 设备发送电子邮件。无需实施 SMTP 等。在 iOS 中使用内置电子邮件功能的最大好处是它可以让您访问地址簿!因此它会自动完成姓名、电子邮件地址。啊啊啊!!
Include, AddressBook
,AddressBookUI
and MessageUI
frameworks and code something like this. Note you can even choose to send content as HTML too!
包括AddressBook
,AddressBookUI
和MessageUI
框架和代码这样的事情。请注意,您甚至可以选择将内容作为 HTML 发送!
#import <MessageUI/MessageUI.h>
#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>
MFMailComposeViewController *mailComposer;
mailComposer = [[MFMailComposeViewController alloc] init];
mailComposer.mailComposeDelegate = self;
[mailComposer setModalPresentationStyle:UIModalPresentationFormSheet];
[mailComposer setSubject:@"your custom subject"];
[mailComposer setMessageBody:@"your custom body content" isHTML:NO];
[self presentModalViewController:mailComposer animated:YES];
[mailComposer release];
For the sake of completeness, I have to write this selector to dismiss the email window if the user presses cancel
or send
-
为了完整起见,如果用户按下cancel
或send
-我必须编写此选择器以关闭电子邮件窗口
- (void)mailComposeController:(MFMailComposeViewController*)controller
didFinishWithResult:(MFMailComposeResult)result
error:(NSError*)error
{
if(error) NSLog(@"ERROR - mailComposeController: %@", [error localizedDescription]);
[self dismissModalViewControllerAnimated:YES];
return;
}
Happy coding...
快乐编码...
回答by Colin
It should be noted that MFMailComposeViewController
has a method called canSendMail. If you don't check this before presenting a MFMailComposeViewController
on a device that doesn't have an email account, you'll get a SIGABRT.
需要注意的是,MFMailComposeViewController
有一个方法叫做canSendMail。如果您在没有MFMailComposeViewController
电子邮件帐户的设备上展示 a 之前不检查此项,您将收到 SIGABRT。
It's easy to miss this when testing on the device or the simulator since you'll probably have an email account on your Mac and your iPad.
在设备或模拟器上进行测试时很容易错过这一点,因为您的 Mac 和 iPad 上可能有一个电子邮件帐户。
回答by Ian L
SKPSMTPMessagestill works fine for sending emails without the need for a UI .
SKPSMTPMessage在不需要 UI 的情况下仍然可以很好地发送电子邮件。
Make sure you add a reference to the CFNetwork.framework
in your project. Otherwise you will get build errors.
确保CFNetwork.framework
在项目中添加对 的引用。否则你会得到构建错误。
回答by SplinterReality
I would imagine the Apple Approved way of doing this would be to send the data to a server via HTTP Post, and have the server generate the mail for you. I've seen others asking similar questions to this, and the answer is that if you send it from the device, you really need to prompt the user.
我想 Apple Approved 的做法是通过 HTTP Post 将数据发送到服务器,然后让服务器为您生成邮件。我看到其他人提出了类似的问题,答案是如果你从设备发送它,你真的需要提示用户。
I can even tell you why this is: Imagine an application that could send itself to everyone in your address book without the your confirmation, telling them that you just installed application X, and they should too. Even if well intentioned, this could quickly create a huge SMTP storm, and in essence this would be the "I love you" virus.
我什至可以告诉你这是为什么:想象一个应用程序,它可以在没有你确认的情况下将自己发送给你地址簿中的每个人,告诉他们你刚刚安装了应用程序 X,他们也应该这样做。即使是出于善意,这也可能很快造成巨大的 SMTP 风暴,本质上这将是“我爱你”病毒。
That was enough of a strain on the public internet, but on wireless carriers, could quickly cause enough overload to block cel service.
这对公共互联网来说已经足够了,但对无线运营商来说,可能很快就会造成足够的过载来阻止 cel 服务。
Conclusion: Either use the ComposeViewController as @Srikar suggests, or else POST the data to your server, and send it from there.
结论:要么按照@Srikar 的建议使用 ComposeViewController,要么将数据 POST 到您的服务器,然后从那里发送。