dotnet sendmail

code, code, dotnet, C#

sending email is a snap with asp.net 2.0, this simple function sends an email to the specified name and email you pass to the function. with a little database integration this can be recalled in almost infinite recursion, and will not be tagged as spam because it sends individual emails, not a bulk message to many users. for fastest delivery times specify the direct path to your mail server (see line #18), and setup your mail server to not scan webserver mail for spam.

    asp.net c# code
  1. using System.Net.Mail;
  2. using System.Net.Mime;
  3.  
  4. protected void sendmsg(string name, string email)
  5. {
  6. String filename = Server.MapPath("some.random.file.exe");
  7. Attachment file = new Attachment(filename, MediaTypeNames.Application.Octet);
  8.  
  9. MailMessage msg = new MailMessage();
  10. msg.From = new MailAddress("sender@fontvir.us");
  11. msg.To.Add(new MailAddress(email));
  12. msg.Subject = "test email";
  13. msg.Attachments.Add(file);
  14. msg.IsBodyHtml = true;
  15. msg.Body = "hello, " + name.ToString();
  16.  
  17. SmtpClient client = new SmtpClient();
  18. client.Host = "mail.fontvir.us";
  19. client.Send(msg);
  20. }
  21.  
example useage
  1. sendmsg("xero", "x@xero.owns.us");

RFC822 compliant dates for rss feeds

code, dotnet, php, work, C#

with the advent of web-syndication, a few different feed protocols have evolved (rss and atom being the most popular). because of their growth in popularity the protocol to create a feed has become more stringent. luckily we have validators who help us keep our feeds on the right track. one of these guidelines is RFC822 compliant dates. these dates look like Sat, 14 Jul 2007 18:40:26 -0400. formatting your data to be compliant with this standard can be challenging, so i have written some code to help you along...

Creating a RFC822 compliant date in php is a snap...

    php code
  1. $rssDate = date("r");


in asp.net this is a bit more tricky. microsoft doesn't have a native RFC822 date format, so we are going to have to convert our dateTime.now() into that format
    asp.net C#
  1. public string dateFormat(DateTime date)
  2. {
  3. int offset = TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now).Hours;
  4. string timeZone = "+" + offset.ToString().PadLeft(2, '0');
  5. if (offset < 0)
  6. {
  7. int i = offset * -1;
  8. timeZone = "-" + i.ToString().PadLeft(2, '0');
  9. }
  10. return date.ToString("ddd, dd MMM yyy HH:mm:ss " + timeZone.PadRight(5, '0'));
  11. }
  12.  

the centeral randomizer

code, dotnet, random, work, C#

here is my asp.net randomizer class based on the c R250/512 shift-register sequence random number generator, by kirkpatrick and stoll and published (j. computational physics, vol 40, pp. 517-526) with an added a pseudo-random class redefinition and buffer overflow protection.

    asp.net 2.0 class
  1. public sealed class randomizer {
  2. private int good_index;
  3. private int evil_index;
  4. private int[] good_buffer = new int[250];
  5. private int[] evil_buffer = new int[521];
  6. public randomizer()
  7. {
  8. Random r = new Random();
  9. int i = 521;
  10. int seed = 1;
  11. int salt = 999999999;
  12. while (i-- > 250) {
  13. evil_buffer[i] = r.Next();
  14. }
  15. while (i-- > 31) {
  16. good_buffer[i] = r.Next();
  17. evil_buffer[i] = r.Next();
  18. }
  19. while (i-- > 0) {
  20. good_buffer[i] = (r.Next() | seed) & salt;
  21. evil_buffer[i] = (r.Next() | seed) & salt;
  22. salt ^= seed;
  23. seed >>= 1;
  24. }
  25. good_buffer[0] = seed;
  26. evil_buffer[0] = salt;
  27. good_index = 0;
  28. evil_index = 0;
  29. }
  30. public int random() {
  31. int g = good_index;
  32. int e = evil_index;
  33. int nina = g - (250-103);
  34. if (nina < 0)
  35. nina = g + 103;
  36. int xero = e - (521-168);
  37. if (xero < 0)
  38. xero = e + 168;
  39. int x = good_buffer[nina] ^ good_buffer[g];
  40. good_buffer[g] = x;
  41. int z = evil_buffer[xero] ^ evil_buffer[e];
  42. evil_buffer[e] = z;
  43. g = (g != 249) ? (g + 1) : 0;
  44. good_index = g;
  45. e = (e != 521) ? (e + 1) : 0;
  46. evil_index = e;
  47. return x ^ z;
  48. }
  49. }
