Code:
/ Net / Net / 3.5.50727.3053 / DEVDIV / depot / DevDiv / releases / whidbey / netfxsp / ndp / fx / src / Net / System / Net / Mail / MimeMultiPart.cs / 1 / 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
{
Collection parts;
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;
}
}
catch {
if (e == null) {
e = new Exception(SR.GetString(SR.net_nonClsCompliantException));
}
}
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);
}
catch {
Complete(result, new Exception(SR.GetString(SR.net_nonClsCompliantException)));
}
}
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);
}
catch {
Complete(result, new Exception(SR.GetString(SR.net_nonClsCompliantException)));
}
}
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);
}
catch {
Complete(result, new Exception(SR.GetString(SR.net_nonClsCompliantException)));
}
}
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.
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
{
Collection parts;
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;
}
}
catch {
if (e == null) {
e = new Exception(SR.GetString(SR.net_nonClsCompliantException));
}
}
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);
}
catch {
Complete(result, new Exception(SR.GetString(SR.net_nonClsCompliantException)));
}
}
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);
}
catch {
Complete(result, new Exception(SR.GetString(SR.net_nonClsCompliantException)));
}
}
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);
}
catch {
Complete(result, new Exception(SR.GetString(SR.net_nonClsCompliantException)));
}
}
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
- MemberCollection.cs
- SslStream.cs
- SpStreamWrapper.cs
- CodeTypeParameterCollection.cs
- HttpWriter.cs
- NoClickablePointException.cs
- XmlDocument.cs
- ProvideValueServiceProvider.cs
- EventLogPermissionEntry.cs
- CollectionViewGroupInternal.cs
- ImageFormat.cs
- CorrelationManager.cs
- RoleGroupCollection.cs
- ExpressionVisitorHelpers.cs
- HttpUnhandledOperationInvoker.cs
- XmlDataDocument.cs
- _ContextAwareResult.cs
- LineVisual.cs
- DataRowChangeEvent.cs
- DockAndAnchorLayout.cs
- GlyphRunDrawing.cs
- Point4D.cs
- DocComment.cs
- LocationUpdates.cs
- AuthenticationModeHelper.cs
- AsynchronousChannelMergeEnumerator.cs
- ValidatorCompatibilityHelper.cs
- NumericUpDownAccelerationCollection.cs
- WindowsTreeView.cs
- StateBag.cs
- UrlPropertyAttribute.cs
- WebPartDisplayMode.cs
- XmlDomTextWriter.cs
- EventLogEntryCollection.cs
- AssemblyInfo.cs
- EncryptedData.cs
- OrderPreservingMergeHelper.cs
- ToolStripItemCollection.cs
- RuntimeConfigurationRecord.cs
- ViewBase.cs
- StickyNote.cs
- Base64Encoder.cs
- HandlerElementCollection.cs
- Root.cs
- ReadingWritingEntityEventArgs.cs
- CodeNamespaceImportCollection.cs
- LeaseManager.cs
- BaseTreeIterator.cs
- EntityObject.cs
- SourceItem.cs
- CodeBlockBuilder.cs
- ECDiffieHellmanCng.cs
- ScriptBehaviorDescriptor.cs
- XmlCharType.cs
- DataContractAttribute.cs
- TypeSystem.cs
- ExpressionBindingCollection.cs
- SerializationInfoEnumerator.cs
- AutomationTextAttribute.cs
- WinFormsSecurity.cs
- Stream.cs
- ValidatedControlConverter.cs
- FlowLayoutSettings.cs
- DrawingAttributes.cs
- ExpressionBinding.cs
- securitycriticaldataformultiplegetandset.cs
- ACL.cs
- Compiler.cs
- DataGridViewComponentPropertyGridSite.cs
- Style.cs
- PathGradientBrush.cs
- InlineUIContainer.cs
- ImageDesigner.cs
- EntityPropertyMappingAttribute.cs
- DefaultTraceListener.cs
- Permission.cs
- UriExt.cs
- X509Chain.cs
- WebBrowserNavigatingEventHandler.cs
- DataBindingExpressionBuilder.cs
- PcmConverter.cs
- SerialErrors.cs
- WebRequest.cs
- COM2TypeInfoProcessor.cs
- MultiPageTextView.cs
- CodeEventReferenceExpression.cs
- LocalFileSettingsProvider.cs
- FilterableAttribute.cs
- SelectionHighlightInfo.cs
- SettingsProperty.cs
- Double.cs
- ServiceModelConfigurationElementCollection.cs
- TraceHwndHost.cs
- LineSegment.cs
- RegexWorker.cs
- AggregateNode.cs
- CellQuery.cs
- SchemaImporterExtension.cs
- Quad.cs
- MSHTMLHost.cs