コード例 #1
0
ファイル: MailTest.cs プロジェクト: marinehero/ThinkAway.net
        public void SmtpTest()
        {

            using (SmtpClient smtpClient = new SmtpClient("smtp.163.com"))
            {

                smtpClient.Connect();

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

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

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

                MailMessage mailMessage = new MailMessage(from, to);
                mailMessage.To.Add("*****@*****.**");
                mailMessage.To.Add("*****@*****.**");
                mailMessage.To.Add("*****@*****.**");
                mailMessage.To.Add("*****@*****.**");

                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);
            }
        }
コード例 #2
0
ファイル: TEST.cs プロジェクト: marinehero/ThinkAway.net
        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);
                    //}
                }
            }

        }
コード例 #3
0
        /// <summary>
        /// Parses an email address from a MIME header<br/>
        /// <br/>
        /// Examples of input:
        /// <c>Eksperten mailrobot &lt;[email protected]&gt;</c><br/>
        /// <c>"Eksperten mailrobot" &lt;[email protected]&gt;</c><br/>
        /// <c>&lt;[email protected]&gt;</c><br/>
        /// <c>[email protected]</c><br/>
        /// <br/>
        /// It might also contain encoded text, which will then be decoded.
        /// </summary>
        /// <param name="input">The value to parse out and email and/or a username</param>
        /// <returns>A <see cref="MailAddress"/></returns>
        /// <exception cref="ArgumentNullException">If <paramref name="input"/> is <see langword="null"/></exception>
        /// <remarks>
        /// <see href="http://tools.ietf.org/html/rfc5322#section-3.4">RFC 5322 section 3.4</see> for more details on email syntax.<br/>
        /// <see cref="EncodedWord.Decode">For more information about encoded text</see>.
        /// </remarks>
        internal static MailAddress ParseMailAddress(string input)
        {
            if (input == null)
                throw new ArgumentNullException("input");

            // Decode the value, if it was encoded
            input = EncodedWord.Decode(input.Trim());

            // Find the location of the email address
            int indexStartEmail = input.LastIndexOf('<');
            int indexEndEmail = input.LastIndexOf('>');
            int indexAtEmail = input.LastIndexOf('@');
            //Display Name
            string displayName = "",address = "";
            //Find it
            if(indexStartEmail == -1 || indexEndEmail == -1)
            {
            	if(indexAtEmail != -1)
            	{           			
            		displayName = input.Substring(0,indexAtEmail);
            		address = input;
            	}
            }
            else
            {
                    // Check if there is a username in front of the email address
                    if (indexStartEmail > 0)
                    {
                        // Parse out the user
                        displayName = input.Substring(0, indexStartEmail).Trim();
                        displayName = displayName.Trim('"');
                    }
                    else
                    {
                        // There was no user
                        displayName = string.Empty;
                    }

                    // Parse out the email address without the "<"  and ">"
                    indexStartEmail = indexStartEmail + 1;
                    int emailLength = indexEndEmail - indexStartEmail;
                    address = input.Substring(indexStartEmail, emailLength).Trim();
            }
            //Regex mailRegex = new Regex(@"^\w+@\w+(\.\w+)+(\,\w+@\w+(\.\w+)+)*$"); 
            //严格验证
            //mailRegex.IsMatch(address)
            //宽松验证 
            if(!address.Contains("@"))
            {
                throw new FormatException(string.Format("无效的 Mail 地址格式:{0}", address));
            }
            
            MailAddress mailAddress = new MailAddress(displayName,address);
            // It could be that the format used was simply a name
            // which is indeed valid according to the RFC
            // Example:
            // Eksperten mailrobot
            return mailAddress;
        }