i already know how to send a email using vb.net, however i cant figure out how to receive an email. I want to go into my email account and read a email and store it in a variable, is this possible? and if yes could any one give me some sample code, because that would be great.
just to get a good idea of what I'm using here's my code for sending a email,
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("trowbridge97#gmail.com", "qwerty1205")
SmtpServer.Port = 587
SmtpServer.Host = "smtp.gmail.com"
mail = New MailMessage()
mail.From = New MailAddress("trowbridge97#gmail.com")
mail.To.Add("trowbridge97#gmail.com")
mail.Subject = "Alarm"
mail.Body = "Some one is in your room"
SmtpServer.Send(mail)
MsgBox("mail send")
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
SMTP is used for sending email, not receiving it.
You need a POP3 or IMAP client. There are many commercial and free .NET libraries for both protocols.
Related
Hello and greetings to all, I need some help. Below is my following code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Mail As New MailMessage
Dim SMTP As New SmtpClient("smtp.gmail.com")
Mail.Subject = "Security Update"
Mail.From = New MailAddress("XXXX#gmail.com")
SMTP.Credentials = New System.Net.NetworkCredential("XXXX#gmail.com", "XXXX")
Mail.To.Add("XXX#gmail.com")
Mail.Body = "Test"
SMTP.EnableSsl = True
SMTP.Port = "587"
SMTP.Send(Mail)
End Sub
However, I get the following Error When Ran: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.
The Password Is The Same To Login and is correct. What am I doing wrong? Also, I have a rich text box that I would like to be the body of the E-mail. I do not know how to do this. Please help.
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(My.Settings.smtpserver)
mail.From = New MailAddress(My.Settings.from)
mail.[To].Add(My.Settings.destination)
mail.Subject = "Program"
mail.Body = "Hi this is a msg form .net application"
Dim path As String = My.Settings.desktop
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 = My.Settings.port
SmtpServer.Credentials = New
System.Net.NetworkCredential(My.Settings.username, My.Settings.password)
SmtpServer.EnableSsl = True
SmtpServer.Send(mail)
MsgBox("Sent Successfuly!", MsgBoxStyle.Information, "Send!")
mail.CC.Add(My.Settings.CC)
mail.Bcc.Add(My.Settings.BCC)
Catch ex As Exception
MsgBox("Failed Sending Email!", MsgBoxStyle.Critical, "Failed!")
End Try
Application.Exit()
End Sub
End Class
the cc and bcc is not working i cant send through cc nor bcc is there any problem with my codes? the user can use this cc and bcc like gmail.
Well - you are setting CC and BCC after you send the email. Thats your problem. Move the lines:
mail.CC.Add(My.Settings.CC)
mail.Bcc.Add(My.Settings.BCC)
Up to where you are setting Mail.Subject, etc.
This is a code snippet for sending email using vb.net:
Imports System.Net.Mail
Public Class Form1
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("username#gmail.com", "password")
SmtpServer.Port = 587
SmtpServer.Host = "smtp.gmail.com"
mail = New MailMessage()
mail.From = New MailAddress("YOURusername#gmail.com")
mail.To.Add("TOADDRESS")
mail.Subject = "Test Mail"
mail.Body = "This is for testing SMTP mail from GMAIL"
SmtpServer.Send(mail)
MsgBox("mail send")
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
End Class
Can I add CC and BCC address too? If yes, what are the possible codes?
e_mail.CC.Add("user#domain.com")
e_mail.Bcc.Add("user#domain.com")
Got it from myself :)
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.
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