Esempio n. 1
0
        public void TestSend()
        {
            try
            {
                Console.WriteLine("\r\n ----- Smtp Test Below -----");

                msg.Subject = subject;
                msg.Body    = body;
                msg.AddImage(@"..\lib\test attachments\test.jpg", "testimage");
                msg.AddImage(@"..\lib\test attachments\test2.jpg", "testimage2");
                msg.HtmlBody = "<body><table><tr><td><b>Here is an embedded IMAGE:<img src=\"cid:testimage\"></td></tr>\r\n<tr><td>Here's another: <img src=\"cid:testimage2\"></td></tr></table></body>";


                msg.AddRecipient(ccAddress, AddressType.Cc);
                msg.AddRecipient(bccAddress, AddressType.Bcc);

                msg.AddAttachment(@"..\lib\test attachments\test.jpg");
                msg.AddAttachment(new Attachment(new FileStream(@"..\lib\test attachments\test.htm", FileMode.Open, FileAccess.Read), "test.htm"));

                msg.AddCustomHeader("X-FakeTestHeader", "Fake Value");
                msg.AddCustomHeader("X-AnotherFakeTestHeader", "Fake Value");
                msg.Notification = true;
                msg.Charset      = "ISO-8859-1";
                msg.Priority     = MailPriority.Low;


                smtp.Username = "******";
                smtp.Password = "******";


                for (int i = 0; i < 1; i++)
                {
                    smtp.SendMail(msg);
                }
            }
            catch (SmtpException se)
            {
                Assertion.Fail("TestSend() threw a SmtpException: " + se.Message);
            }
            catch (System.Exception e)
            {
                Assertion.Fail("TestSend() threw a System.Exception: " + e.Message + "; Target: " + e.TargetSite);
            }
        }
Esempio n. 2
0
        protected void Init()
        {
            sender        = "*****@*****.**";
            recipient     = "*****@*****.**";
            cc            = "*****@*****.**";
            senderName    = "FromName";
            recipientName = "ToName";
            ccName        = "ccName";
            subject       = "Mail Message Test\r\n";
            body          = "Hello from MailMessageTest";
            htmlBody      = "<HTML><HEAD></HEAD><BODY bgColor=\"#00ffff\"><b>Hello Jane. This is the body of the HTML mail message.</b></BODY></HTML>";
            charset       = "us-ascii";

            senderEmail    = new EmailAddress(sender, senderName);
            recipientEmail = new EmailAddress(recipient, recipientName);
            ccEmail        = new EmailAddress(cc, ccName);

            msg = new MailMessage(senderEmail, recipientEmail);
            msg.AddRecipient("*****@*****.**", AddressType.To);
            msg.AddRecipient("*****@*****.**", AddressType.To);
            msg.Subject  = subject;
            msg.Body     = body;
            msg.Charset  = charset;
            msg.Priority = MailPriority.High;

            msg.HtmlBody = htmlBody.ToString();
            msg.AddRecipient(ccEmail, AddressType.To);
            msg.AddRecipient(ccEmail, AddressType.Cc);
            msg.AddCustomHeader("X-Something", "Value");
            msg.AddCustomHeader("X-SomethingElse", "Value");
            msg.AddAttachment(@"..\lib\test attachments\test.jpg");
            msg.AddAttachment(@"..\lib\test attachments\test.htm");
            Attachment att = new Attachment(@"..\lib\test attachments\test.zip");

            msg.AddAttachment(att);


            msg.Notification = true;
        }
Esempio n. 3
0
        /// <summary>
        /// Send a problem report by email
        /// </summary>
        public void Send(List <Failure> failures)
        {
            if (failures.Count > 0)
            {
                MailMessage mm = new MailMessage();
                mm.Subject = "Failed tasks on " + RuntimeEnvironment.ServerName;
                mm.Body    = EmailIntro + Checker.FailuresAsText(failures);
                mm.UseHtml = false;
                mm.AddRecipient(Config.GetApplicationSettingValue("AlertRecipient", ""), "");
                mm.Send();

                if (mm.Result != "")
                {
                    Console.WriteLine(mm.Result);
                    Lib.Exceptions.Log(mm.Result);
                }
            }
        }
