Using a Web Service to Provide E-Mail Access

In the previous section, you learned how to create Visual Basic .NET programs that can send e-mail messages. You may be wondering, therefore, why you might create a web service to send e-mail messages. Assume, for example, you are on the road and don’t have access to a program that you can use to send e-mail messages. For such cases, you can create a web service that provides you with the ability to send a message from your e-mail account access regardless of your location. Using the web service, you can create and send messages using only a browser, as shown in Figure 10.3.

click to expand
Figure 10.3: Using a browser to interact with a web service that allows a user to send an e-mail message

You might also use such a web service to log the messages you send using a remote PC or to simplify the process of using public-key encryption to encrypt the messages you must send from a remote location. In addition, you may also find that providing your programs and web services with e-mail access makes it easy to notify programmers and system administrators that a serious error has occurred. For example, if a program is unable to access a database, the program can automatically send e-mail messages to specific addresses to notify developers of the error.

The MailAccess web service shown in Listing 10.2 provides methods that you can use to send e-mail messages:

boolean SimpleMessage(string ToAddr, string Subject, string body) boolean StandardMessage(string ToAddr, string CC, string BCC, _    string Subject, string Body) boolean MessageWithAttachment(string ToAddr, string CC, string BCC, _     string Subject, string Body, string Attachment) boolean ExtendedMessage(string ToAddr, string From, string Server, _     string CC, string BCC, string Subject, string Body, string _ Attachment)

The methods vary by the options each supports. The SimpleMessage method, for example, does not support courtesy and blind-courtesy copies or the attachment of messages. The ExtendedMessage method, in contrast, not only provides such settings, it also lets the caller specify the SMTP server that he wants to use to send the messages.

To create the MailAccess 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 MailAccess. 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 10.2.

Listing 10.2 MailAccess Web Service

start example
Imports System.Web.Mail 'CONSTANTS Private Const SMTP_SERVER As String = "put_your_SMTP_server_here" Private Const EMAIL_ADDRESS As String = "put_your_email_address_here" <WebMethod()> Public Function SimpleMessage(ByVal ToAddr As String, _ Ä   ByVal Subject As String, ByVal Body As String) As Boolean    Dim SMTPInfo As SmtpMail    Dim Message As New MailMessage()    SMTPInfo.SmtpServer = SMTP_SERVER    Message.To = ToAddr    Message.Subject = Subject    Message.Body = Body    Message.From = EMAIL_ADDRESS    SMTPInfo.Send(Message)    SimpleMessage = True End Function <WebMethod()> Public Function StandardMessage(ByVal ToAddr As String, _ Ä   ByVal CC As String, ByVal BCC As String, ByVal Subject As String, _ Ä   ByVal Body As String) As Boolean    Dim SMTPInfo As SmtpMail    Dim Message As New MailMessage()    SMTPInfo.SmtpServer = SMTP_SERVER    Message.To = ToAddr    Message.Cc = CC    Message.Bcc = BCC    Message.From = EMAIL_ADDRESS    Message.Subject = Subject    Message.Body = Body    SMTPInfo.Send(Message)    StandardMessage = True End Function <WebMethod()> Public Function MessageWithAttachment(ByVal ToAddr As _ Ä   String, ByVal CC As String, ByVal BCC As String, ByVal Subject As _ Ä   String, ByVal Body As String, ByVal Attachment As String) _ Ä   As  Boolean    Dim SMTPInfo As SmtpMail    Dim Message As New MailMessage()    SMTPInfo.SmtpServer = SMTP_SERVER    Message.To = ToAddr    Message.Cc = CC    Message.Bcc = BCC    Message.Subject = Subject    Message.Body = Body    Message.From = EMAIL_ADDRESS    If (Attachment.Length > 0) Then      Message.Attachments.Add(New MailAttachment(Attachment, _      Ä  MailEncoding.Base64))    End If    SMTPInfo.Send(Message)    MessageWithAttachment = True End Function <WebMethod()> Public Function ExtendedMessage(ByVal ToAddr As String, _ Ä  ByVal From As String, ByVal Server As String, ByVal CC As String, _ Ä  ByVal BCC As String, ByVal Subject As String, ByVal Body As String, _ Ä  ByVal Attachment As String) As Boolean    Dim SMTPInfo As SmtpMail    Dim Message As New MailMessage()    SMTPInfo.SmtpServer = Server    Message.To = ToAddr    Message.Cc = CC    Message.Bcc = BCC    Message.Subject = Subject    Message.Body = Body    Message.From = From    If (Attachment.Length > 0) Then      Message.Attachments.Add(New MailAttachment(Attachment, _      Ä  MailEncoding.Base64))    End If    SMTPInfo.Send(Message)    ExtendedMessage = True End Function
end example

