当前位置: 首页>>代码示例>>C#>>正文


C# MailMessage.AddAttachment方法代码示例

本文整理汇总了C#中MailMessage.AddAttachment方法的典型用法代码示例。如果您正苦于以下问题:C# MailMessage.AddAttachment方法的具体用法?C# MailMessage.AddAttachment怎么用?C# MailMessage.AddAttachment使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在MailMessage的用法示例。


在下文中一共展示了MailMessage.AddAttachment方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Run

        public static void Run()
        {
            // ExStart:ManagingEmailAttachments
            // The path to the File directory.
            string dataDir = RunExamples.GetDataDir_Email();
         
            // Create an instance of MailMessage class
            MailMessage message = new MailMessage {From = "[email protected]"};
            message.To.Add("[email protected]");

            // Load an attachment
            Attachment attachment = new Attachment(dataDir + "1.txt");

            // Add Multiple Attachment in instance of MailMessage class and Save message to disk
            message.Attachments.Add(attachment);           
            message.AddAttachment(new Attachment(dataDir + "1.jpg"));
            message.AddAttachment(new Attachment(dataDir + "1.doc"));
            message.AddAttachment(new Attachment(dataDir + "1.rar"));
            message.AddAttachment(new Attachment(dataDir + "1.pdf"));
            message.Save(dataDir + "outputAttachments_out.msg", SaveOptions.DefaultMsgUnicode);
            // ExEnd:ManagingEmailAttachments
        }
开发者ID:aspose-email,项目名称:Aspose.Email-for-.NET,代码行数:22,代码来源:ManagingEmailAttachments.cs

示例2: Run

        public static void Run()
        {
            // ExStart:RemoveAttachments
            // The path to the File directory.
            string dataDir = RunExamples.GetDataDir_Email();
            string dstEmailRemoved = dataDir + "RemoveAttachments.msg";

            // Create an instance of MailMessage class
            MailMessage message = new MailMessage();
            message.From = "[email protected]";
            message.To.Add("[email protected]");

            // Load an attachment
            Attachment attachment = new Attachment(dataDir + "1.txt");

            // Add Multiple Attachment in instance of MailMessage class and Save message to disk
            message.Attachments.Add(attachment);
            message.AddAttachment(new Attachment(dataDir + "1.jpg"));
            message.AddAttachment(new Attachment(dataDir + "1.doc"));
            message.AddAttachment(new Attachment(dataDir + "1.rar"));
            message.AddAttachment(new Attachment(dataDir + "1.pdf"));

            // Remove attachment from your MailMessage and Save message to disk after removing a single attachment 
            message.Attachments.Remove(attachment);
            message.Save(dstEmailRemoved, SaveOptions.DefaultMsgUnicode);

            // Create a loop to display the no. of attachments present in email message
            foreach (Attachment getAttachment in message.Attachments)
            {
                // Save your attachments here and Display the the attachment file name
                getAttachment.Save(dataDir + "/RemoveAttachments/" + "attachment_out" + getAttachment.Name);
                Console.WriteLine(getAttachment.Name);
            }
            // ExEnd:RemoveAttachments
            Console.WriteLine(Environment.NewLine + "Attachments removed successfully from " + dstEmailRemoved);
        }
开发者ID:aspose-email,项目名称:Aspose.Email-for-.NET,代码行数:36,代码来源:RemoveAttachments.cs

示例3: Run

        public static void Run()
        {
            // The path to the documents directory.
            string dataDir = RunExamples.GetDataDir_SMTP();
            string dstEmail = dataDir + "outputAttachments.msg";
            string dstEmailRemoved = dataDir + "outputAttachmentRemoved.msg";

            //Create an instance of MailMessage class
            MailMessage message = new MailMessage();

            //From
            message.From = "[email protected]";

            //to whom
            message.To.Add("[email protected]");



            // 1.
            // Attaching an Attachment to an Email

            //Adding 1st attachment
            //Create an instance of Attachment class
            Attachment attachment;

            //Load an attachment
            attachment = new Attachment(dataDir + "1.txt");

            //Add attachment in instance of MailMessage class
            message.Attachments.Add(attachment);

            //Add 2nd Attachment
            message.AddAttachment(new Attachment(dataDir + "1.jpg"));

            //Add 3rd Attachment
            message.AddAttachment(new Attachment(dataDir + "1.doc"));

            //Add 4th Attachment
            message.AddAttachment(new Attachment(dataDir + "1.rar"));

            //Add 5th Attachment
            message.AddAttachment(new Attachment(dataDir + "1.pdf"));

            //Save message to disk
            message.Save(dstEmail, Aspose.Email.Mail.SaveOptions.DefaultMsgUnicode);



            // 2.
            // Removing an Attachment

            //Remove attachment from your MailMessage
            message.Attachments.Remove(attachment);

            //Save message to disk after removing a single attachment 
            message.Save(dstEmailRemoved, Aspose.Email.Mail.SaveOptions.DefaultMsgUnicode);



            // 3.
            // Display remaining attachment names from message and save attachments

            //Create a loop to display the no. of attachments present in email message
            foreach (Attachment atchmnt in message.Attachments)
            {

                //Save your attachments here
                atchmnt.Save(dataDir + "attachment_" + atchmnt.Name);

                //display the the attachment file name
                Console.WriteLine(atchmnt.Name);
            }

            Console.WriteLine(Environment.NewLine + "Attachments removed successfully from " + dstEmailRemoved);
        }
开发者ID:ruanzx,项目名称:Aspose_Email_NET,代码行数:75,代码来源:ManagingEmailAttachments.cs

