SMS texting with VB - vb.net

Program sends SMS text messages from your computer using a G-mail account. This code works fine but it takes a bit of overall time and it is currently sending a text to 4 different email addresses, with only one working. So and ideas or help would be great.
Help write a loop or something of the sort to simplify my code?
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
Dim Smtp_Server As New SmtpClient
Dim e_mail As New MailMessage()
Dim self As New MailAddress("blank#gmail.com")
Dim Sprint As String
Dim Verizon As String
Dim TMobile As String
Dim ATT As String
Smtp_Server.UseDefaultCredentials = False
Smtp_Server.Credentials = New Net.NetworkCredential("blank#gmail.com", "password")
Smtp_Server.Port = 587
Smtp_Server.EnableSsl = True
Smtp_Server.Host = "smtp.gmail.com"
Sprint = "#pm.sprint.com"
Verizon = "#vtext.com"
TMobile = "#tmomail.net"
ATT = "#txt.att.net"
If txtTo.Text.Length > 1 Then
e_mail = New MailMessage()
e_mail.From = self
e_mail.To.Add(txtTo.Text + Sprint) 'Spint
e_mail.Subject = txtSubject.Text
e_mail.IsBodyHtml = False
e_mail.Body = txtMessage.Text
Smtp_Server.Send(e_mail)
e_mail = New MailMessage()
e_mail.From = self
e_mail.To.Add(txtTo.Text + ATT) ' AT&T
e_mail.Subject = txtSubject.Text
e_mail.IsBodyHtml = False
e_mail.Body = txtMessage.Text
Smtp_Server.Send(e_mail)
e_mail = New MailMessage()
e_mail.From = self
e_mail.To.Add(txtTo.Text + Verizon) ' Verizon
e_mail.Subject = txtSubject.Text
e_mail.IsBodyHtml = False
e_mail.Body = txtMessage.Text
Smtp_Server.Send(e_mail)
e_mail = New MailMessage()
e_mail.From = self
e_mail.To.Add(txtTo.Text + TMobile) ' T-mobial
e_mail.Subject = txtSubject.Text
e_mail.IsBodyHtml = False
e_mail.Body = txtMessage.Text
Smtp_Server.Send(e_mail)
End If
MsgBox("Mail Sent")
Catch error_t As Exception
MsgBox(error_t.ToString)
End Try
End Sub

This won't help speed up your sending... But it does reduce your lines of code using the loop you request...
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
Dim Smtp_Server As New SmtpClient
Dim e_mail As New MailMessage()
Dim self As New MailAddress("blank#gmail.com")
Dim strCarriers as New List(of String)
Smtp_Server.UseDefaultCredentials = False
Smtp_Server.Credentials = New Net.NetworkCredential("blank#gmail.com", "password")
Smtp_Server.Port = 587
Smtp_Server.EnableSsl = True
Smtp_Server.Host = "smtp.gmail.com"
strCarriers.Add("#pm.sprint.com")
strCarriers.Add("#vtext.com")
strCarriers.Add("#tmomail.net")
strCarriers.Add("#txt.att.net")
If txtTo.Text.Length > 1 Then
For Each Carrier As String In strCarriers
e_mail = New MailMessage()
e_mail.From = self
e_mail.To.Add(txtTo.Text + Carrier)
e_mail.Subject = txtSubject.Text
e_mail.IsBodyHtml = False
e_mail.Body = txtMessage.Text
Smtp_Server.Send(e_mail)
Next
End If
MsgBox("Mail Sent")
Catch error_t As Exception
MsgBox(error_t.ToString)
End Try
End Sub
May I suggest using either Threading or Asynchronous calls to perhaps send multiple emails at the same time maybe?

Related

Sending Email with Attachment Failed(Using System.Net.Mail) without Attachment working

