Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Sunday, May 30, 2010

C# - Send email by using CDO

Ref:
http://support.microsoft.com/kb/310212


Example:
try
{
CDO.Message oMsg = new CDO.Message();
CDO.IConfiguration iConfg;

iConfg = oMsg.Configuration;

ADODB.Fields oFields;
oFields = iConfg.Fields;

// Set configuration.
ADODB.Field oField = oFields["http://schemas.microsoft.com/cdo/configuration/sendusing"];

//TODO: To send by using the smart host, uncomment the following lines:
//oField.Value = CDO.CdoSendUsing.cdoSendUsingPort;
//oField = oFields["http://schemas.microsoft.com/cdo/configuration/smtpserver"];
//oField.Value = "smarthost";

// TODO: To send by using local SMTP service.
//oField = oFields["http://schemas.microsoft.com/cdo/configuration/sendusing"];
//oField.Value = 1;

oFields.Update();

// Set common properties from message.

//TODO: To send text body, uncomment the following line:
//oMsg.TextBody = "Hello, how are you doing?";


//TODO: To send HTML body, uncomment the following lines:
//String sHtml;
//sHtml = "\n" +
// "\n" +
// "\n" +
// "\n" +
// "

\n" +
// "

Inline graphics

\n" +
// "\n" +
// "";
//oMsg.HTMLBody = sHtml;

//TOTO: To send WEb page in an e-mail, uncomment the following lines and make changes in TODO section.
//TODO: Replace with your preferred Web page
//oMsg.CreateMHTMLBody("http://www.microsoft.com",
// CDO.CdoMHTMLFlags.cdoSuppressNone,
// "", "");
oMsg.Subject = "Test SMTP";

//TODO: Change the To and From address to reflect your information.
oMsg.From = "someone@example.com";
oMsg.To = "someone@example.com";
//ADD attachment.
//TODO: Change the path to the file that you want to attach.
oMsg.AddAttachment("C:\\Hello.txt", "", "");
oMsg.AddAttachment("C:\\Test.doc", "", "");
oMsg.Send();
}
catch (Exception e)
{
Console.WriteLine("{0} Exception caught.", e);
}
return;
}

Tuesday, April 20, 2010

C# - Example of using Web Application for HTTP POST request

Below is an example of using ASP.NET (with C#) to create Web Application (.ASPX file, with code-behind DLL written in C#) for HTTP POST command:

1) HTTP Post Request (from a windows form application)

string strResult = "";
string strXML = "";
string strMD5 = "key";
string strUserID = "12345678";

try
{
// ----------------------------------------------------------------
// Send HTTP POST command
// ----------------------------------------------------------------
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(
"http://172.18.2...../Test_HTTPPost.ASPX");
request.Method = "POST";

// additional parameters for HTTP header
request.Headers.Add("Encrypt", strMD5);
request.Headers.Add("UserID", strUserID);

// encode HTTP POST data in base-64 format
byte[] szBase64Bytes;

strXML = "....";

szBase64Bytes = new System.Text.UTF8Encoding().GetBytes(strXML);
strXML = System.Convert.ToBase64String(szBase64Bytes);
byte[] byteArray = Encoding.UTF8.GetBytes(strXML);

request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;

Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();

// Get response
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);

string strTemp = "";
while ((strTemp = reader.ReadLine()) != null)
{
strResult = strResult + strTemp;
}
dataStream.Close();
response.Close();

MessageBox.Show("Result=[" + strResult + "]");
}
catch (Exception ex)
{
MessageBox.Show("Fault. Error=[" + ex.Message + "]");
}

2) Retrieve data for HTTP Post Request (from a ASPX Web Application)

protected void Page_Load(object sender, EventArgs e)
{
string strLogDir = this.Server.MapPath(".\\") + "Log";
Directory.CreateDirectory(strLogDir);
strLogDir = strLogDir + "\\";

LogToFile(strLogDir, "Load Page");

// get request information
int iSize = Request.ContentLength;

// get HTTP header
string strMD5 = Request.Headers["Encrypt"];
string strUserID = Request.Headers["UserID"];

// get HTTP post data
Stream dataStream = Request.InputStream;
StreamReader reader = new StreamReader(dataStream);

string strTemp = "";
string strResult = "";
while ((strTemp = reader.ReadLine()) != null)
{
strResult = strResult + strTemp;
}
dataStream.Close();

if (strResult != "")
{ // Base64 decode of input parameter
byte[] byteArray = System.Convert.FromBase64String(strResult);

System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();
strResult = enc.GetString(byteArray);
}

LogToFile(strLogDir, "Size=[" + iSize + "]");
LogToFile(strLogDir, "MD5=[" + strMD5 + "]");
LogToFile(strLogDir, "UserID=[" + strUserID + "]");
LogToFile(strLogDir, "Data=[" + strResult + "]");

// response for testing purpose
Response.Write("Test");
Response.End();
}

private static void LogToFile(string strURL,
string strMess)
{
DateTime Now = System.DateTime.Now;
string strLog = "";
string strNow = Now.ToString("yyyyMMdd");

string REQUEST_LOG = strURL + "XXX" + strNow + ".txt";

StreamWriter sr = File.Exists(REQUEST_LOG) ? File.AppendText(REQUEST_LOG) : File.CreateText(REQUEST_LOG);

strLog = Now.ToString("yyyy-MM-dd HH:mm:ss:") + Now.Millisecond.ToString() + "," + strMess;
sr.WriteLine(strLog);

sr.Close();
}

Thursday, March 18, 2010

C# - Example of HTTP Post

Below is an eample for HTTP POST (with additional HTTP header)

{ ....
try
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create
("http://xxxx/Mt.do");
request.Method = "POST";

string parameter = "HTTP body content";
byte[] byteArray = Encoding.UTF8.GetBytes(parameter);

request.Headers.Add("Encrypt", strMD5);
request.Headers.Add("Mobile", "xxxx");
request.Headers.Add("Vas", "xxxx");

request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;

Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (HttpStatusCode.OK == response.StatusCode)
{
dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);

string line;
while ((line = reader.ReadLine()) != null)
{
MessageBox.Show(line.ToString());
}
dataStream.Close();
response.Close();
}
else MessageBox.Show("Response code not OK");
....

}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
...
}