MimeKit.MimeMessage.WriteTo C# (CSharp) Method

WriteTo() public method

Writes the message to the specified output stream.
Writes the message to the output stream using the provided formatting options.
/// is null. /// -or- /// is null. /// /// The operation was canceled via the cancellation token. /// /// An I/O error occurred. ///
public WriteTo ( MimeKit.FormatOptions options, Stream stream, CancellationToken cancellationToken = default(CancellationToken) ) : void
options MimeKit.FormatOptions The formatting options.
stream Stream The output stream.
cancellationToken System.Threading.CancellationToken A cancellation token.
return void
		public void WriteTo (FormatOptions options, Stream stream, CancellationToken cancellationToken = default (CancellationToken))
		{
			if (options == null)
				throw new ArgumentNullException (nameof (options));

			if (stream == null)
				throw new ArgumentNullException (nameof (stream));

			if (compliance == RfcComplianceMode.Strict && Body != null && Body.Headers.Count > 0 && !Headers.Contains (HeaderId.MimeVersion))
				MimeVersion = new Version (1, 0);

			if (Body != null) {
				using (var filtered = new FilteredStream (stream)) {
					filtered.Add (options.CreateNewLineFilter ());

					foreach (var header in MergeHeaders ()) {
						if (options.HiddenHeaders.Contains (header.Id))
							continue;

						var rawValue = header.GetRawValue (options);

						filtered.Write (header.RawField, 0, header.RawField.Length, cancellationToken);
						filtered.Write (new [] { (byte) ':' }, 0, 1, cancellationToken);
						filtered.Write (rawValue, 0, rawValue.Length, cancellationToken);
					}

					filtered.Flush (cancellationToken);
				}

				var cancellable = stream as ICancellableStream;

				if (cancellable != null) {
					cancellable.Write (options.NewLineBytes, 0, options.NewLineBytes.Length, cancellationToken);
				} else {
					cancellationToken.ThrowIfCancellationRequested ();
					stream.Write (options.NewLineBytes, 0, options.NewLineBytes.Length);
				}

				try {
					Body.EnsureNewLine = compliance == RfcComplianceMode.Strict;
					Body.Headers.Suppress = true;
					Body.WriteTo (options, stream, cancellationToken);
				} finally {
					Body.Headers.Suppress = false;
					Body.EnsureNewLine = false;
				}
			} else {
				Headers.WriteTo (options, stream, cancellationToken);
			}
		}

Same methods

MimeMessage::WriteTo ( MimeKit.FormatOptions options, string fileName, CancellationToken cancellationToken = default(CancellationToken) ) : void
MimeMessage::WriteTo ( Stream stream, CancellationToken cancellationToken = default(CancellationToken) ) : void
MimeMessage::WriteTo ( string fileName, CancellationToken cancellationToken = default(CancellationToken) ) : void

Usage Example

Example #1
11
        public async Task<ActionResult> Index(CancellationToken cancellationToken, GmailFormModel email)
        {
            //return View();

            var result = await new AuthorizationCodeMvcApp(this, new AppFlowMetadata()).
                AuthorizeAsync(cancellationToken);

            AuthorizationCodeMvcApp o = new AuthorizationCodeMvcApp(this, new AppFlowMetadata());


            if (result.Credential != null)
            {
                if (email.to != null)
                {
                    BaseClientService.Initializer initializer = new BaseClientService.Initializer
                    {
                        HttpClientInitializer = result.Credential,
                        ApplicationName = "ASP.NET MVC Sample5"
                    };

                    var service = new GmailService(initializer);

                    string fromEmail = "*****@*****.**";
                    //string fromName = @"AdamMorgan";
                    string fromName = "";

                    /// This code ss needed to extract signed in user info (email, display name)
                    var gps = new PlusService(initializer);
                    var me = gps.People.Get("me").Execute();
                    fromEmail = me.Emails.First().Value;
                    //fromName = me.DisplayName;
                    ////////////////////////////////////////

                    MimeMessage message = new MimeMessage();
                    message.To.Add(new MailboxAddress("", email.to));
                    if (email.cc != null)
                        message.Cc.Add(new MailboxAddress("", email.cc));
                    if (email.bcc != null)
                        message.Bcc.Add(new MailboxAddress("", email.bcc));
                    if (email.subject != null)
                        message.Subject = email.subject;

                    var addr = new MailboxAddress(fromName, fromEmail);
                    message.From.Add(addr);
                    if (email.body != null)
                    {
                        message.Body = new TextPart("html")
                        {
                            Text = email.body
                        };
                    }

                    var ms = new MemoryStream();
                    message.WriteTo(ms);
                    ms.Position = 0;
                    StreamReader sr = new StreamReader(ms);
                    Google.Apis.Gmail.v1.Data.Message msg = new Google.Apis.Gmail.v1.Data.Message();
                    string rawString = sr.ReadToEnd();

                    byte[] raw = System.Text.Encoding.UTF8.GetBytes(rawString);
                    msg.Raw = System.Convert.ToBase64String(raw);
                    var res = service.Users.Messages.Send(msg, "me").Execute();

                }
                return View();
            }
            else
            {
                string redirectUri = result.RedirectUri;
                
                /// Remove port from the redirect URL.
                /// This action is needed for http://gmailappasp.apphb.com/ application only.
                /// Remove it if you hosted solution not on the http://gmailappasp.apphb.com/
                redirectUri = Regex.Replace(result.RedirectUri, @"(:\d{3,5})", "", RegexOptions.Multiline | RegexOptions.IgnoreCase);
                ///

                return new RedirectResult(redirectUri);
            }
        }
All Usage Examples Of MimeKit.MimeMessage::WriteTo