I have successfully sent email using my code below:
Friend Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
Dim Smtp_Server As SmtpClient
Dim e_mail As MailMessage
Smtp_Server = New SmtpClient()
Smtp_Server.UseDefaultCredentials = False
Smtp_Server.Credentials = New Net.NetworkCredential("myuserid#gmail.com", "mypassword")
Smtp_Server.EnableSsl = True
Smtp_Server.Port = 587
Smtp_Server.Host = "smtp.gmail.com"
e_mail = New MailMessage()
e_mail.From = New MailAddress(txtFrom.Text)
e_mail.To.Add(txtTo.Text)
e_mail.Subject = "Email Sending Subject"
e_mail.IsBodyHtml = False
e_mail.Body = txtMessage.Text ''Take from textbox
attachment = New System.Net.Mail.Attachment("D:\mypdfdoc.pdf")
e_mail.Attachments.Add(attachment) 'attachment
Smtp_Server.Send(e_mail)
MsgBox("Mail Sent")
Catch error_t As Exception
MsgBox(error_t.ToString)
End Try
End Sub
But when I attached a file with:
attachment = New System.Net.Mail.Attachment("D:\mypdfdoc.pdf")
e_mail.Attachments.Add(attachment) 'attachment
It throws a error:
System.Net.Mail.SmtpException: The operation has timed out. at
System.Net.Mail.SmtpClient.Send(MailMessage message)
(Without attachment it works fine by enabling less secure apps from google account)
What sort of solution is available yet.

How should I send the DataGrid Result that im using DataGridViewCellFormattingEventArgs

Below is the code for DataGridViewCellFormattingEventArgs
Public Sub DataGridView_CellFormatting(ByVal sender As Object,
ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs)
Handles DataGrid.CellFormatting
For i As Integer = 0 To dt_result.Rows.Count() - 1
If dt_result.Rows.Count() - 0 Then
DataGrid.Rows(i).DefaultCellStyle.BackColor = Color.Green
End If
Next
Dim drv As DataRowView
If e.RowIndex >= 0 Then
If e.RowIndex <= dt_result.Rows.Count - 1 Then
drv = dt_result.DefaultView.Item(e.RowIndex)
Dim c As Color
If drv.Item("Status").ToString = "Working" Then
c = Color.Green
Else
c = Color.Red
End If
e.CellStyle.BackColor = c
End If
End If
DataGrid.DataSource = dt_result
End Sub
Below is my Function for Send Mail Using SMTP
Try
'Create instance of MailMessage Class
Dim msg As New MailMessage()
'Assign From mail address
msg.From = New MailAddress("Emailaddress")
'Set To mail address
msg.[To].Add(New MailAddress("Emailadress"))
'Set Subject of mail
msg.Subject = "Subject of Mail"
'Create Mail Body
msg.Body = "Body of Mail"
msg.Body += "DataGrid Result from DataGridViewCellFormattingEventArgs" <-- Here's my Target that DataGridResult will be here
'for sending body as HTML
msg.IsBodyHtml = True
'Create Instance of SMTP Class
Dim SmtpServer As New SmtpClient("smtp.gmail.com")
'Assign Host
'SmtpServer.Host = ""
'Assign Post Number
SmtpServer.Port = 587
'Setting the credential for the sender
SmtpServer.Credentials = New System.Net.NetworkCredential("Email address", "Password")
'Enable teh Secure Soket Layer to Encrypte the connection
SmtpServer.EnableSsl = True
'Sending the message
SmtpServer.Send(msg)
LblMessage.Text = "Mail Sent Sucessfully "
Catch ex As Exception
LblMessage.Text = "Issue while sending the mail Please check."
End Try

Auto attach file on email reporting program vb.net

