protected async void Button1_Click(object sender, EventArgs e)
        {




                MandrillApi api = new MandrillApi("Mandrill Api Key", true);

                IEnumerable<EmailAddress> addresses = new[]
            {
                new EmailAddress("*****@*****.**", "Name Of the Contact"),
                
            };




                var message = new EmailMessage();

                message.FromEmail = "*****@*****.**";
                message.FromName = "Your name";
            message.Html = "emailbody";
                message.Text = "emailbodytext";
            message.Subject = "subject";
                message.To = addresses;
              

                SendMessageRequest req = new SendMessageRequest(message);

                var returnvalue = await api.SendMessage(req);
             
            }
        /// <summary>
        /// Send an email
        /// </summary>
        /// <param name="request">Contains detailed email info and optionally provider info (such as ApiKeys)</param>
        /// <param name="messageId">The message id returned by the service provider</param>
        /// <param name="detailMessage">The message returned by the service provider, e.g. "failed to send message", "Sent", "Message Queued for delivery"</param>
        /// <returns>True if email was sent successfully</returns>
        public override bool SendMail(SendMailRequest request, out string messageId, out string detailMessage)
        {
            // Create request
            string apiKey = GetApiKey(request);
            MandrillApi api = new MandrillApi(apiKey);

            // Email details
            EmailMessage msg = new EmailMessage();
            msg.FromEmail = request.From;
            msg.To = request.To.Select(e=>new EmailAddress(e));
            msg.Text = request.Text;

            // Send email
            SendMessageRequest smReq = new SendMessageRequest(msg);
            Task<List<EmailResult>> task = api.SendMessage(smReq);
            task.Wait();

            // process response
            messageId = "";
            detailMessage = "";

            EmailResult er = null;
            if (task.Result != null && task.Result.Count >0)
            {
                er = task.Result[0];
            }

            if (er == null)
            {
                detailMessage = "Invalid return result from provider.";
                return false;
            }

            messageId = er.Id;
            detailMessage = er.Status.ToString();

            if (er.Status == EmailResultStatus.Queued
                || er.Status == EmailResultStatus.Scheduled
                || er.Status == EmailResultStatus.Sent)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public async Task<string> Send(IMessage message)
        {
            //var info = _api.UserInfo();

            var msg = new EmailMessage();

            msg.Subject = message.Subject;
            msg.FromEmail = message.From;
            msg.Html = message.Body;

            msg.To = new List<EmailAddress> { new EmailAddress() { Email = message.To } };

            msg.TrackClicks = true;
            msg.TrackOpens = true;

            var request = new SendMessageRequest(msg);

            var result = (await this._api.SendMessage(request)).FirstOrDefault();

            if (result == null)
            {
                return null;
            }

            if (result.Status == EmailResultStatus.Rejected)
            {
                throw new MailerException(result.RejectReason, MailerException.enReason.RejectByProvider);
            }

            if (result.Status != EmailResultStatus.Sent)
            {
                throw new MailerException("Sending email failed", MailerException.enReason.Other);
            }

            return result.Id;
        }
    /// <summary>
    ///   Sends a new transactional message through Mandrill.
    /// </summary>
    /// <param name="message">
    ///   The message.
    /// </param>
    /// <param name="send_at">
    ///   The send_at.
    /// </param>
    /// <returns>
    ///   The <see cref="Task" />.
    /// </returns>
    public async Task<List<EmailResult>> SendMessage(SendMessageRequest request)
    {
      string path = "/messages/send.json";

      List<EmailResult> resp = await Post<List<EmailResult>>(path, request);
      return resp;
    }
    public async Task Should_Send_Email_Message_With_Headers() {
      // Setup
      string apiKey = ConfigurationManager.AppSettings["APIKey"];
      string toEmail = ConfigurationManager.AppSettings["ValidToEmail"];
      string fromEmail = ConfigurationManager.AppSettings["FromEMail"];

      // Exercise
      var api = new MandrillApi(apiKey);

      var sendMessageRequest = new SendMessageRequest(new EmailMessage {
        To =
          new List<EmailAddress> { new EmailAddress { Email = toEmail, Name = "" } },
        FromEmail = fromEmail,
        Subject = "Mandrill Integration Test",
        Html = "<strong>Example HTML</strong>",
        Text = "Example text",
      });
      sendMessageRequest.Message.AddHeader("Reply-To", fromEmail);
      List<EmailResult> result = await api.SendMessage(sendMessageRequest);

      // Verify
      Assert.AreEqual(1, result.Count);
      Assert.AreEqual(toEmail, result.First().Email);
      Assert.AreEqual(EmailResultStatus.Sent, result.First().Status);
    }
Beispiel #6
0
        public async Task<bool> SendAsync(EmailMessage email, bool deleteAttachmentes, params string[] attachments)
        {
            var config = this.Config as Config;

            if (config == null)
            {
                email.Status = Status.Cancelled;
                return false;
            }

            try
            {
                email.Status = Status.Executing;


                var message = new Mandrill.Models.EmailMessage();
                message.RawTo = email.SentTo.Split(',').ToList();
                message.FromEmail = email.FromEmail;
                message.FromName = email.FromName;
                message.Subject = email.Subject;
                if (email.IsBodyHtml)
                {
                    message.Html = email.Message;
                }
                else
                {
                    message.Text = email.Message;
                }
                message = AttachmentHelper.AddAttachments(message, attachments);
                var sendMessageRequest = new SendMessageRequest(message);

                var api = new MandrillApi(config.ApiKey);
                var result = await api.SendMessage(sendMessageRequest);

                var status = result.First().Status;
                // Verify
                if (status == Mandrill.Models.EmailResultStatus.Invalid || status == Mandrill.Models.EmailResultStatus.Rejected)
                {
                    email.Status = Status.Failed;
                }
                else if (status == Mandrill.Models.EmailResultStatus.Queued || status == Mandrill.Models.EmailResultStatus.Scheduled)
                {
                    email.Status = Status.Executing;
                }
                else if (status == Mandrill.Models.EmailResultStatus.Sent)
                {
                    email.Status = Status.Completed;
                }
                return true;
            }
            // ReSharper disable once CatchAllClause
            catch (Exception ex)
            {
                email.Status = Status.Failed;
                Log.Warning(@"Could not send email to {To} using SendGrid API. {Ex}. ", email.SentTo, ex);
            }
            finally
            {
                if (deleteAttachmentes)
                {
                    FileHelper.DeleteFiles(attachments);
                }
            }

            return false;
        }
        public virtual async Task<List<IMailResponse>> SendAsync(MailAttributes mailAttributes)
        {
            var mail = GenerateProspectiveMailMessage(mailAttributes);
            var response = new List<IMailResponse>();
            var request = new SendMessageRequest(mail);

            await _client.SendMessage(request).ContinueWith(x => response.AddRange(x.Result.Select(result => new MandrillMailResponse
            {
                Email = result.Email,
                Status = MandrillMailResponse.GetProspectiveStatus(result.Status.ToString()),
                RejectReason = result.RejectReason,
                Id = result.Id
            })));

            return response;
        }
        public virtual List<IMailResponse> Send(MailAttributes mailAttributes)
        {
            var mail = GenerateProspectiveMailMessage(mailAttributes);
            var response = new List<IMailResponse>();
            var request = new SendMessageRequest(mail);

            var resp = AsyncHelpers.RunSync(() => _client.SendMessage(request));
            response.AddRange(resp.Select(result => new MandrillMailResponse
            {
                Email = result.Email,
                Status = MandrillMailResponse.GetProspectiveStatus(result.Status.ToString()),
                RejectReason = result.RejectReason,
                Id = result.Id
            }));

            return response;
        }