Sveučilište u Zagrebu  |  Filozofski fakultet  |  Služba za informatiku  |  Kutak za korisnike  |  Teme

E-mail sending sample

Imports System.Net.Mail

Module Module1

    Sub Main()

        SendMailMessage(
                "mailserver.yourdomain.com",
                25,
                "userId-notImportantUnlessServerChecksForIt",
                "password-notImportantUnlessServerChecksForIt",
                "Sender Blue <sender.blue@yourdomain.hr>",
                "Receiver Brown <receiver.brown@yourdomain.hr>, Receiver Black <receiver.black@yourdomain.hr>",
                "Carbon Copy 1 <carbon.copy.1@yourdomain.hr>; Carbon Copy 2 <carbon.copy.2@yourdomain.hr>",
                "Subject line",
                "Click <a href=""www.facebook.com"">here</a> to go to Facebook",
                True,
                True,
                {"C:\filename1.pdf", "E:\books\book1.pdf", "C:\LocalDocuments\doc1.doc"}
                )

    End Sub

    Sub SendMailMessage(
                mailServerName As String,
                mailServerPort As Integer,
                mailServerUserId As String,
                mailServerUserPassword As String,
                mailSenderAddress As String,
                mailReceiverAddress As String,
                mailReceiverBccAddress As String,
                messageSubjectLine As String,
                messageBody As String,
                Optional isHtml As Boolean = False,
                Optional useSSL As Boolean = True,
                Optional attachmentsFilesFullPath() As String = Nothing
                )

        'Imports System.Net.Mail
        Dim mailClient As New SmtpClient()
        Dim msg As New MailMessage()

        msg.From = New MailAddress(mailSenderAddress)
        AddMailAddresses(msg.To, mailReceiverAddress)
        AddMailAddresses(msg.Bcc, mailReceiverBccAddress)

        msg.Subject = messageSubjectLine
        msg.SubjectEncoding = System.Text.Encoding.UTF8

        msg.Body = messageBody
        msg.IsBodyHtml = isHtml

        mailClient.Host = mailServerName
        mailClient.Port = mailServerPort
        mailClient.DeliveryFormat = SmtpDeliveryFormat.International

        mailClient.EnableSsl = useSSL
        mailClient.Credentials = New System.Net.NetworkCredential(mailServerUserId, mailServerUserPassword)

        If attachmentsFilesFullPath IsNot Nothing Then
            For Each file In attachmentsFilesFullPath
                msg.Attachments.Add(New Attachment(file.Trim))
            Next
        End If

        mailClient.Send(msg)
        mailClient = Nothing

    End Sub

    Sub AddMailAddresses(msgTo As MailAddressCollection, addresses As String)

        Dim addressList = addresses.Split({","c, ";"c})

        For Each addr In addressList
            msgTo.Add(addr.Trim)
        Next

    End Sub

End Module

 

komentare molim ovdje