示例4: CopyAttachments

        private void CopyAttachments(IMessage msg, MailMessage outMsg)
        {
            MAPI33.IMAPITable attachmentsTable = null;
            msg.GetAttachmentTable(0, out attachmentsTable);
            using (attachmentsTable) {
                MAPI33.MapiTypes.Value[,] rows;
                attachmentsTable.SetColumns(new Tags[] {Tags.PR_ATTACH_FILENAME, Tags.PR_ATTACH_CONTENT_ID, Tags.PR_ATTACH_MIME_TAG, Tags.PR_ATTACH_ENCODING, Tags.PR_ATTACH_LONG_PATHNAME, Tags.PR_ATTACH_SIZE, Tags.PR_ATTACH_NUM},
                    IMAPITable.FLAGS.Default);

                //Use OpenAttach to get an IAttachment

                int attachIdx = 0;
                for( ;attachmentsTable.QueryRows(1, 0, out rows) == Error.Success && rows.Length > 0; ) {
                    try {
                        attachIdx++;
                        String attachFilename, attachMimeTag, attachContentId;
                        uint attachNum;
                        IAttachment attachObj;
                        byte[] attachData;

                        attachNum = (uint)((MapiInt32)rows[0,6]).Value;
                        msg.OpenAttach(attachNum, IAttachment.IID, 0, out attachObj);
                        using (attachObj) {
                            if (rows[0,0] is MapiString) {
                                attachFilename = ((MapiString)rows[0,0]).Value;
                            } else {
                                attachFilename = String.Format("Attach{0}.dat", attachIdx);
                            }

                            if (rows[0, 1] is MapiString) {
                                attachContentId = ((MapiString)rows[0, 1]).Value;
                            } else {
                                attachContentId = null;
                            }

                            if (rows[0,2] is MapiString) {
                                attachMimeTag = ((MapiString)rows[0,2]).Value;
                            } else {
                                attachMimeTag = String.Format("application/x-octet-stream");
                            }

                            IStream attachDataStream;
                            IUnknown unk;
                            Error hr = attachObj.OpenProperty(Tags.PR_ATTACH_DATA_BIN, IStream.IID, 0, 0, out unk);
                            //OLE object attachments are PR_ATTACH_DATA_OBJ, so if BIN isn't found, try that
                            if (hr == Error.NotFound) {
                                hr = attachObj.OpenProperty(Tags.PR_ATTACH_DATA_OBJ, IStream.IID, 0, 0, out unk);
                            }

                            if (hr == Error.Success) {
                                attachDataStream = (IStream)unk;
                                unk.Dispose();

                                using (attachDataStream) {
                                    int bytesRead = 0;
                                    ArrayList streamContents = new ArrayList();
                                    byte[] buffer = new byte[1024];

                                    do {
                                        attachDataStream.Read(buffer, (uint)buffer.Length, out bytesRead);
                                        if (bytesRead == buffer.Length) {
                                            streamContents.AddRange(buffer);
                                        } else {
                                            for (int idx = 0; idx < (int)bytesRead; idx++) {
                                                streamContents.Add(buffer[idx]);
                                            }
                                        }
                                    } while (bytesRead > 0);

                                    attachData = (byte[])streamContents.ToArray(typeof(byte));
                                }
                            } else {
                                attachData = System.Text.Encoding.ASCII.GetBytes(String.Format("Error getting contents of attachment '{0}' due to error '{1}'", attachFilename, hr));
                            }

                            //Use a MemoryStream to stream the attachment data into the Attachment object
                            using (MemoryStream ms = new MemoryStream(attachData)) {
                                outMsg.AddAttachment(ms, attachFilename, attachMimeTag, attachContentId);
                            }
                        }
                    } catch (Exception e) {
                        System.Diagnostics.Debug.WriteLine(e.ToString());
                    }
                }
            }
        }
开发者ID:anelson,项目名称:mapi_to_maildir,代码行数:86,代码来源:MaildirWriter.cs

示例5: SendViaEmail

        public void SendViaEmail(bool BatchOrNo, string to = null, string Subject = null, string Body = null)
        {
            FormCard(BatchOrNo);

            MailMessage emailMessage = new MailMessage();

            emailMessage.UserName = "[email protected]";
            emailMessage.Password = ServiceManager.DecryptPasscode("secret");
            emailMessage.AccountType = MailMessage.accountType.MicrosoftAccount;

            if ((emailMessage.UserName.ToLower()).Contains("gmail"))
            {
                emailMessage.AccountType = MailMessage.accountType.Gmail;
            }
            else if ((emailMessage.UserName.ToLower()).Contains("hotmail"))
            {
                emailMessage.AccountType = MailMessage.accountType.MicrosoftAccount;
            }

            if (to != null)
            {
                emailMessage.From = App.User.Email;
                emailMessage.To = to;
                emailMessage.Subject = Subject;
                emailMessage.Body = Body;
            }
            else
            {
                emailMessage.From = App.User.Email;
                emailMessage.To = App.vCard.EmailAddress;
                emailMessage.Subject = App.vCard.EmailSubject;
                emailMessage.Body = App.vCard.EmailBody;
            }

            if (App.FavoritesList != null)
            {
                using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    IsolatedStorageFileStream save = new IsolatedStorageFileStream("contact.vcf", System.IO.FileMode.Create, store);
                    StreamWriter writer = new StreamWriter(save);
                    writer.Write(vCard);
                    writer.Close();
                }
            }

            emailMessage.AddAttachment("contact.vcf");
            emailMessage.Error += emailMessage_Error;
            emailMessage.MailSent += emailMessage_MailSent;
            emailMessage.Send();
        }
开发者ID:shegg77,项目名称:Contacts,代码行数:50,代码来源:VCard.cs


注:本文中的MailMessage.AddAttachment方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。