Esempio n. 4
0
        public void MailTest()
        {
            using (SmtpClient smtpClient = new SmtpClient("smtp.163.com"))
            {
                smtpClient.Connected              += (x, y) => _logger.Debug("mail", "Connected:{0}", y);
                smtpClient.Authenticated          += (x, y) => _logger.Debug("mail", "Authenticated:{0}", y);
                smtpClient.StartedMessageTransfer += (x, y) => _logger.Debug("mail", "StartedMessageTransfer:{0}", y);
                smtpClient.EndedMessageTransfer   += (x, y) => _logger.Debug("mail", "EndedMessageTransfer:{0}", y);
                smtpClient.Disconnected           += (x, y) => _logger.Debug("mail", "Disconnected:{0}", y);

                smtpClient.Connect();

                smtpClient.UserName = "******";
                smtpClient.Password = "******";

                smtpClient.Authenticate("*****@*****.**", "*****@*****.**");

                MailAddress from = new MailAddress("Lsong", "*****@*****.**");
                MailAddress to   = new MailAddress("*****@*****.**");
                MailAddress cc   = new MailAddress("Test<*****@*****.**>");

                MailMessage mailMessage = new MailMessage(from, to);
                mailMessage.AddRecipient(cc, AddressType.Cc);
                mailMessage.AddRecipient("*****@*****.**", AddressType.Bcc);

                mailMessage.Charset      = "UTF-8";
                mailMessage.Priority     = MailPriority.High;
                mailMessage.Notification = true;

                mailMessage.AddCustomHeader("X-CustomHeader", "Value");
                mailMessage.AddCustomHeader("X-CompanyName", "Value");

                //string testCid = mailMessage.AddImage("C:\\test.bmp");

                //mailMessage.AddAttachment("C:\\test.zip");

                mailMessage.Subject  = "This's a test Mail.";
                mailMessage.Body     = "hello everybody .";
                mailMessage.HtmlBody =
                    string.Format("<html><body>hello everybody .<br /><img src='cid:{0}' /></body></html>", "");

                smtpClient.SendMail(mailMessage);
            }

            using (PopClient popClient = new PopClient("pop.163.com"))
            {
                popClient.UserName = "";
                popClient.Password = "";
                popClient.Connect("pop.163.com", 110, false);
                popClient.Authenticate("*****@*****.**", "*****@*****.**");

                int messageCount = popClient.GetMessageCount();
                for (int i = messageCount; i > 1; i--)
                {
                    //try
                    //{
                    MessageHeader      messageHeader = popClient.GetMessageHeaders(i);
                    MailAddress        sender        = messageHeader.Sender;
                    MailAddress        from          = messageHeader.From;
                    List <MailAddress> to            = messageHeader.To;
                    string             subject       = messageHeader.Subject;

                    _logger.Debug("mail", subject);
                    if (sender != null)
                    {
                        _logger.Info("mail", "Sender:{0}", sender);
                    }
                    if (from != null)
                    {
                        _logger.Info("mail", "From:{0}", from);
                    }
                    if (to != null)
                    {
                        foreach (MailAddress mailAddress in to)
                        {
                            _logger.Info("mail", "TO:{0}", mailAddress);
                        }
                    }
                    Message     message  = popClient.GetMessage(i);
                    MessagePart textBody = message.FindFirstPlainTextVersion();
                    MessagePart htmlBody = message.FindFirstHtmlVersion();


                    if (textBody != null)
                    {
                        string text = textBody.GetBodyAsText();
                        System.Console.WriteLine(text);
                    }
                    else if (htmlBody != null)
                    {
                        string html = htmlBody.GetBodyAsText();
                        System.Console.WriteLine(html);
                    }
                    System.Console.ReadKey();
                    //}
                    //catch (Exception exception)
                    //{
                    //    _logger.Error("mail", exception);
                    //}
                }
            }
        }
