Beispiel #1
0
        public bool SendSmtpEmail(string emailTo, string url, HttpPostedFileBase attachment = null, string[] cc = null, string[] bcc = null, string attachmentPath = null)
        {
            var settings = new EmailSettings();

            var emailContent = new EmailMessageContent
            {
                Subject = settings.MessageSubject,
                Body    = GetHtml(url)
            };
            var subject  = emailContent.Subject;
            var bodyHtml = emailContent.Body;

            if (string.IsNullOrEmpty(emailTo))
            {
                throw new Exception("Recipient is empty!");
            }

            var sended = SendEmail(emailTo, subject, bodyHtml, settings, cc, bcc, attachment, attachmentPath);

            if (sended)
            {
                return(true);
            }

            return(false);
        }
        private String SendEmailTest([FromBody] EmailMessageContent sentEmail)
        {
            var sb = new StringBuilder();

            sb.AppendFormat("Received email message from {0}: , To {1}, Subject {2}", sentEmail.From, sentEmail.To, sentEmail.Subject);
            //sentEmail.Attachments.ForEach(i =>
            //    sb.AppendFormat("Got attachment {0} of type {1} and size {2} bytes,", i.FileName, i.MimeType,
            //        i.FileData.Length)
            //    );

            var result = sb.ToString();

            Trace.Write(result);

            return(result);
        }
        private HttpResponseMessage SendEmail(EmailMessageContent sentEmail)

        /*void sendEmail(string strFrom
         *                  , string strTo
         *                  , string strSubject
         *                  , string strBody
         *                  , string strAttachment)*/
        {
            /// Send an email using NG SMTP server

            char[]   delimiterChars = { ',', ';' };
            string[] strToNames     = sentEmail.To.Split(delimiterChars);

            MailMessage objMailMessage = new MailMessage();

            System.Net.NetworkCredential objSMTPUserInfo = new System.Net.NetworkCredential();
            SmtpClient objSmtpClient = new SmtpClient();

            try
            {
                objMailMessage.From = new MailAddress(sentEmail.From);

                //Add each To address to the message
                foreach (string s in strToNames)
                {
                    objMailMessage.To.Add(new MailAddress(s));
                }

                //Setup message
                objMailMessage.Subject = sentEmail.Subject;
                objMailMessage.Body    = sentEmail.Body;

                //Get attachments
                foreach (var i in sentEmail.Attachments)
                {
                    Stream iBytedata = new MemoryStream(i.FileData);
                    objMailMessage.Attachments.Add(new Attachment(iBytedata, i.FileName, i.MimeType));
                }

                objSmtpClient   = new SmtpClient("intmail.ngic.com"); /// NatGen Server IP
                objSMTPUserInfo = new System.Net.NetworkCredential
                                      ("AH-APP-PL-Dev", "&uThuC5A", "NGIC");
                objSmtpClient.Credentials           = objSMTPUserInfo;
                objSmtpClient.UseDefaultCredentials = false;
                objSmtpClient.DeliveryMethod        = SmtpDeliveryMethod.Network;

                //Send Message
                objSmtpClient.Send(objMailMessage);
            }
            catch (Exception ex)
            { throw ex; }

            finally
            {
                objMailMessage  = null;
                objSMTPUserInfo = null;
                objSmtpClient   = null;
            }

            return(Request.CreateResponse <string>(HttpStatusCode.OK, "Email Sent"));
        }