Here is my program for auto email with manual attachments on certain time using config.ini for email username and password
Imports System.Net.Mail
Imports System.Timers
Public Class Form1
Dim file(2) As String
Dim pesan As String
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.Text = "Water Monitoring"
Timer1.Start()
End Sub
Public Sub kirim() 'step send e-mail manual'
Try
Dim Smtp_Server As New SmtpClient
Dim e_mail As New MailMessage()
Dim txtEmail As String
Dim txtPassword As String
txtEmail = Module1.Read_INI("GENERAL", "Email")
txtPassword = Module1.Read_INI("GENERAL", "Password")
Smtp_Server.UseDefaultCredentials = False
Smtp_Server.Credentials = New Net.NetworkCredential(txtEmail, txtPassword) 'login email'
Smtp_Server.Port = 587
Smtp_Server.Timeout = 3000000
Smtp_Server.EnableSsl = True
Smtp_Server.Host = "smtp.gmail.com"
e_mail = New MailMessage()
e_mail.From = New MailAddress(txtEmail)
e_mail.To.Add(txtTo.Text)
e_mail.Subject = txtSubject.Text
e_mail.IsBodyHtml = False
e_mail.Body = pesan
If Not txtFile1.Text = Nothing Then
Dim attach As New Attachment(txtFile1.Text)
e_mail.Attachments.Add(attach) 'attach attachment 1
End If
If Not txtFile2.Text = Nothing Then
Dim attach As New Attachment(txtFile2.Text)
e_mail.Attachments.Add(attach) 'attach attachment 2
End If
If Not txtFile3.Text = Nothing Then
Dim attach As New Attachment(txtFile3.Text)
e_mail.Attachments.Add(attach) 'attach attachment 3
End If
Smtp_Server.Send(e_mail)
MsgBox("Mail Sent")
Catch error_t As Exception
MsgBox(error_t.ToString) 'message box error
End Try
End Sub
Private Sub chckboxAuto30s_CheckedChanged(sender As Object, e As EventArgs) Handles chckboxAuto30s.CheckedChanged
If chckboxAuto30s.Checked = True Then
btnSend.Visible = False
Else
btnSend.Visible = True
End If
End Sub
Private Sub txtMessage_TextChanged(sender As Object, e As EventArgs) Handles txtMessage.TextChanged
pesan = txtMessage.Text
End Sub
Private Sub btnCancelAllAttachments_Click(sender As Object, e As EventArgs) Handles btnCancelAllAttachments.Click
txtFile1.Text = ""
txtFile2.Text = ""
txtFile3.Text = ""
file = Nothing
End Sub
Private Sub btnAddAttachments_Click(sender As Object, e As EventArgs) Handles btnAddAttachments.Click
file = Nothing
OpenFileDialog1.ShowDialog()
file = OpenFileDialog1.FileNames
txtFile1.Text = file(0)
Try
txtFile2.Text = file(1)
Catch ex As IndexOutOfRangeException
End Try
Try
txtFile3.Text = file(2)
Catch ex As IndexOutOfRangeException 'attach file attachment'
End Try
End Sub
Private Sub btnSend_Click(sender As Object, e As EventArgs) Handles btnSend.Click
kirim() 'send e-mail manual'
End Sub
Private Sub btnClearText_Click(sender As Object, e As EventArgs) Handles btnClearText.Click
txtTo.Text = ""
txtSubject.Text = ""
txtMessage.Text = ""
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Dim timerforAuto As Date
timerforAuto = CDate(timeAuto.Text)
If timerforAuto.Hour = Now.Hour And timerforAuto.Minute = Now.Minute And timerforAuto.Second = Now.Second Then
kirim()
End If
End Sub
End Class
My question is, how to setting the attachments is choosed automatically? I want to attach file automatically based on current time.
For example : i want to attach
C:\testing1.xlsx
C:\testing2.xlsx
automatically. And refresh the file if the file contents in the xlsx have changed every day.
Change your kirim() call in the timer1_tick to accept a date parameter.
Public Sub kirim(TimeKickedOff as Date) 'step send e-mail manual'
Try
Dim Smtp_Server As New SmtpClient
Dim e_mail As New MailMessage()
Dim txtEmail As String
Dim txtPassword As String
txtEmail = Module1.Read_INI("GENERAL", "Email")
txtPassword = Module1.Read_INI("GENERAL", "Password")
Smtp_Server.UseDefaultCredentials = False
Smtp_Server.Credentials = New Net.NetworkCredential(txtEmail, txtPassword) 'login email'
Smtp_Server.Port = 587
Smtp_Server.Timeout = 3000000
Smtp_Server.EnableSsl = True
Smtp_Server.Host = "smtp.gmail.com"
e_mail = New MailMessage()
e_mail.From = New MailAddress(txtEmail)
e_mail.To.Add(txtTo.Text)
e_mail.Subject = txtSubject.Text
e_mail.IsBodyHtml = False
e_mail.Body = pesan
If Not txtFile1.Text = Nothing Then
Dim attach As New Attachment(txtFile1.Text)
e_mail.Attachments.Add(attach) 'attach attachment 1
End If
If Not txtFile2.Text = Nothing Then
Dim attach As New Attachment(txtFile2.Text)
e_mail.Attachments.Add(attach) 'attach attachment 2
End If
If Not txtFile3.Text = Nothing Then
Dim attach As New Attachment(txtFile3.Text)
e_mail.Attachments.Add(attach) 'attach attachment 3
End If
Smtp_Server.Send(e_mail)
MsgBox("Mail Sent")
Catch error_t As Exception
MsgBox(error_t.ToString) 'message box error
End Try
End Sub
Change the call in timer1_tick to pass the time it was kicked off to kirim.
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Dim timerforAuto As Date
timerforAuto = CDate(timeAuto.Text)
If timerforAuto.Hour = Now.Hour And timerforAuto.Minute = Now.Minute And timerforAuto.Second = Now.Second Then
kirim(timerforAuto)
End If
End Sub
In the kirim(TimeKickedOff as Date) sub add code that tests the TimeKickedOff to the datetime you want each attachment attached to the email, for example, in the kirim sub where SomeTime = the datetime you want the file attached:
If Not txtFile3.Text = Nothing Then
if TimeKickedOff.Hour = SomeTime.Hour And TimeKickedOff.Minute = SomeTime.Minute And TimeKickedOff.Second = SomeTime.Second Then
Dim attach As New Attachment(txtFile3.Text)
e_mail.Attachments.Add(attach) 'attach attachment 3
End If
End If
To test if a file has changed, you can handle this in the timer event. Dim a static variable that contains the content of the file and check to see if the content changes every so often and if the content changes do what you need to do when that happens and load the new content into the static variable so you can keep checking for newer changes.