Esempio n. 5
0
        private void ProcessMailMessage(IMessage msg, MailMessage outMsg)
        {
            //Copy some of the more important headers
            outMsg.From = MailMessage.CreateEmailAddress(MapiUtils.GetStringProperty(msg, Tags.PR_SENDER_EMAIL_ADDRESS),
                MapiUtils.GetStringProperty(msg, Tags.PR_SENDER_NAME));
            if (MapiUtils.GetStringProperty(msg, Tags.ptagSentRepresentingName) != null) {
                outMsg.ReplyTo = MailMessage.CreateEmailAddress(MapiUtils.GetStringProperty(msg, Tags.ptagSentRepresentingEmailAddr),
                    MapiUtils.GetStringProperty(msg, Tags.ptagSentRepresentingName));
            }
            outMsg.Subject = MapiUtils.GetStringProperty(msg, Tags.PR_SUBJECT);

            IMAPITable recipientsTable = null;
            msg.GetRecipientTable(0, out recipientsTable);
            using (recipientsTable) {

                MAPI33.MapiTypes.Value[,] rows;
                recipientsTable.SetColumns(new Tags[] {Tags.PR_RECIPIENT_TYPE, Tags.PR_EMAIL_ADDRESS, Tags.PR_DISPLAY_NAME},
                    IMAPITable.FLAGS.Default);

                for( ;recipientsTable.QueryRows(1, 0, out rows) == Error.Success && rows.Length > 0; ) {
                    MAPI33.WellKnownValues.PR_RECIPIENT_TYPE recipType = (MAPI33.WellKnownValues.PR_RECIPIENT_TYPE)((MapiInt32)rows[0,0]).Value;
                    String emailAddr = null;
                    String emailName = null;

                    if (rows[0, 1] is MapiString) {
                        emailAddr = ((MapiString)rows[0, 1]).Value;
                    }

                    if (rows[0, 2] is MapiString) {
                        emailName = ((MapiString)rows[0, 2]).Value;
                    }

                    AddressType type = AddressType.To;

                    switch (recipType) {
                        case MAPI33.WellKnownValues.PR_RECIPIENT_TYPE.To:
                            type = AddressType.To;
                            break;

                        case MAPI33.WellKnownValues.PR_RECIPIENT_TYPE.Cc:
                            type = AddressType.Cc;
                            break;

                        case MAPI33.WellKnownValues.PR_RECIPIENT_TYPE.Bcc:
                            type = AddressType.Bcc;
                            break;
                    }

                    outMsg.AddRecipient(emailAddr, emailName, type);
                }
            }

            //Set the body.  There can be a plain Body, which is the plain text version of the message,
            //as well as an HtmlBody, which includes rich text formatting via HTML.
            //
            //This is complicated by the fact that, in MAPI, there are three possible sources
            //of the body: PR_BODY (plain text body), PR_BODY_HTML (HTML body), and PR_RTF_COMPRESSED
            //(RTF body).  Based on empirical observation, there is nearly always a PR_BODY, which is not surprising
            //as every HTML mail client I've ever encountered sends multi-part MIME messages with a plain text version
            //as well as HTML.  PR_BODY_HTML *seems* to be the HTML version of the message as specified by the sender,
            //and may not be present in plain text messages.
            //
            //It only gets complicated with PR_RTF_COMPRESSED (or just PR_RTF for short).  Often, HTML messages
            //don't have a PR_HTML at all, but rather a PR_RTF that has a bunch of RTF tags wrapped around the original
            //HTML of the message.  All messages I've seen have a PR_RTF, however only messages which were originally
            //received as HTML and subsequently transmogrified into RTF include the \@fromhtml RTF tag.
            //
            //Thus, the existence of this \@fromhtml tag is a roundabout way of determining if a message came from
            //the sender as HTML or text (ignoring for now the third possiblity; an older Exchange client using RTF).
            //
            //Therefore, when setting BodyHtml, we first check if PR_RTF includes this \@fromhtml (that's what
            //MapiUtils.BodyRtfToHtml does, among other things).  If it doesn't, BodyHtml is set to null.  If it
            //does, PR_BODY_HTML is checked first, and used to populate BodyHtml if it's non-null.  Failing that, the
            //unwrapped HTML from PR_RTF is used.  So:
            //
            // Body: Always PR_BODY
            // HtmlBody: PR_BODY_HTML if PR_BODY_HTML is non-null and PR_RTF includes the \@fromhtml tag
            //           The HTML embedded in PR_RTF if PR_BODY_HTML is null and PR_RTF includes \@fromhtml
            //           Else null.
            String propVal = null;
            if ((propVal = MapiUtils.GetLongStringProperty(msg, Tags.PR_BODY)) != null) {
                outMsg.Body = propVal;
            }

            //Check for HTML embedded in PR_RTF
            byte[] rtf = MapiUtils.GetBodyRtf(msg);
            if (rtf != null) {
                outMsg.HtmlBody = MapiUtils.BodyRtfToHtml(rtf);
                if (outMsg.HtmlBody != null) {
                    //There's HTML.  use PR_HTML if it's there, else stick w/ what we have
                    if ((propVal = MapiUtils.GetLongStringProperty(msg, Tags.PR_BODY_HTML)) != null) {
                        outMsg.HtmlBody = propVal;
                    }
                }
            }

            //Copy the attachments
            CopyAttachments(msg, outMsg);

            //Output all of the MAPI properties as extended headers, just in case they are needed later
            DumpMapiProperties(msg, outMsg);
        }