Before you can use the web service to send e-mail messages, you must change the program code to specify your SMTP server address and the e-mail address from which you want the messages to be sent, by changing the following statements:

Private Const SMTP_SERVER As String = "put_your_SMTP_server_here" Private Const EMAIL_ADDRESS As String = "put_your_email_address_here"

The e-mail address you specify must be one that is valid to the SMTP server. Otherwise, the SMTP server might not send the message.

The SendMail program imports the System.Web.Mail namespace. To use the namespace, you must include a reference to the system.web.dll file by performing these steps:

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

  2. Within the Add Reference dialog box, select the System.Web.dll file and choose OK.

The web service ExtendedMessage method lets the caller specify the address of a specific SMTP server and a valid username on the server. If, for example, the service’s default SMTP server is unavailable, a program can use the ExtendedMessage method to specify an alternate server.

Putting the MailAccess Web Service to Use

In a similar way, the ASP.NET page in Listing 10.3, SendRemoteMail.aspx, displays a form similar to that shown in Figure 10.4 that you can use to send an e-mail message. After you enter the message data and click the Send Message button, the page will interact with the MailAccess web service to send the message. The page also provides two fields a user can use to specify the SMTP server that the user wants to use (such as stmp.east.cox.net) as well as her username on the server (such as JaneDoe@cox.net).

click to expand
Figure 10.4: Sending an e-mail message using an ASP.NET page that interacts with a web service

To create the SendRemoteMail.aspx page, 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 Application. Finally, within the Location field, specify the folder within which you want to store the program and the program name SendRemoteMail. Select OK. Visual Studio .NET will display a page onto which you can drag and drop the program’s controls (label, buttons, and text box).

  3. Using the Toolbox, drag and drop the button and text boxes, labels, and buttons previously shown in Figure 10.4 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/MailAccess/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 10.3.

    Listing 10.3 SendRemoteMail.aspx

    start example
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As _ Ä  System.EventArgs) Handles Button1.Click   Dim WS As New localhost.Service1()   Dim Attachments As String = ""   ' Do not support attachments   Dim Result As Boolean = False   Try     If TextBox1.Text.Length = 0 Then       TextBox8.Text = "Must specify target address"     Else       If (TextBox6.Text.Length = 0) Then         Result = WS.StandardMessage(TextBox1.Text, TextBox2.Text, _         Ä  TextBox3.Text, TextBox4.Text, TextBox5.Text)       Else         Result = WS.ExtendedMessage(TextBox1.Text, TextBox7.Text, _         Ä  TextBox6.Text, TextBox2.Text, TextBox3.Text, TextBox4.Text, _         Ä  TextBox5.Text, Attachments)       End If       If Result Then         TextBox8.Text = "Message Sent"         TextBox1.Text = ""         TextBox2.Text = ""         TextBox3.Text = ""         TextBox4.Text = ""         TextBox5.Text = ""       Else         TextBox8.Text = "Error sending message"       End If     End If   Catch Ex As Exception     TextBox8.Text = Ex.Message   End Try End Sub
    end example

Although the MailAccess web service supports file attachments, the ASP.NET page, for simplicity, does not use them. To support attachments, the code would need to send the attached files to a remote service as binary data. In Chapter 11, “Integrating Binary Data into .NET Web Services,” you will learn how to exchange binary data between a program and web service.

If the user does not specify an SMTP server and username, the code will use the web service’s SendStandard method to send the message. If the user specifies an SMTP server and username, the code will call the ExtendedMessage method.

start sidebar
Taking Advantage of Fax Capabilities

Years ago, having a fax machine was essential not only for offices, but also for many homes. Today, e-mail has eliminated the need for most faxes. That said, too many users still feel the need to send legal documents via fax.

The website eFax.com, shown in the following illustration, enables users to integrate faxing and e-mail. At the eFax site, you can register for a fax number to which other users can send you faxes. When a fax arrives at your number, the eFax software will capture and send the fax contents to you as an e-mail message. To the sender, the process appears as a standard fax operation.

The eFax.com site also lets you send faxes using e-mail. To do so, you create a message that specifies the recipient’s fax information and to which you attach the document you want the software to fax.

Using the e-mail web services this chapter presents, your programs can use the eFax. com website to send faxes. In the future, the eFax.com site will likely offer a web service interface.

end sidebar




. 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