Send e-mail through VB.NET using SMTP

I really started to get confused about sending an e-mail to (example: test#test.com) through VB.NET and for sure using SMTP.
I want the mail to be sent when Button1 is clicked (example).
So I started by a code like that
Imports System.Net.Mail
Public Class MyProgram
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Using message As New MailMessage()
'set to the from, to and subject fields
message.From = New MailAddress("sender#sender.com")
message.[To].Add(New MailAddress("test#test.com"))
message.Subject = "Test"
'code the message body
Dim MsgBody As String
MsgBody = TextBox1.Text.ToString() & vbCr & _
TextBox2.Text.ToString()
message.Body = MsgBody
Dim client As New SmtpClient()
client.Host = "smtp.example.com"
client.Port = "465"
client.EnableSsl = True
client.Credentials = New Net.NetworkCredential("USERNAME#MAIL.COM", "PASSWORD")
client.Send(message)
End Using
'display submitted box
MessageBox.Show("Congrats", "Congratulations!")
'close form
Me.Close()
End Sub
End Class
And I get:
Next, I tried another code (which is) ...
Imports System.Net.Mail
Public Class MyProgram
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Smtp_Server As New SmtpClient
Dim e_mail As New MailMessage()
Smtp_Server.UseDefaultCredentials = False
Smtp_Server.Credentials = New Net.NetworkCredential("USERNAME#MAIL.COM", "PASSWORD")
Smtp_Server.Port = 465
Smtp_Server.EnableSsl = True
Smtp_Server.Host = "smtp.example.com"
e_mail = New MailMessage()
e_mail.From = New MailAddress("sender#sender.com")
e_mail.To.Add("test#test.com")
e_mail.Subject = "Test E-mail Address Sent by My Program"
e_mail.IsBodyHtml = False
e_mail.Body = "Test"
Smtp_Server.Send(e_mail)
MsgBox("Mail Sent")
End Sub
End Class
And now I get the following in the Debugger:
And at last the e-mail isn't sent!
Please don't say "sender#sender.com" isn't configured as a sender e-mail address because it is!
I really need help!
Thanks. :-)

