INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Web Services

Sending an email with Attachment through a web service by jshurst
Posted: 15 Jun 05 (Edited 21 Sep 06)

I had a programmer ask me how they could send an email automatically with an attachment.  I decided to use a web service for this and decided to share it with all of you.

CODE

Imports System
Imports System.Web.Services
Imports System.Exception

<System.Web.Services.WebService(Namespace:="http://tempuri.org/UtilityService/UtilityService")> _
Public Class UtilityService
    Inherits System.Web.Services.WebService

#Region " Web Services Designer Generated Code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Web Services Designer.
        InitializeComponent()

        'Add your own initialization code after the InitializeComponent() call

    End Sub

    'Required by the Web Services Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Web Services Designer
    'It can be modified using the Web Services Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        components = New System.ComponentModel.Container
    End Sub

    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        'CODEGEN: This procedure is required by the Web Services Designer
        'Do not modify it using the code editor.
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

#End Region

    <WebMethod(EnableSession:=True, Description:="This creates an basic email with an optional attachment.  Remember that the path is relative to the server on which the web service resides.  So C:\ is not your local machine, but rather the server.  The path for an attachment when creating this web service was 'C:\inetpub\wwwroot\apps\UtilityService\Attachments\Test.pdf'.  Alternatively you can fully qualify the path to another server.  Also if an attachment (or a carboned person) is not needed, just enter 'Nothing' in those fields.  In addition please notice that the Recipients (and CarbonCopy) parameter is an array, so that multiple people can be emailed at once.")> _
             Public Function Email(ByVal Sender As String, ByVal Recipients() As String, ByVal CarbonCopy() As String, ByVal AttachmentPath As String, ByVal EmailSubject As String, ByVal EmailBody As String) As String

        'dimming the return value
        Dim Outcome As String

        Try
            'Dimming a new message (email)
            Dim Message As System.Web.Mail.MailMessage = New System.Web.Mail.MailMessage
            'dimming a string so that I can create a giant string of all recipients
            Dim MassEmail As String = Recipients(0)

            'dimming a string so that I can loop through records
            Dim strElement As String

            'looping through each recipient in the array
            For Each strElement In Recipients
                'if the following is true then we have reached the end of the loop (or an error has occured)
                If MassEmail = strElement Or strElement Is Nothing Then

                Else
                    'adding a new recipient to the string
                    MassEmail = MassEmail & ";" & strElement
                End If

            Next strElement


            'resetting the strElement
            strElement = Nothing


            'dimming a string so that I can create a giant string of all cc'ed people (if any exsist)

            'since the carbon copy field can be left blank
            If CarbonCopy Is Nothing Then
                Message.Cc = Nothing
            Else
                Dim MassCarbonCopy As String = CarbonCopy(0)
                'creating a string for carbon copy recipients
                For Each strElement In CarbonCopy
                    'If the following is true then we have reached the end of the loop
                    If MassCarbonCopy = strElement Or strElement Is Nothing Then
                    Else
                        'adding a cc'ed person to the list
                        MassCarbonCopy = MassCarbonCopy & ";" & strElement
                    End If

                Next strElement

                'setting the carbon copy recipients to the string we just built
                Message.Cc = MassCarbonCopy
            End If


            Try
                'must be a valid sender (email address)
                Message.From = Sender

            Catch ex As Exception
                Outcome = "You must specify a valid email address for the sender. (e.x. bill@microsoft.com)"
                Return Outcome
            End Try

            Try
                'setting the recipients to the string built above
                Message.To = MassEmail

            Catch ex As Exception
                Outcome = "You must specify a valid email address for the recipient (in the form of an array)."
                Return Outcome
            End Try

            'setting the subject and the body based on what was passed in
            Message.Subject = EmailSubject
            Message.Body = EmailBody


            Try
                'If no attachment was sent over
                If AttachmentPath Is Nothing Then
                Else
                    'creating the attachment
                    Dim Attachment As System.Web.Mail.MailAttachment = New System.Web.Mail.MailAttachment(AttachmentPath)

                    'Adding the attachment to the message
                    Message.Attachments.Add(Attachment)
                End If

            Catch ex As Exception
                Outcome = "There was a problem attaching the message.  Please make sure that the path is correct and remember that this path is relative to the server on which the web server resides."
                Outcome = ex.ToString
                Return Outcome
            End Try

            'sending the message
            System.Web.Mail.SmtpMail.Send(Message)

            'returning the outcome
            Outcome = "Email(s) succeeded."
            Return Outcome
        Catch ex As Exception
            'returning the outcome if there is an error
            Outcome = "There was a  problem with the email."
            Outcome = ex.ToString
            Return Outcome
        End Try

    End Function

Hope this helps.

Back to Microsoft: ASP.NET FAQ Index
Back to Microsoft: ASP.NET Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close