Well, I make frequent mention of this inability to find easy code to send an SMTP message, but ironically I have not yet posted any source code on the matter. Here she is.
First, put the following in your declarations area:
Imports System.Net.Mail
Second, declare the appropriate objects.
‘This declares the message itself
Dim msg As New System.Net.Mail.MailMessage
‘This declares the SMTP-related items object
Dim smtp As New System.Net.Mail.SmtpClient
‘Declare the From Address
Dim addrFrom As New System.Net.Mail.MailAddress("Insert your from address here")
‘Declare the email address for the recipient – you can add more than one recipient – just keep declaring
Dim addrTo As New System.Net.Mail.MailAddress(“Insert recipient address here”)
Third, the code.
‘Assign objects to MailMessage
msg.From = addrFrom
msg.To.Add(addrTo)
‘If you want an attachment – use this line. Repeat as necessary.
msg.Attachments.Add(New Attachment(“Insert File and Path”))
‘Subject to Email
msg.Subject = “Subject”
‘Body Contents
msg.Body = “Body”
‘Server Settings, Etc (NOTE: I use My.Settings. Replace with your contents as needed)
smtp.Host = My.Settings.Server
smtp.Port = My.Settings.Port
smtp.DeliveryMethod = Net.Mail.SmtpDeliveryMethod.Network
smtp.Credentials = New System.Net.NetworkCredential(My.Settings.Username, My.Settings.Password)
‘And, send the message
smtp.Send(msg)
My standard disclaimer is that you should have Try/Catch statements in case you receive errors attempting to send.
On a side note, I did point to an Exchange server that had an SMTP connector installed and it worked the first time without a hitch.
Thursday, July 31, 2008
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment