Code:
/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / Net / System / Net / Mail / MimeMultiPart.cs / 1305376 / MimeMultiPart.cs
using System; using System.IO; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Globalization; namespace System.Net.Mime { ////// Summary description for MimeMultiPart. /// internal class MimeMultiPart:MimeBasePart { Collectionparts; static int boundary; AsyncCallback mimePartSentCallback; internal MimeMultiPart(MimeMultiPartType type) { MimeMultiPartType = type; } internal MimeMultiPartType MimeMultiPartType { /* // Consider removing. get { return GetPartType(); } */ set { if (value > MimeMultiPartType.Related || value < MimeMultiPartType.Mixed) { throw new NotSupportedException(value.ToString()); } SetType(value); } } void SetType(MimeMultiPartType type) { ContentType.MediaType = "multipart" + "/" + type.ToString().ToLower(CultureInfo.InvariantCulture); ContentType.Boundary = GetNextBoundary(); } /* // Consider removing. MimeMultiPartType GetPartType() { switch (ContentType.MediaType) { case "multipart/alternative": return MimeMultiPartType.Alternative; case "multipart/mixed": return MimeMultiPartType.Mixed; case "multipart/parallel": return MimeMultiPartType.Parallel; case "multipart/related": return MimeMultiPartType.Related; } return MimeMultiPartType.Unknown; } */ internal Collection Parts { get{ if (parts == null) { parts = new Collection (); } return parts; } } internal void Complete(IAsyncResult result, Exception e){ //if we already completed and we got called again, //it mean's that there was an exception in the callback and we //should just rethrow it. MimePartContext context = (MimePartContext)result.AsyncState; if (context.completed) { throw e; } try{ context.outputStream.Close(); } catch(Exception ex){ if (e == null) { e = ex; } } context.completed = true; context.result.InvokeCallback(e); } internal void MimeWriterCloseCallback(IAsyncResult result) { if (result.CompletedSynchronously ) { return; } ((MimePartContext)result.AsyncState).completedSynchronously = false; try{ MimeWriterCloseCallbackHandler(result); } catch (Exception e) { Complete(result,e); } } void MimeWriterCloseCallbackHandler(IAsyncResult result) { MimePartContext context = (MimePartContext)result.AsyncState; ((MimeWriter)context.writer).EndClose(result); Complete(result,null); } internal void MimePartSentCallback(IAsyncResult result) { if (result.CompletedSynchronously ) { return; } ((MimePartContext)result.AsyncState).completedSynchronously = false; try{ MimePartSentCallbackHandler(result); } catch (Exception e) { Complete(result,e); } } void MimePartSentCallbackHandler(IAsyncResult result) { MimePartContext context = (MimePartContext)result.AsyncState; MimeBasePart part = (MimeBasePart)context.partsEnumerator.Current; part.EndSend(result); if (context.partsEnumerator.MoveNext()) { part = (MimeBasePart)context.partsEnumerator.Current; IAsyncResult sendResult = part.BeginSend(context.writer, mimePartSentCallback, context); if (sendResult.CompletedSynchronously) { MimePartSentCallbackHandler(sendResult); } return; } else { IAsyncResult closeResult = ((MimeWriter)context.writer).BeginClose(new AsyncCallback(MimeWriterCloseCallback), context); if (closeResult.CompletedSynchronously) { MimeWriterCloseCallbackHandler(closeResult); } } } internal void ContentStreamCallback(IAsyncResult result) { if (result.CompletedSynchronously ) { return; } ((MimePartContext)result.AsyncState).completedSynchronously = false; try{ ContentStreamCallbackHandler(result); } catch (Exception e) { Complete(result,e); } } void ContentStreamCallbackHandler(IAsyncResult result) { MimePartContext context = (MimePartContext)result.AsyncState; context.outputStream = context.writer.EndGetContentStream(result); context.writer = new MimeWriter(context.outputStream, ContentType.Boundary); if (context.partsEnumerator.MoveNext()) { MimeBasePart part = (MimeBasePart)context.partsEnumerator.Current; mimePartSentCallback = new AsyncCallback(MimePartSentCallback); IAsyncResult sendResult = part.BeginSend(context.writer, mimePartSentCallback, context); if (sendResult.CompletedSynchronously) { MimePartSentCallbackHandler(sendResult); } return; } else { IAsyncResult closeResult = ((MimeWriter)context.writer).BeginClose(new AsyncCallback(MimeWriterCloseCallback),context); if (closeResult.CompletedSynchronously) { MimeWriterCloseCallbackHandler(closeResult); } } } internal override IAsyncResult BeginSend(BaseWriter writer, AsyncCallback callback, object state) { writer.WriteHeaders(Headers); MimePartAsyncResult result = new MimePartAsyncResult(this, state, callback); MimePartContext context = new MimePartContext(writer, result, Parts.GetEnumerator()); IAsyncResult contentResult = writer.BeginGetContentStream(new AsyncCallback(ContentStreamCallback), context); if (contentResult.CompletedSynchronously) { ContentStreamCallbackHandler(contentResult); } return result; } internal class MimePartContext { internal MimePartContext(BaseWriter writer, LazyAsyncResult result, IEnumerator partsEnumerator) { this.writer = writer; this.result = result; this.partsEnumerator = partsEnumerator; } internal IEnumerator partsEnumerator; internal Stream outputStream; internal LazyAsyncResult result; internal BaseWriter writer; internal bool completed; internal bool completedSynchronously = true; } internal override void Send(BaseWriter writer) { writer.WriteHeaders(Headers); Stream outputStream = writer.GetContentStream(); MimeWriter mimeWriter = new MimeWriter(outputStream, ContentType.Boundary); foreach (MimeBasePart part in Parts) { part.Send(mimeWriter); } mimeWriter.Close(); outputStream.Close(); } internal string GetNextBoundary() { string boundaryString = "--boundary_" + boundary.ToString(CultureInfo.InvariantCulture)+"_"+Guid.NewGuid().ToString(null, CultureInfo.InvariantCulture); boundary++; return boundaryString; } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- securitycriticaldataformultiplegetandset.cs
- DataGridViewColumnCollectionEditor.cs
- DependencyPropertyValueSerializer.cs
- ColorAnimationBase.cs
- CodeAttributeArgumentCollection.cs
- DocumentSchemaValidator.cs
- TraceHandler.cs
- DocobjHost.cs
- StorageAssociationSetMapping.cs
- ZipFileInfo.cs
- HMAC.cs
- SmiMetaData.cs
- HttpModule.cs
- ToolboxBitmapAttribute.cs
- HasCopySemanticsAttribute.cs
- Registry.cs
- MDIControlStrip.cs
- LocalValueEnumerator.cs
- DataGridViewCellEventArgs.cs
- StylusButton.cs
- DoubleAnimation.cs
- StreamInfo.cs
- CallbackDebugElement.cs
- File.cs
- NamespaceInfo.cs
- ForEachAction.cs
- SimpleFileLog.cs
- EntityAdapter.cs
- EmptyControlCollection.cs
- WebPartConnectionsCancelVerb.cs
- EmissiveMaterial.cs
- __TransparentProxy.cs
- SessionStateModule.cs
- ViewGenerator.cs
- ParallelActivityDesigner.cs
- DrawingImage.cs
- SemaphoreSecurity.cs
- EmptyEnumerator.cs
- MDIControlStrip.cs
- TypedDataSetSchemaImporterExtensionFx35.cs
- SynchronizedInputAdaptor.cs
- NonceToken.cs
- XmlSchemaObjectCollection.cs
- KeyBinding.cs
- PackageProperties.cs
- ConnectionPoint.cs
- Codec.cs
- XpsFilter.cs
- AuthenticationManager.cs
- TextLine.cs
- DbConnectionOptions.cs
- EntityConnectionStringBuilderItem.cs
- GridItemPattern.cs
- BamlResourceDeserializer.cs
- XPathNode.cs
- UseAttributeSetsAction.cs
- StringDictionary.cs
- OraclePermission.cs
- TagMapInfo.cs
- Socket.cs
- TemplateContent.cs
- ApplicationDirectoryMembershipCondition.cs
- WebSysDescriptionAttribute.cs
- PlatformCulture.cs
- FormCollection.cs
- DragDeltaEventArgs.cs
- PixelFormatConverter.cs
- GuidTagList.cs
- ServicesSection.cs
- InternalTypeHelper.cs
- AttributeUsageAttribute.cs
- EmptyEnumerator.cs
- ClassGenerator.cs
- ComboBox.cs
- FixedDocumentPaginator.cs
- ParserOptions.cs
- ReversePositionQuery.cs
- CollectionViewGroup.cs
- AppSettingsReader.cs
- FocusWithinProperty.cs
- SubMenuStyleCollection.cs
- RuntimeConfigLKG.cs
- ManifestSignedXml.cs
- ProxyAttribute.cs
- TypeTypeConverter.cs
- TagElement.cs
- Grant.cs
- WindowVisualStateTracker.cs
- InProcStateClientManager.cs
- SchemaNotation.cs
- StringDictionary.cs
- BitmapEffectDrawingContextWalker.cs
- EditorZone.cs
- SortDescriptionCollection.cs
- DesignerTransactionCloseEvent.cs
- ClassDataContract.cs
- SafeFileMappingHandle.cs
- UnescapedXmlDiagnosticData.cs
- WaitingCursor.cs
- CookielessHelper.cs