Introduce DataBase,Asp.net,JavaScript,Xml,Html,Css,Sql,Php,ASP.NET Controls,AJAX,Tools,HTML,CSS,JavaScript,Open Source Project,WPF,.Net Framework,Linq
Top Recommended Hosting

Send Email with VB.NET windows application

by the3factory 3/13/2008 6:23:00 PM

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.

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

A brief description of how to use the article or code. The class names, the methods and properties, any tricks or tips.

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.

Collapse
	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

Related posts

Sign up for PayPal and start accepting credit card payments instantly.


Powered by BlogEngine.NET 1.2.0.0