example usage
  1. randomizer x = new randomizer();
  2. int num = x.random();

asp.net pure code database connection

code, database, dotnet, C#, tutorial, SQL

visual studio 2005 has a tool for connecting to a database called a "sqlDataSource". while this tool works, i find myself wanting to create the connection and build or execute stored procedures within my own C# code. this tutorial will show you how to connect to a sql database 3 different ways in asp.net

the first thing is creating a connection with your database. if you understand how connection strings are built, write your own other wise will can use the visual studio database tool to do that for us.

drag a new sqlDataSource onto the canvas, highlight it, then click the little arrow that appears to reveal configure datasource. in the windows that follow you can create a new database connection (or use an existing one). refresh the server list and select your database server (or localhost). then chose if you want to use windows integrated security (where the server is the database user) or sql server security (where you supply the sql username and password). then select the database you want to connect to on the server, and test your connection.

press ok, and you will return to the previous window, click the little plus sign to expand and show you the connection string that visual studio generated for you. copy it, and click cancel. now you can delete the sqlDataSource and switch to code view.

now the first thing we need to do is create a database connection in our code

    asp.net c# code
  1. SqlConnection con = new SqlConnection("Data Source=DEVELOPMENT;Initial Catalog=rss;Integrated Security=True");
  2.  
hardcoded procedure
    asp.net c# code
  1. int id = 13;
  2. con.Open();
  3. //-create procedure
  4. string select = "SELECT * FROM 'table' WHERE 'id' = @ID";
  5. SqlCommand command = new SqlCommand(select, Con);
  6. //-paramerter adding
  7. command.Parameters.Add("@ID", SqlDbType.Int).Value = id;
  8. //-make a data reader
  9. SqlDataReader reader = command.ExecuteReader();
  10. while (reader.Read())
  11. {
  12. String data = reader["thing"].ToString();
  13. }
  14. //-close connection
  15. con.Close();
using a stored procedure with one parameter
(ie: "DELETE 'tabelname' WHERE 'id' = @ID; ")
    asp.net c# code
  1. int id = 42;
  2. con.Open();
  3. //-setup query
  4. SqlCommand commando = new SqlCommand("deleteUser", con);
  5. commando.CommandType = CommandType.StoredProcedure;
  6. //-paramerter adding
  7. commando.Parameters.Add("@ID", SqlDbType.Int).Value = id;
  8. //-execute the query
  9. commando.ExecuteNonQuery();
  10. con.Close();
using a stored procedure without any parameters
(ie: "SELECT * FROM 'tabelname' ;")
    asp.net c# code
  1. con.Open();
  2. //-select a stored procedure
  3. SqlCommand commander = new SqlCommand("getFilename", con);
  4. commander.CommandType = CommandType.StoredProcedure;
  5. //-make a data reader
  6. SqlDataReader SQLreader;
  7. SQLreader = commander.ExecuteReader();
  8. while (SQLreader.Read())
  9. {
  10. //-select data by array index
  11. String data = SQLreader[0].ToString();
  12. //-select data by sql collumn name
  13. String data2 = SQLreader["name"].ToString();
  14. }
  15. //-close connections
  16. SQLreader.Close();
  17. con.Close();

dotnet querystrings and post vars

code, dotnet, work, C#

here's a nice little snip of c# code. this is meant to be used in an asp.net web application. the following code will return either a querystring variable or a post variable, it tries both and finds the valid one.

    asp.net c# code
  1. public string RequestParam(string paramname)
  2. {
  3. string result = string.Empty;
  4. if (Context.Request.Form.Count != 0)
  5. {
  6. result = Convert.ToString(Context.Request.Form[paramname]);
  7. }
  8. else if (Context.Request.QueryString.Count != 0)
  9. {
  10. result = Convert.ToString(Context.Request.QueryString[paramname]);
  11. }
  12. return (result == null) ? string.Empty : result.Trim();
  13. }
  14.  

MMVII .( xero harrison ) . http://the.fontvir.us/b10g
RSS syndication