Locating and E-Mailing a File

So far, the programs you have created in this chapter let you download a specific file from a remote server. There may be times, however, when you will want to e-mail a file that resides on a remote server to a specific e-mail address. The MailFile web service in Listing 13.8 uses e-mail to send the file a user specifies to a specific e-mail address. To create the MailFile web service, perform these steps:

  1. Within Visual Studio .NET, select the File menu New Project option. Visual Studio .NET will display the New Project dialog box.

  2. Within the New Project dialog box Project Types list, click Visual Basic Projects. Then, within the Templates field, click ASP.NET Web Service. Finally, within the Location field, specify the folder within which you want to store the program and the program name MailFile. Select OK. Visual Studio .NET will display a page onto which you can drag and drop the service’s components.

  3. Select the View menu Code option. Visual Studio .NET will display the program’s source code. Within the source code add the program statements in Listing 13.8.

Listing 13.8 MailFile.asmx.vb

start example
Imports System.Web.Mail <WebMethod()> Public Function MailIt(ByVal FilePath As String) _ Ä As Boolean      Try      Dim Smtp As SmtpMail      Smtp.SmtpServer = "smtp.someprovider.com"      Dim Msg As MailMessage = New MailMessage()      Msg.Attachments.Add(New MailAttachment(FilePath, _ Ä        MailEncoding.Base64))      Msg.To = "YourEmail@SomeAddress.com"      Msg.From = "YourEmail@SomeAddress.com"      Msg.Subject = FilePath      Smtp.Send(Msg)      MailIt = True    Catch Ex As Exception      MailIt = False    End Try End Function
end example

For security purposes, the web service will send the requested file only to a specific e-mail address, which prevents another user from using the web service to send files from your website to their own e-mail accounts. To use this web service, you must specify your e-mail address for the Msg object To and From fields. You must also specify the address of your SMTP server for the SMTP object SmtpServer field.

Sending a Remote File to an E-Mail Address

The Visual Basic .NET program in Listing 13.9, RemoteEmail.vb, interacts with the MailFile web service to e-mail a file’s contents to a specific e-mail address. When you run the program, your screen will display a form similar to that shown in Figure 13.8 that prompts you to enter the file that you desire as well as the target e-mail address.

click to expand
Figure 13.8: Using e-mail to send a remote file to a specific e-mail address

To create the RemoteEmail.vb program, perform these steps:

  1. Within Visual Studio .NET, select the File menu New Project option. Visual Studio .NET will display the New Project dialog box.

  2. Within the New Project dialog box Project Types list, click Visual Basic Projects. Then, within the Templates field, click Windows Application. Finally, within the Location field, specify the folder within which you want to store the program and the program name RemoteEmail. Select OK. Visual Studio .NET will display a form onto which you can drag and drop the program’s controls.

  3. Using the Toolbox, drag and drop the button and text boxes, list box, label, and hyperlink label previously shown in Figure 13.6 onto the page.

  4. Select the Project menu Add Web Reference option. Visual Studio .NET will display the Add Web Reference dialog box.

  5. Within the Address field, type localhost/MailFile/Service1.asmx?WSDL and press Enter. The dialog box will load the file’s contents. Click the Add Reference button.

  6. Select the View menu Code option. Visual Studio .NET will display the program’s source code. Within the source code add the program statements in Listing 13.9.

Listing 13.9 RemoteEmail.vb

start example
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As _ Ä System.EventArgs) Handles Button1.Click      If (TextBox1.Text.Length > 0) Then       Try          Dim WS As New localhost.Service1()          If WS.MailIt(TextBox1.Text) Then            TextBox2.Text = "Message Sent"          Else            TextBox2.Text = "Error sending message"          End If       Catch Ex As Exception          TextBox2.Text = Ex.Message       End Try     Else       TextBox2.Text = "Must specify a file"     End If End Sub
end example

As you can see, when the user clicks the E-Mail the File button, the code calls the MailFile web service’s MailIt method, passing to the method the name of the remote file the user wants to his e-mail account.




. NET Web Services Solutions
.NET Web Services Solutions
ISBN: 0782141722
EAN: 2147483647
Year: 2005
Pages: 161
Authors: Kris Jamsa

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net