![]() |
Languages »
VB.NET »
General
Beginner
VB.Net 2005 SMTP Email MessageBy EdacioSend an SMTP mail message with multiple recipients and multiple file attachments |
VB, Windows, .NET, Visual Studio, Dev
|
||||||||||
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
07-11-2006 Article and Source Updated
I needed to send an SMTP email to multiple recipients with multiple attachements in VB.net 2005. Sending an SMTP email in VB.net 2005 changed a little since VB.net 2003. I have updated the class so that the SendEmailMessage procedure is overloaded to accept either multiple recipients and multiple attachments or a single recipient and a single attachment. This makes it a little easier so that you do not need to instanciate two string arrays if you are only sending an email to one recipient
Create a class file with this code
Imports Microsoft.VisualBasic
Imports System.net.Mail
Public Class SendEmail
Public Sub SendEmailMessage(ByVal strFrom As String, ByVal strTo() _
As String, ByVal strSubject _
As String, ByVal strMessage _
As String, ByVal fileList() As String)
'This procedure takes string array parameters for multiple recipients and files
Try
For Each item As String In strTo
'For each to address create a mail message
Dim MailMsg As New MailMessage(New MailAddress(strFrom.Trim()), New MailAddress(item))
MailMsg.BodyEncoding = Encoding.Default
MailMsg.Subject = strSubject.Trim()
MailMsg.Body = strMessage.Trim() & vbCrLf
MailMsg.Priority = MailPriority.High
MailMsg.IsBodyHtml = True
'attach each file attachment
For Each strfile As String In fileList
If Not strfile = "" Then
Dim MsgAttach As New Attachment(strfile)
MailMsg.Attachments.Add(MsgAttach)
End If
Next
'Smtpclient to send the mail message
Dim SmtpMail As New SmtpClient
SmtpMail.Host = "10.10.10.10"
SmtpMail.Send(MailMsg)
Next
'Message Successful
Catch ex As Exception
'Message Error
End Try
End Sub
Public Sub SendEmailMessage(ByVal strFrom As String, ByVal strTo _
As String, ByVal strSubject _
As String, ByVal strMessage _
As String, ByVal file As String)
'This procedure overrides the first procedure and accepts a single
'string for the recipient and file attachement
Try
Dim MailMsg As New MailMessage(New MailAddress(strFrom.Trim()), New MailAddress(strTo))
MailMsg.BodyEncoding = Encoding.Default
MailMsg.Subject = strSubject.Trim()
MailMsg.Body = strMessage.Trim() & vbCrLf
MailMsg.Priority = MailPriority.High
MailMsg.IsBodyHtml = True
If Not file = "" Then
Dim MsgAttach As New Attachment(file)
MailMsg.Attachments.Add(MsgAttach)
End If
'Smtpclient to send the mail message
Dim SmtpMail As New SmtpClient
SmtpMail.Host = "10.10.10.10"
SmtpMail.Send(MailMsg)
Catch ex As Exception
'Message Error
End Try
End Sub
End Class
Instead of setting the SMTP Host settings inside of the class you can config your SMTP Host inside of the Web.Config file.
<system.net> <mailSettings> <!-- these settings define the mail server settings from: the user name from which the email is sent - this is the application that is sending the message host: the name of your mail server userName: the name the application will use to log into the mail server password: the password for the above user name --> <smtp from="admin@your-domain.com"> <network host="your-mail-server-name" userName="your-user-name" password="your-password" /> </smtp> </mailSettings> </system.net>
Use this code in your form code or in an asp.net code behind
Dim SendEmail As New SendEmail
Dim SendTo(2) As String
Dim FileAttach(2) As String
Dim strSubject As String
Dim strMessage As String
SendTo(0) = "someone@email.com"
SendTo(1) = "someoneelse@email.com"
FileAttach(0) = "c:\text.txt"
FileAttach(1) = "c:\Othertext.txt"
strSubject = "Email Subject"
strMessage = "Email Messge Text" 'The body encoding is set to HTML
SendEmail.SendEmailMessage("fromsomeone@email.com", SendTo, strSubject, strMessage, FileAttach)
Since the SendEmailMessage is overloaded you can also send an email to a single recipient with a single attachment.
Dim SendEmail As New SendEmail
Dim SendTo As String
Dim FileAttach As String
Dim strSubject As String
Dim strMessage As String
SendTo = "someone@email.com"
FileAttach(0) = "c:\text.txt"
strSubject = "Email Subject"
strMessage = "Email Messge Text" 'The body encoding is set to HTML
SendEmail.SendEmailMessage("fromsomeone@email.com", SendTo, strSubject, strMessage, FileAttach)
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 11 Jul 2006 Editor: |
Copyright 2006 by Edacio Everything else Copyright © CodeProject, 1999-2009 Web21 | Advertise on the Code Project |