Esempio n. 1
0
        private void postFile(string fileName, string subject, params string[] groups)
        {
            int fileSize = 0;
            if(File.Exists(fileName))
            {
                if (groups.Length == 0)
                {
                    MessageBox.Show("No newsgroups selected", "Error posting file", MessageBoxButtons.OK);
                    return ;
                }

                Stream myStream = null;
                string result = "";

                try
                {
                    if ((myStream = File.OpenRead(fileName)) != null)
                    {
                        using (myStream)
                        {
                            // Insert code to read the stream here.
                            fileSize = (int)myStream.Length;
                            byte[] buffer = new byte[fileSize];
                            int count;                            // actual number of bytes read
                            int sum = 0;                          // total number of bytes read

                            // read until Read method returns 0 (end of the stream has been reached)
                            while ((count = myStream.Read(buffer, sum, fileSize - sum)) > 0)
                                sum += count;  // sum is a buffer offset for next reading

                            myStream.Close();

                            Encoding enc = Encoding.GetEncoding("Windows-1252");
                            result = enc.GetString(buffer);
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error Reading File", MessageBoxButtons.OK);
                }

                List<string> resultList = new List<string>(Regex.Split(result, "\r\n"));

                using (Rfc977NntpClientWithExtensions client = new Rfc977NntpClientWithExtensions())
                {
                    client.Connect(conf.UsenetServer);
                    client.AuthenticateUser(conf.UsenetUser, conf.UsenetPassword);

                    string newsgroup = groups[0];
                    for (int i = 1; i < groups.Length; i++)
                        newsgroup += "," + groups[i];

                    SetOutText("NewsGroups: " + newsgroup);

                    client.SelectNewsgroup(groups[0]);

                    ArticleHeadersDictionary headers = new ArticleHeadersDictionary();
                    headers.AddHeader("From", conf.UsenetPoster);
                    headers.AddHeader("Subject", subject);
                    headers.AddHeader("Newsgroups", newsgroup);
                    headers.AddHeader("Date", new NntpDateTime(DateTime.Now).ToString());

                    SetOutText("ResultList length: " + resultList.Count);
                    client.PostArticle(new ArticleHeadersDictionaryEnumerator(headers), resultList);

                    SetOutText("Done Sending file: " + client.LastNntpResponse);

                    string[] response = client.LastNntpResponse.Split(' ');
                    if (response[0] != "240")
                    {
                        MessageBox.Show("News server returned error: " + response[0], "Error posting file", MessageBoxButtons.OK);
                    }
                    else
                    {
                        char[] strip = { '<', '>' };
                        fileList.Last().Append(response[1].Trim(strip), fileSize);
                    }
                }
            }else{
                MessageBox.Show("Could not find file: " + fileName, "Error posting file", MessageBoxButtons.OK);
            }
        }
        public void PostArticleWithAuthentication()
        {
            using (var client = new Rfc977NntpClientWithExtensions())
            {
                client.ConnectTest();

                var newsgroup = "alt.test";
                client.SelectNewsgroup(newsgroup);

                var headers = new ArticleHeadersDictionary();
                headers.AddHeader("From", "[email protected] (Anonymous)");
                headers.AddHeader("Subject", "Test - " + Guid.NewGuid().ToString());
                headers.AddHeader("Newsgroups", newsgroup);
                headers.AddHeader("Date", new NntpDateTime(DateTime.Now).ToString());

                var body = new List<string>();
                body.Add("One line test message body.");

                client.PostArticle(new ArticleHeadersDictionaryEnumerator(headers), body);
            }
        }