ToString() public method

Returns a System.String that represents the current MimeKit.MimeMessage.

Returns a System.String that represents the current MimeKit.MimeMessage.

Note: In general, the string returned from this method SHOULD NOT be used for serializing the message to disk. It is recommended that you use WriteTo(Stream,CancellationToken) instead.

public ToString ( ) : string
return string
Exemplo n.º 1
0
        private static void SendConfirmationEmail(GmailService gmail, Dictionary <string, string> dict)
        {
            MailMessage mailmsg = new MailMessage();

            {
                mailmsg.Subject = dict["subject"];
                mailmsg.Body    = string.Format(dict["HTML"], dict["link"]);
                mailmsg.From    = new MailAddress(dict["from"]);
                mailmsg.To.Add(new MailAddress(dict["to"]));
                mailmsg.IsBodyHtml = true;
            }
            ////add attachment if specified
            if (dict.ContainsKey("attachement"))
            {
                if (File.Exists(dict["attachment"]))
                {
                    Attachment data = new Attachment(dict["attachment"]);
                    mailmsg.Attachments.Add(data);
                }
                else
                {
                    Console.WriteLine("Error: Invalid Attachemnt");
                }
            }
            //Make mail message a Mime message
            MimeKit.MimeMessage mimemessage = MimeKit.MimeMessage.CreateFromMailMessage(mailmsg);
            Google.Apis.Gmail.v1.Data.Message finalmessage = new Google.Apis.Gmail.v1.Data.Message();
            finalmessage.Raw = Base64UrlEncode(mimemessage.ToString());
            var result = gmail.Users.Messages.Send(finalmessage, "me").Execute();
        }
        public static async Task<SendMessageResponse> SendMessageAsync(string accessToken,
            SendMessageRequest sendMessageRequest, string username)
        {
            var message = new MimeMessage();
            //message.From.Add(new MailboxAddress(sendMessageRequest.Message.));
            foreach (var to in sendMessageRequest.Message.ToRecipients)
            {
                message.To.Add(new MailboxAddress(to.EmailAddress.Name, to.EmailAddress.Address));
            }
            message.Subject = sendMessageRequest.Message.Subject;

            var builder = new BodyBuilder();

            // Set the plain-text version of the message text
            //builder.TextBody = @"";

            // Set the html version of the message text
            builder.HtmlBody = sendMessageRequest.Message.Body.Content;

            // Now we just need to set the message body and we're done
            message.Body = builder.ToMessageBody();
            var encodedEmail = Base64UrlEncode(message.ToString());
            var url = $"https://www.googleapis.com/upload/gmail/v1/users/{username}/messages/send?uploadType=media";
            var sendMessageResponse = new SendMessageResponse { Status = SendMessageStatusEnum.NotSent };
            using (var client = new HttpClient())
            {
                using (var request = new HttpRequestMessage(HttpMethod.Post, url))
                {
                    request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
                    request.Content = new StringContent(encodedEmail, Encoding.UTF8, "message/rfc822");
                    using (var response = await client.SendAsync(request))
                    {
                        if (response.IsSuccessStatusCode)
                        {
                            sendMessageResponse.Status = SendMessageStatusEnum.Sent;
                            sendMessageResponse.StatusMessage = null;
                        }
                        else
                        {
                            sendMessageResponse.Status = SendMessageStatusEnum.Fail;
                            sendMessageResponse.StatusMessage = response.ReasonPhrase;
                        }
                    }
                }
            }
            return sendMessageResponse;
        }
        public async void SendEmailToGmail(string subject, string From, string data)
        {
            GmailService gmailService = await GetGmailService();

            MailMessage mail = new MailMessage();

            mail.Subject    = subject;
            mail.Body       = data;
            mail.From       = new MailAddress(From);
            mail.IsBodyHtml = true;
            mail.To.Add(new MailAddress("*****@*****.**"));
            //mail.CC.Add(new MailAddress(CC));
            #region comments
            //foreach (string add in vendorEmailList.Split(','))
            //{
            //    if (string.IsNullOrEmpty(add))
            //        continue;

            //    mail.To.Add(new MailAddress(add));
            //}

            //foreach (string add in userEmailList.Split(','))
            //{
            //    if (string.IsNullOrEmpty(add))
            //        continue;

            //    mail.CC.Add(new MailAddress(add));
            //}

            //foreach (string path in attachments)
            //{
            //    //var bytes = File.ReadAllBytes(path);
            //    //string mimeType = MimeMapping.GetMimeMapping(path);
            //    Attachment attachment = new Attachment(path);//bytes, mimeType, Path.GetFileName(path), true);
            //    mail.Attachments.Add(attachment);
            //}
            #endregion
            MimeKit.MimeMessage mimeMessage = MimeMessage.CreateFromMailMessage(mail);
            Message             message     = new Message();
            var inputBytes = System.Text.Encoding.UTF8.GetBytes(mimeMessage.ToString());
            message.Raw = Convert.ToBase64String(inputBytes);
            var result = gmailService.Users.Messages.Send(message, From).Execute();
        }
Exemplo n.º 4
0
		public void TestIssue135 ()
		{
			var message = new MimeMessage ();
			message.Body = new TextPart ("plain") {
				ContentTransferEncoding = ContentEncoding.Base64,
				ContentObject = new ContentObject (new MemoryStream (new byte[1], false))
			};

			try {
				message.ToString ();
			} catch (Exception ex) {
				Assert.Fail (ex.Message);
			}
		}
        public async Task SendEmailAsync(
            IEnumerable <string> to,
            IEnumerable <string> cc,
            IEnumerable <string> bcc,
            string subject,
            string body)
        {
            using (var gmailservice = await _googleSheetsServiceAccessor.GetGmailServiceAsync())
            {
                Boolean noReciver = true;
                System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
                var msg = new MailMessage()
                {
                    Subject    = subject,
                    Body       = body,
                    IsBodyHtml = true
                };

                foreach (var address in to)
                {
                    if (!string.IsNullOrEmpty(address))
                    {
                        noReciver = false;
                        msg.To.Add(new MailAddress(address));
                    }
                }
                foreach (var address in cc)
                {
                    if (!string.IsNullOrEmpty(address))
                    {
                        noReciver = false;
                        msg.CC.Add(new MailAddress(address));
                    }
                }
                foreach (var address in bcc)
                {
                    if (!string.IsNullOrEmpty(address))
                    {
                        noReciver = false;
                        msg.Bcc.Add(new MailAddress(address));
                    }
                }
                //foreach (string path in attachments)
                //{
                //    Attachment attachment = new Attachment(path);
                //    msg.Attachments.Add(attachment);
                //}

                if (noReciver != true)
                {
                    MimeKit.MimeMessage mimeMessage = MimeMessage.CreateFromMailMessage(msg);
                    Message             message     = new Message()
                    {
                        Raw = Base64UrlEncode(mimeMessage.ToString())
                    };
                    try
                    {
                        var result = gmailservice.Users.Messages.Send(message, "me").Execute();
                        _logger.Information("Just sent the email");
                    }
                    catch (Exception e)
                    {
                        _logger.Error("An error occurred: " + e.Message);
                    }
                }
                else if (noReciver == true)
                {
                    _logger.Warning("No recivers for this email");
                }
            }
        }