locked
How do I send mail in VB.NET? RRS feed

Answers

  • You can use System.Net.Mail namespace to send an email in VB.NET.

     

    Module Module1
        Sub Main()
            '' Send some test mails
            ''
            Mailer.SendMail("jamie@plenderj.com", "jamie@plenderj.com", _
                            "Test HTML mail with attachments", "test email <br /> yeah!", _
                            "smtp.magnet.ie", , , New String() {"c:\file1.txt", _
                                                                "c:\file2.txt", "c:\file3.txt"})
            Mailer.SendMail("jamie@plenderj.com", "jamie@plenderj.com", _
                            "Test text-only mail with attachments", "test email yeah!", _
                            "smtp.magnet.ie", False, , New String() {"c:\file1.txt", _
                                                                     "c:\file2.txt", "c:\file3.txt"})
            Mailer.SendMail("jamie@plenderj.com", "jamie@plenderj.com", _
                            "Test HTML mail with no attachments", _
                            "test email <br /> yeah!", "smtp.magnet.ie")
            Mailer.SendMail("jamie@plenderj.com", "jamie@plenderj.com", _
                            "Test text-only with no attachments and authentication", _
                            "test email yeah!", "smtp.magnet.ie", , , , "MyUsername", "P@$$w0rd")
            Mailer.SendMail(From:="jamie@plenderj.com", _
                            To:="jamie@plenderj.com", _
                            Subject:="Test text-only with no attachments and authentication #2", _
                            Body:="test email yeah!", _
                            Attachments:=Nothing, _
                            IsBodyHtml:=False, _
                            MailPort:=125, _
                            MailServer:="mail.magnet.ie")
        End Sub
        Class Mailer
            ''one static method for sending e-mails
            Shared Sub SendMail(ByVal [From] As String, ByVal [To] As String, _
                                ByVal Subject As String, ByVal Body As String, ByVal MailServer _
                                As String, Optional ByVal IsBodyHtml As Boolean = True, _
                                Optional ByVal MailPort As Integer = 25, _
                                Optional ByVal Attachments() As String = Nothing, Optional _
                                ByVal AuthUsername As String = Nothing, Optional ByVal _
                                AuthPassword As String = Nothing)
                ''create a SmtpClient object to allow applications to send 
                ''e-mail by using the Simple Mail Transfer Protocol (SMTP).
                Dim MailClient As System.Net.Mail.SmtpClient = _
                New System.Net.Mail.SmtpClient(MailServer, MailPort)
                ''create a MailMessage object to represent an e-mail message
                ''that can be sent using the SmtpClient class
                Dim MailMessage = New System.Net.Mail.MailMessage( _
                [From], [To], Subject, Body)
                ''sets a value indicating whether the mail message body is in Html.
                MailMessage.IsBodyHtml = IsBodyHtml
                ''sets the credentials used to authenticate the sender
                If (AuthUsername IsNot Nothing) AndAlso (AuthPassword _
                                                         IsNot Nothing) Then
                    MailClient.Credentials = New  _
                    System.Net.NetworkCredential(AuthUsername, AuthPassword)
                End If
                ''add the files as the attachments for the mailmessage object
                If (Attachments IsNot Nothing) Then
                    For Each FileName In Attachments
                        MailMessage.Attachments.Add( _
                        New System.Net.Mail.Attachment(FileName))
                    Next
                End If
                MailClient.Send(MailMessage)
            End Sub
        End Class
    End Module
    

     

    Related thread:

    http://social.msdn.microsoft.com/forums/en-US/Vsexpressvb/thread/1a47815d-2b7e-4002-8368-50d5449170f6/

     

    For more FAQ about Visual Basic Express, please see Visual Basic Express FAQ

     

     


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Thursday, April 9, 2009 2:33 PM