Click here to Skip to main content
Click here to Skip to main content
Go to top

Send Email with VB.NET windows application

, 13 Mar 2008
Rate this:
Please Sign up or sign in to vote.
Send email with attachment using windows app by posting to ASP.net application

Download WindowsMailer.zip - 50.72 KB

Download EmailSender.zip - 4.4 KB

WindowsApp.JPG

Send email with attachment using windows app by posting to ASP.net App

Normally a windows app needs SMTP server to send email. But to setup SMTP server in every client machine is not feasible. With this setup, the emailing will be handled by the web application (as most asp.net servers give smtp access) and the windows app will POST email details including attachment to a ASP.NET page and the page can get the values from the controls and send the mail.

WebApp.JPG

Background

Applications cant trust outlook to send email as most people may not configure it, In those case they can use this solution

Using the code

Set up the web app and give its URL in the windows app. I have created a folder named D:\Emailer\ to save the attachment. You might need to change the value for view state if the webserver is returning error. Check the correct value by updoading a file with browser to the web app and logging the request. (Code commented)

"Content-Disposition: form-data; name=""__VIEWSTATE""")"")"/wEPDwULLTIwMjIyMzkxMzQPZBYCAgMPZBYCAgkPFgIeCWlubmVyaHRtbAUBc2RkYtx2BkFJ2ZWs9JmppGZpGmQ0NFY=")

As the web app is using multipart/form-data to receive form values and a file as attachment, the windows app is simulating such a communication to post the data (rfc1867). It will put begin end marking and send the form variables along with the attachement. The asp.net app gets the values form the controls in the Load event handler and can use them to send the mail. Find the form variables in asp.net page. Please create a folder in D:\Emailer so that the attachment can be saved. The email sending code is omitted as it is very popular.

    Dim sURL As String = txtURL.Text
        Dim request As WebRequest = WebRequest.Create(sURL)
        Dim response As WebResponse
        Dim writeStream As Stream
        Dim bytes(0) As Byte

        request.Method = "POST"
        request.Timeout = 20000
        request.ContentType = "multipart/form-data; boundary=---------------------------7d823238a027a"

        Try
            GetContent(bytes)
            request.ContentLength = bytes.Length
            writeStream = request.GetRequestStream()
            writeStream.Write(bytes, 0, bytes.Length)
            writeStream.Close()
        Catch ex As Exception
            If Not writeStream Is Nothing Then writeStream.Close()
            MessageBox.Show(ex.Message + " : " + ex.StackTrace)
            Return
        End Try

        Try
            response = request.GetResponse()
        Catch ex As Exception
            MessageBox.Show(ex.Message + " : " + ex.StackTrace)
            Return
        End Try
        
        Dim sr As StreamReader = New StreamReader(response.GetResponseStream())
        txtResponse.Text = sr.ReadToEnd()
        

You can see the actual communication if you could log the request in Asp.net page

Points of Interest

The annoying thing is to post correct data so that the asp.net page can receive it

History

Keep a running update of any changes or improvements you've made here.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

sonur
Software Developer
India India
.Net dev for last 3 years wanting to learn more!

Comments and Discussions

 
GeneralSend Email with vb.net through web server PinmemberC G Gerring21-Jun-12 10:48 
GeneralMy vote of 1 PinmemberCode Master12328-Apr-12 7:52 
Questionthanks Pinmembermohamed_nor_eldin2-Apr-12 7:57 
GeneralMy vote of 3 PinmemberMatto Matti12-Sep-11 7:46 
Generalplease send me code to send email Pinmembermona200918-Dec-09 1:33 
GeneralVisual Studio 2002 code Pinmembermona200916-Dec-09 4:04 
GeneralVB.NET windows application Pinmembermona200916-Dec-09 4:01 
Generalexplain code Pinmembermncba15-Dec-09 2:58 
I want to be more clear about the asp code
 
I want the code to send email
GeneralNICE ARTICLE Pinmembererick4u16-Aug-08 0:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140905.1 | Last Updated 13 Mar 2008
Article Copyright 2008 by sonur
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid