Can anyone help me. I've created a email program. I'm having an error when clicking on the send button. Here's my code:
Imports System.Net.Mail
Public Class frmEmail
Dim emailaddress As String
Dim password As String
Dim port As Integer
Dim host As String
Private Sub frmEmail_Load(sender As Object, e As EventArgs)
If emailaddress.ToLower.Contains("#gmail") Then
port = 587
host = "smtp.gmail.com"
ElseIf emailaddress.ToLower.Contains("#yahoo") Then
port = 465
host = "smtp.mail.yahoo.com"
End If
End Sub
Private Sub btnSend_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSend.Click
Try
Dim smtpServer As New SmtpClient
Dim mail As New MailMessage()
smtpServer.Credentials = New Net.NetworkCredential(emailaddress, password)
smtpServer.Port = port
smtpServer.Host = host
smtpServer.EnableSsl = True
mail = New MailMessage()
mail.From = New MailAddress(emailaddress)
mail.To.Add(txtTo.Text)
mail.Subject = txtSubject.Text
mail.Body = txtMessage.Text
smtpServer.Send(mail)
MsgBox("The mail is send!", MsgBoxStyle.Information)
Catch error_t As Exception
MsgBox(error_t.ToString)
End Try
End Sub
End Class
And I've got this error: System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name:value
at System.Net.Mail.SmtpClient.set_Port(Int32 value)
***Edited forgot to modify it to the original code
Your sub
Private Sub frmEmail_Load(sender As Object, e As EventArgs)
doesnt run when the form loads.
Because of this port is set to the default value of 0
You need to tell VB.Net to run when the form loads like this
Private Sub frmEmail_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Also further on down the code, you might find that you get an error about authentication from the email server. If you get this, just below
Dim smtpServer As New SmtpClient
add this
smtpServer.UseDefaultCredentials = False
it might work.
Related
This is the error I'm getting when Me.Close() is called:
This is my code:
Imports System.Net.Mail
Imports System.IO
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Try
Dim mail As New MailMessage()
Dim SmtpServer As New SmtpClient("smtp.gmail.com")
mail.From = New MailAddress("your email#domain.com")
mail.[To].Add("to#domain.com")
mail.Subject = "Trial"
mail.Body = "Trial"
Dim path As String = "C:\Users\CrystalUser\Desktop\trial"
Dim dir As New DirectoryInfo(path)
Dim filesInDirectory As FileInfo() = dir.GetFiles()
Dim attach As System.Net.Mail.Attachment
For Each file In filesInDirectory
attach = New System.Net.Mail.Attachment(file.FullName)
mail.Attachments.Add(attach)
Next
SmtpServer.Port = 587
SmtpServer.Credentials = New System.Net.NetworkCredential("username", "password")
SmtpServer.EnableSsl = True
SmtpServer.Send(mail)
MsgBox("Sent Successfuly", MsgBoxStyle.Information, "Send")
Catch alvin As Exception
MessageBox.Show(alvin.Message)
End Try
Me.Close()
End Sub
End Class
I want the program to close automatically after sending the MailMessage.
I have used Me.Hide() and Me.Close() but neither worked.
Although your error is little unclear and this answer may not fix your problem it would be wise to implement Using on the MailMessage, SmtpClient and Attachment classes:
Sometimes your code requires an unmanaged resource, such as a file handle, a COM wrapper, or a SQL connection. A Using block guarantees the disposal of one or more such resources when your code is finished with them. This makes them available for other code to use.
I would also look at reducing some unnecessary code when using .GetFiles().
Code:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Try
Using mail As New MailMessage("your email#domain.com", "to#domain.com", "Trial", "Trial"),
smtpServer As New SmtpClient("smtp.gmail.com", 587)
Dim filesInDirectory As FileInfo() = New DirectoryInfo("C:\Users\CrystalUser\Desktop\trial").GetFiles()
For Each file In filesInDirectory
Using attach As New Attachment(file.FullName)
mail.Attachments.Add(attach)
End Using
Next
smtpServer.Credentials = New System.Net.NetworkCredential("username", "password")
smtpServer.EnableSsl = True
smtpServer.Send(mail)
MessageBox.Show("Mail sent")
End Using
Catch alvin As Exception
MessageBox.Show(alvin.Message)
End Try
Me.Close()
End Sub
The point of implementing Using is so that the objects are disposed of properly once finished with. The objects will also be disposed of before entering the Catch statement, should any errors occur.
Give this a go and let me know how you get on.
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. :-)
I am trying to make a system where if a certain value/text is in a label from an internet source then the computer sends me an email this is the code that i have got up to so far if any is incorrect or if you would be so kind as to retype out a new code for me so this works it would be marvellous! Thanks again your help is much appreciated as it is my GCSE project for IT. My code:
Imports System.Net.Mail
Public Class form2
Private Sub avsl_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
If itemstock1.Text = "low" Then
Try
Dim emailmessage As New MailMessage
emailmessage.From = New MailAddress("xxxxx.yyyyyy#outlook.com")
emailmessage.To.Add("xxxx.zzzz#icloud.com")
emailmessage.Subject = ("guitar hanger 180.175")
emailmessage.Body = itemstock1.Text
Dim SMTP As New SmtpClient("smtp.live.com")
SMTP.Port = 587
SMTP.EnableSsl = True
SMTP.Credentials = New System.Net.NetworkCredential("zxzxz.yyy#outlook.com", "(my password)")
SMTP.Send(emailmessage)
Catch ex As Exception
End Try
ElseIf itemstock1.Text = "low" Then
Me.Show()
End If
End Sub
End Class
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
Try
Dim SmtpServer As New SmtpClient()
Dim mail As New MailMessage()
SmtpServer.Credentials = New Net.NetworkCredential("gahlotprayank#yahoo.com", "*******")
SmtpServer.Port = 465
SmtpServer.Host = "smtp.mail.yahoo.com"
mail = New MailMessage()
mail.From = New MailAddress("gahlotprayank#yahoo.com")
mail.To.Add("rebelme23#gmail.com")
mail.Subject = TextBox1.Text
mail.Body = TextBox2.Text
SmtpServer.Send(mail)
MsgBox("ok!")
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
i tried the above code but got the error saying "timeout" and couldn't mail the textbox data
The time out is likely server related and has nothing to do with your text box value. Are you sure that you have permission to use yahoo's mail server to send email?
Try using "smtp.gmail.com" port: 587
Then enable the "access for less secure apps" option of your email account
'Variable which will send the mail
Dim obj As System.Net.Mail.SmtpClient
'Variable to store the attachments
Dim Attachment As System.Net.Mail.Attachment
'Variable to create the message to send
Dim Mailmsg As New Mail.MailMessage()
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Dim ol As New Outlook.Application()
Dim ns As Outlook.NameSpace
Dim fdMail As Outlook.MAPIFolder
ns = ol.GetNamespace("MAPI")
ns.Logon(, , True, True)
'creating a new MailItem object
Dim newMail As Outlook.MailItem
'gets defaultfolder for my Outlook Outbox
fdMail = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderOutbox)
'assign values to the newMail MailItem
newMail = fdMail.Items.Add(Outlook.OlItemType.olMailItem)
newMail.Subject = "tesst"
newMail.Body = "test"
newMail.To = TextBox1.Text
Dim sSource As String = Application.StartupPath + "\kk.sys"
' TODO: Replace with attachment name
Dim sDisplayName As String = "kaar.jpg"
Dim sBodyLen As String = newMail.Body.Length
newMail.SaveSentMessageFolder = fdMail
newMail.Send()
Catch ex As Exception
Using writer As StreamWriter = New StreamWriter(Application.StartupPath + "\err1.txt")
writer.WriteLine(ex.ToString)
End Using
End Try
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Try
Dim SmtpServer As New SmtpClient()
Dim mail As New MailMessage()
Dim address As New MailAddress(TextBox1.Text, "Nigraan")
Dim oAttch As Mail.Attachment = New Mail.Attachment(Application.StartupPath + "\kk.sys")
SmtpServer.Credentials = New _
Net.NetworkCredential(TextBox2.Text, TextBox3.Text)
SmtpServer.Port = "587"
SmtpServer.Host = "smtp.gmail.com"
mail = New MailMessage()
mail.From = New MailAddress(TextBox2.Text)
mail.To.Add(New MailAddress(TextBox1.Text))
mail.Subject = TextBox3.Text
mail.Body = "test"
mail.Attachments.Add(oAttch)
SmtpServer.Send(mail)
Catch ex As Exception
Using writer As StreamWriter = New StreamWriter(Application.StartupPath + "\err2.txt")
writer.WriteLine(ex.ToString)
End Using
End Try
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Try
System.Diagnostics.Process.Start("mailto:" & TextBox1.Text & "?subject=" & "re:Subject" & "&body=" & "EmailBody")
Catch ex As Exception
Using writer As StreamWriter = New StreamWriter(Application.StartupPath + "\err3.txt")
writer.WriteLine(ex.ToString)
End Using
End Try
End Sub`
errors are:
err1:
System.Runtime.InteropServices.COMException (0x80004005): There must be at least one name or distribution list in the To, Cc, or Bcc box.
at Microsoft.Office.Interop.Outlook._MailItem.Send()
at WindowsApplication1.Form1.Button1_Click(Object sender, EventArgs e)
err2:
System.ArgumentException: The parameter 'address' cannot be an empty string.
Parameter name: address
at System.Net.Mail.MailAddress..ctor(String address, String displayName, Encoding displayNameEncoding)
at WindowsApplication1.Form1.Button2_Click(Object sender, EventArgs e)
When i send using a machine with visual studio both mail gets sent, when not these errors show.
i have double checked .net framework
thank you..
Try creating a variable string and set that before sending the mail
Dim ToEmail as string
ToEmail = Textbox1.text
Then set your to address first.
'assign values to the newMail MailItem
newMail.To = ToEmail
newMail = fdMail.Items.Add(Outlook.OlItemType.olMailItem)
newMail.Subject = "tesst"
newMail.Body = "test"
i got everything working,
got smtp to work by giving ssl encryption to true
got outlook to work by creating a contact and giving the contact's email id in the 'to' field
just don't save the contact if u don't want the contact to be added in outlook :D
yeyyy!!