Sending an E-Mail Message with .NET

The .NET environment makes it very easy to create programs that send e-mail messages. For example, the Visual Basic .NET program in Listing 10.1, SendMail.vb, displays a form similar to that shown in Figure 10.1 that you can use to send an e-mail message. As you can see, the form lets you specify recipients, a subject, and message text, and also lets you attach a file that you want to send with the message.

click to expand
Figure 10.1: Sending an e-mail message using a Visual Basic .NET program

To create the SendMail.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. Within the Name and Location fields, type SendMail. 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 text boxes, labels, and buttons previously shown in Figure 10.1 onto the form.

  4. 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.1.

    Listing 10.1 SendMail.vb

    start example
    Imports System.Web.Mail Dim Attachments As String = "" Dim Message As New MailMessage() Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As _ Ä   System.EventArgs) Handles Button2.Click   If (TextBox1.Text.Length = 0) And (TextBox2.Text.Length = 0) And _ Ä   (TextBox3.Text.Length = 0) Then     MessageBox.Show("Must specify target e-mail address")   Else     Dim SMTPInfo As SmtpMail     SMTPInfo.SmtpServer = "put_your_SMTP_server_here"     Message.To = TextBox1.Text     Message.Cc = TextBox2.Text     Message.Bcc = TextBox3.Text     Message.Subject = TextBox4.Text     Message.Body = TextBox5.Text     Button2.Enabled = False     SMTPInfo.Send(Message)     Message.Attachments.Clear()     TextBox1.Text = ""     TextBox2.Text = ""     TextBox3.Text = ""     TextBox4.Text = ""     TextBox5.Text = ""     TextBox6.Text = ""     MessageBox.Show("Message Sent")     Button2.Enabled = True   End If End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As _ Ä   System.EventArgs) Handles Button1.Click   Dim FileDB As New OpenFileDialog()   If (FileDB.ShowDialog() = DialogResult.OK) Then     If (FileDB.FileName.Length > 0) Then       Message.Attachments.Add(New MailAttachment(FileDB.FileName, _       Ä   MailEncoding.Base64))       TextBox5.Text = FileDB.FileName & ";" & TextBox5.Text     End If   End If End Sub
    end example

Before you can use the SendMail program to send an e-mail message, you must assign the address of your SMTP (Simple Mail Transfer Protocol) server to the SMTPInfo object’s SmtpServer field, replacing the code shown here with your server’s address:

SMTPInfo.SmtpServer = "put_your_SMTP_server_here"

For example, if you use an SMTP server at smtp.somesite.com, you would use the following entry:

SMTPInfo.SmtpServer = "stmp.somesite.com"

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, as shown in Figure 10.2.

    click to expand
    Figure 10.2: Adding a reference to the System.Web.dll file

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

As discussed, the program lets you attach files to the e-mail messages that you send. If you click the Attach File button, the program will display an Open dialog box that you can use to browse for the files you want to attach to the message. The current code does not support the selection of multiple files within the dialog box. To support multiple files, you would need to parse the selected files, placing a semicolon between filenames.




. 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