Reading a Database to list in Vb

The program is suppose to take the values in a specific Column and send 4 emails to each one. Each of the 4 emails is a major cell company email. The data in the list is the phone number but im having trouble implementing it. What am i doing wrong? Any suggestions?
Imports System.Net.Mail
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'ContactsDataSet.VBQuery' table. You can move, or remove it, as needed.
Me.VBQueryTableAdapter.Fill(Me.ContactsDataSet.VBQuery)
' Set the caption bar text of the form.
Me.Text = "Texting From Email"
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
Dim Smtp_Server As New SmtpClient
Dim e_mail As New MailMessage()
Dim self As New MailAddress("blank#gmail.com")
Dim strCarriers As New List(Of String)
Dim colIndex As Integer
Dim strList As New List(Of String)
Smtp_Server.UseDefaultCredentials = False
Smtp_Server.Credentials = New Net.NetworkCredential("blank#gmail.com", "password3")
Smtp_Server.Port = 587
Smtp_Server.EnableSsl = True
Smtp_Server.Host = "smtp.gmail.com"
strCarriers.Add("#pm.sprint.com")
strCarriers.Add("#vtext.com")
strCarriers.Add("#tmomail.net")
strCarriers.Add("#txt.att.net")
colIndex = 4
If rad1.Checked = True Then
Dim NumToCall As String
For i As Integer = 0 To DataGridView1.Rows.Count - 1
' dont need
'strList.Add(DataGridView1.Item(colIndex, i).Value.ToString)
NumToCall = DataGridView1.Item(colIndex, i).Value.ToString
' you cal also reference by name:
For Each item As String In strList
For Each Carrier As String In strCarriers
' Send Email
e_mail.To.Add(NumToCall & "Carrier")
e_mail = New MailMessage()
e_mail.From = self
e_mail.Subject = txtSubject.Text
e_mail.IsBodyHtml = False
e_mail.Body = txtMessage.Text
Smtp_Server.Send(e_mail)
Next
Next
Next
ElseIf rad1.Checked = False Then
For Each Carrier As String In strCarriers
e_mail = New MailMessage()
e_mail.From = self
e_mail.To.Add(txtTo.Text + Carrier)
e_mail.Subject = txtSubject.Text
e_mail.IsBodyHtml = False
e_mail.Body = txtMessage.Text
Smtp_Server.Send(e_mail)
Next
End If
MsgBox("Mail Sent")
Catch error_t As Exception
MsgBox(error_t.ToString)
End Try
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Close()
End Sub
Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
txtMessage.Text = String.Empty
txtSubject.Text = String.Empty
txtTo.Text = String.Empty
rad1.Checked = False
End Sub
End Class
You have to remove the strList ForLoop block. I guess it's not needed anymore
Dim NumToCall As String
For i As Integer = 0 To DataGridView1.Rows.Count - 1
NumToCall = DataGridView1.Item(colIndex, i).Value.ToString
'you cal also reference by name:
For Each Carrier As String In strCarriers
' Send Email
e_mail.To.Add(NumToCall & "Carrier")
e_mail = New MailMessage()
e_mail.From = self
e_mail.Subject = txtSubject.Text
e_mail.IsBodyHtml = False
e_mail.Body = txtMessage.Text
Smtp_Server.Send(e_mail)
Next
Next