Send image and text in email from within app

こ雲淡風輕ζ 提交于 2019-12-21 21:25:17

问题


How can I send an image along with text, which is in the form of tabular data, in an email from within my app?

Please help and make suggestions. Thanks.


回答1:


- (void)sendMailWithImage:(UIImage *)image
{
if([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController *mailController = [[MFMailComposeViewController alloc] init];
if(mailController!=nil) {
mailController.mailComposeDelegate = self;
NSData *imageData = UIImagePNGRepresentation(image);
[mailController addAttachmentData:imageData mimeType:@"image/png" fileName:@"MyImageName"];
[mailController setSubject:yourSubject];
[mailController setMessageBody:yourBody isHTML:NO];
[self presentModalViewController:mailController animated:YES];
[mailController release];
}
else
{
//Do something like show an alert
}
}

Hope this helps




回答2:


Look at the MessageComposer sample App. Basically you use addAttachmentData:mimeType:fileName:.

This is from the MessageComposer app:

NSString *path = [[NSBundle mainBundle] pathForResource:@"rainy" ofType:@"jpg"];
NSData *myData = [NSData dataWithContentsOfFile:path];
[picker addAttachmentData:myData mimeType:@"image/jpeg" fileName:@"rainy"];



回答3:


You can send image as attachment, use MFMailComposerController for sending mail.

-(void)displayComposerSheet 
{
    MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
    picker.mailComposeDelegate = self;
    [picker setSubject:@"Test Subject"];
    // Attach an image to the email
    NSString *path = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@",imageName] ofType:@"png"];
    NSData *myData = [NSData dataWithContentsOfFile:path];
    [picker setMessageBody:body isHTML:NO];
    if (picker != nil)  {
        [self presentModalViewController:picker animated:YES];
        [picker release];
    }
}



回答4:


You use the MFMailComposerController class to allow the user to compose and send the mail. You can attach images and other files using the addAttachmentData:mimeType:fileName: method, and the body of the message (plain text or HTML) using the setMessageBody:isHTML: method.

Note that there is no way currently to include images in the HTML using multipart/related, you would have to either use data: URIs (not supported by all clients) or images on an external server (also not supported by all clients, for privacy reasons). Or, of course, bypass Apple completely and send the mail via conversation with your own server.




回答5:


You can use MFMailComposeViewController from Apple to sent mail from iOS apps. Its official documentation is here. Its usage

  1. Add MessageUI.framework to your project
  2. Import necessary header files

       #import <MessageUI/MessageUI.h> 
       #import <MessageUI/MFMailComposeViewController.h>
    
  3. To sent mail, open MFMailComposerController

    if ([MFMailComposeViewController canSendMail]) { 
       MFMailComposeViewController *ctrller = [[MFMailComposeViewController alloc] init]; 
       ctrller.mailComposeDelegate = self; 
       [ctrller setSubject:@"Subject Goes Here."]; 
       [ctrller setMessageBody:@"Your message goes here." isHTML:NO]; 
       [self presentModalViewController:ctrller animated:YES]; 
       [ctrller release]; //if not using ARC
    } else { 
        NSLog(@Device is unable to send email in its current state.); 
    }
    
  4. If you want to attach data you can use addAttachmentData: method

    [ctrller addAttachmentData:YOUR_DATA_IN_NSDATA_FORMAT 
               mimeType:YOUR_MIME_TYPE 
               fileName:YOUR_ATTACHEMENT_FILENAME];
    


来源:https://stackoverflow.com/questions/5716384/send-image-and-text-in-email-from-within-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!