Tuesday, April 29, 2008

ASP.NET, Sending mail with attachment

Unlike PHP, sending mail out with ASP.NET is always troublesome especially if you are sending it from a shared hosting company and you don't have much access towards it except using FTP. I was having a major headache while trying to send email out from client's hosting site while it has been working properly on my development server. The problem started when i'm trying to send the email with the attachment. I cant get proper permission for the folder to upload the attachment thus giving me trouble to upload the file and attach it. I found several solutions which at the end only one of them that helps. This is one of the solution which i found STUPID:

"
Dim msg As New Net.Mail.MailMessage(txtFrom.Text,txtTo.Text,txtSubject.Text,txtMessage.Text)

If fileUpload.PostedFile.FileName = "" Then
Else
msg.Attachments.Add( New System.Net.Mail.Attachment(fileUpload.PostedFile.FileName))
End If"


taken from this stupid site He didn't even bother to use fileUpload.HasFile to check if the control has any file attached to it but use .Filename ="" instead. Either lazy or stupid programmer.

At the end I managed to find a solution which I think it's the best for my situation which I can't upload the file to a folder those, by using stream I can attach the file without even executing any IO to the disk. Superb!!

 if (attachFile.PostedFile != null && 
attachFile.PostedFile.ContentLength > 0)
{
// attachFile.PostedFile.FileName contains
// the full path of the file. We only want the file
// so we delimit it by forward slashes into an array

string[] tempFileName =
attachFile.PostedFile.FileName.Split('\\');

// attachFile.PostedFile exposes a System.IO.Stream
// property named InputStream

Attachment emailAttach = new Attachment(
attachFile.PostedFile.InputStream,
tempFileName[tempFileName.Length - 1]);

email.Attachments.Add(emailAttach);
}
attachFile is a FileUpload control and email is a MailMessage class. :) You can read the whole solution here

Cheers,
Hatim

Monday, April 28, 2008

Ebooks

I thought I share with you guys some ebook links as i stumbled upon while searching information regarding AJAX and XHTML.

http://acinfo.unap.cl/fmedina/archivos/Libros/Computacion/135.For.Dummies.ebooks.Wiley.Publishing/
http://hates.home.anadolu.edu.tr/books/?C=M;O=D
http://seclab.pl/pdf/

although the best place to look for AJAX and ASP.NET information would be at http://www.asp.net/

Knowledge is power... have fun reading!!!

Cheers,
Rads78

Wednesday, April 16, 2008

Welcome....

Welcome to the technical side of me. As a technical manager cum programmer with several years of experience doing development, I have lots to share with you readers regarding development tips and tricks. Although this blog will be focusing most of the time on C# development, ASP.NET and .NET framework, I will as well include some information regarding Linux's system administration. But do not expect much from me on Linux's side as its not my cup of tea but still I enjoy working on it. Feel free to browse and comment on all of my posts and please excuse me if what i write here is wrong or the explanation is not as what its supposed to be. Please correct me if I do so and by the end of the day, I hope that this blog would be beneficial to me, you and all of the development community worldwide.

By the end, what differs between programmers are not what they had programmed but how they programmed it.

Regards from Malaysia (or anywhere around the world),
Hatim