Welcome to Dream.In.Code
Getting VB.NET Help is Easy!

Join 136,459 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,810 people online right now. Registration is fast and FREE... Join Now!




How to receive email's

 
Reply to this topicStart new topic

How to receive email's

musicmaniac123
28 Jul, 2008 - 12:09 PM
Post #1

New D.I.C Head
*

Joined: 28 Jun, 2008
Posts: 8

hello. im working on an application that is sort of like outlook. You will be able to set up your email account (from a internet email like gmail or aol) and it will grab the email the internet mail. I have the sending part taken care of but i need the reveiving part. If anyone would be able to help me code something to recieve email from a internet website such as gmail or aol, i would be very thankful. Because of the dream in code's policy, i will show you what i have so far. I probably should say that i am using visual basics 2008.

heres the code for sending email...

CODE

Imports System.Net.Mail
Public Class Form1

Public Sub SendEmail()
Dim client As New SmtpClient()
Dim sendTo As New MailAddress("SendingTo@gmail.com")
Dim from As MailAddress = New MailAddress("SentFrom@gmail.com")
Dim message As New MailMessage(from, sendTo)
message.IsBodyHtml = True
message.Subject = "The Subject"
message.Body = "Message's Body"

Dim basicAuthenticationInfo As New System.Net.NetworkCredential("your username", "your password")

client.Host = "smtp.gmail.com"
client.UseDefaultCredentials = False
client.Credentials = basicAuthenticationInfo
client.EnableSsl = True
Try
client.Send(message)
msgbox"Message Sent!"
Catch ex As Exception
msgbox"Message not sent."
End Try
End Sub


This works great. I just need the reveive email part.
Thanks in advance.
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: How To Receive Email's
28 Jul, 2008 - 02:14 PM
Post #2

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 8,993



Thanked: 125 times
Dream Kudos: 8625
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
Moved to VB.NET smile.gif
User is offlineProfile CardPM
+Quote Post

narmer93
RE: How To Receive Email's
28 Jul, 2008 - 04:23 PM
Post #3

D.I.C Head
**

Joined: 13 Mar, 2008
Posts: 240



Thanked: 1 times
My Contributions
well i found something in an arabic site about this but i didn't try it yet
ok i will translate
first u have to get a gmail to be able to send with it cuz it have smtp
second type these
vb

imports system.net.mail
imports system.net.mail.mailmessage
imports system.net.networkcredential

then u type the code
vb

dim mail as new mailmessage
dim smtp as new smtpclient
mail.form=new mailadress("'your gmail account'","'your name or something'")
mail.to.add("'mail u want the message to be sent to'")
mail.subject="'subject'"
mail.body="the message u want to send'"
mail.isbodyhtml=true'u can disable it if u don't want the mail to contain html'
mail.reply to=new mailadree("'i gues there is something wrong in this code'")
'this part i am not sure of it but u can try'
smtp.host="smtp.gmail.com"
smtp.port=25
smtp.enable ssl=true
smtp.credentials=new system.net.networkcredential("'your email'","'your email password'"'or the opposit i think')
smtp.send('mail')
msgbox("your message has been sent")

or a short way

vb

dim smtp as new net.mail.smtpclient("'smtp.domail.com'")
dim msg as new net.mail.mailmessage('this area contains your email,the email u want to be sent to ,subject,messaege and don't worry cuz the vb.net will tell u what are they and u will have to leave a comma between each')
smtp.send('msg')

and here is an explanation
CODE

smtp.domain.com=smtp server adress

and of course lots of things will be replaced by textboxes

hey wait it is working well with u?
so u want the part so u will recieve mail from ur project?
i have no idea about that
but i am glad u made this topic,i was going to make one soon cuz my code wasb't working well
i hope this was helpful

User is online!Profile CardPM
+Quote Post

musicmaniac123
RE: How To Receive Email's
28 Jul, 2008 - 05:21 PM
Post #4

New D.I.C Head
*

Joined: 28 Jun, 2008
Posts: 8

I want it so that it will be able to take the mail that you received from a different person. It will show up in your form. When you double click on it, you will be able to open it. just like outlook, or gmail, or aim. And yea. i was looking for the receive part. Thanks though. And actually, my send mail part works great. All you need is a button or something ( you could even add it in the sendemail() part after "Try" . you would have me.sendmail and it works just fine. If you can figure out how to receive mail, please tell me. Thanks a lot!
User is offlineProfile CardPM
+Quote Post

narmer93
RE: How To Receive Email's
28 Jul, 2008 - 06:08 PM
Post #5

D.I.C Head
**

Joined: 13 Mar, 2008
Posts: 240



Thanked: 1 times
My Contributions
well i don't think i can help u with this cuz the code u gave didn't work for me and i see the message"message not sent"
so what is wrong?
may be it is something in this area
client.Host = "smtp.gmail.com"
what should be typed there then?
User is online!Profile CardPM
+Quote Post

musicmaniac123
RE: How To Receive Email's
28 Jul, 2008 - 06:26 PM
Post #6

New D.I.C Head
*

Joined: 28 Jun, 2008
Posts: 8

i think i know what you need to do.
everything is alright with the "smtp.gmail.com"
you just need to add @gmail.com to your username
Dim basicAuthenticationInfo As New System.Net.NetworkCredential("YourGmailUsername@gmail.com", "xxxxxxxxx your password")

i think everything should be fine then.
if not, ill try something else then.
User is offlineProfile CardPM
+Quote Post

narmer93
RE: How To Receive Email's
28 Jul, 2008 - 06:48 PM
Post #7

D.I.C Head
**

Joined: 13 Mar, 2008
Posts: 240



Thanked: 1 times
My Contributions
well the final code is this
vb
Imports System.Net.Mail
Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim client As New SmtpClient()
Dim sendTo As New MailAddress("mbkfa93@gmail.com")
Dim from As MailAddress = New MailAddress("mbkfa93@gmail.com")
Dim message As New MailMessage("mbkfa93@gmail.com", "mbkfa93@gmail.com")
message.IsBodyHtml = True
message.Subject = "subject"
message.Body = Me.TextBox1.Text
Dim basicAuthenticationInfo As New System.Net.NetworkCredential("mbkfa93@gmail.com", "my email password")

client.Host = "smtp.gmail.com"
client.UseDefaultCredentials = False
client.Credentials = basicAuthenticationInfo
client.EnableSsl = True
Try
client.Send(message)
MsgBox("Message Sent!")
Catch ex As Exception
MsgBox("Message not sent.")
End Try

End Sub
End Class

and now i still get the "message not sent message"
what is the problem now then?
User is online!Profile CardPM
+Quote Post

musicmaniac123
RE: How To Receive Email's
28 Jul, 2008 - 07:03 PM
Post #8

New D.I.C Head
*

Joined: 28 Jun, 2008
Posts: 8

CODE

Imports System.Net.Mail
Public Class Form1
    Public Sub SendEmail()
        Dim client As New SmtpClient()
        Dim sendTo As New MailAddress("musicmaniac108@gmail.com")
        Dim from As MailAddress = New MailAddress("musicmaniac108@gmail.com")
        Dim message As New MailMessage(from, sendTo)
        message.IsBodyHtml = True
        message.Subject = "The Subject"
        message.Body = TextBox1.Text

        Dim basicAuthenticationInfo As New System.Net.NetworkCredential("musicmaniac108@gmail.com", "passwrd")

        client.Host = "smtp.gmail.com"
        client.UseDefaultCredentials = False
        client.Credentials = basicAuthenticationInfo
        client.EnableSsl = True
        Try
            client.Send(message)
            MsgBox("Message Sent")
        Catch ex As Exception
            MsgBox("Message not sent.")
        End Try
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.SendEmail()
    End Sub
end class



dont change anything except your username and the email stuff. the ovbious. lol
reply and tell me if it works
User is offlineProfile CardPM
+Quote Post

Jayman
RE: How To Receive Email's
28 Jul, 2008 - 08:46 PM
Post #9

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,941



Thanked: 42 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
I'm sorry to say that the .Mail namespace can only be used for sending emails, not receiving.

You will need to look into using Microsoft.Office.Interrop.Outlook Namespace.

However, perhaps this link will provide you with what you need.
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: How To Receive Email's
28 Jul, 2008 - 09:44 PM
Post #10

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 8,993



Thanked: 125 times
Dream Kudos: 8625
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
You will need to look into using the TcpClient() class of the System.Net.Sockets class. Here is some sample code that should at least get you down the right path:


vb

Dim tcpC As New System.Net.Sockets.TcpClient()

' send command strings and return the response
Function SendCommand(ByRef netstream As System.Net.Sockets.NetworkStream, ByVal sToSend As String)
Dim bData() As Byte = Encoding.ASCII.GetBytes(sToSend.ToCharArray)
netstream.Write(bData, 0, bData.Length())
Return GetResponse(netstream)
End Function

' check if there is a response to get and return it
Function GetResponse(ByRef netstream As System.Net.Sockets.NetworkStream)
Dim bytes(tcpC.ReceiveBufferSize) As Byte
Dim ret As Integer = netstream.Read(bytes, 0, bytes.Length)
' Returns the data received
Dim returndata As String = Encoding.ASCII.GetString(bytes)
Return returndata
End Function



Function ReadMail(ByVal host As String, ByVal user As String, ByVal pass As String)
' initialise objects
Dim netstream As System.Net.Sockets.NetworkStream
Dim thisResponse As String

' open connection to server
Try
tcpC.Connect(host, 110)
Catch ex As Exception
Response.Write("Error connecting to host: " & ex.Message & " - Please check your details and try again")
Response.End()
End Try

' get response
netstream = tcpC.GetStream()
thisResponse = GetResponse(netstream)

' enter user name
thisResponse = SendCommand(netstream, "user " & user & vbCrLf)

' enter password
thisResponse = SendCommand(netstream, "pass " & pass & vbCrLf)

' check if logged in ok
If Not Left(thisResponse, 4) = "-ERR" Then
Response.Write("<font face=courier>Logged in OK <BR>")
Else
Response.Write("Error logging in, check your user details and try again<BR>")
Response.Write("<P>" & thisResponse)
Response.End()
End If

thisResponse = SendCommand(netstream, "stat" & vbCrLf)

Dim tmpArray() As String
tmpArray = Split(thisResponse, " ")

Dim thisMess As Integer
Dim numMess As String = tmpArray(1)

Response.Write("<p><hr>")

thisResponse = ""

If CInt(numMess) > 0 Then
Response.Write("Messages: " & numMess & "<br>")
For thisMess = 1 To CInt(numMess)
thisResponse += Replace(SendCommand(netstream, "top " & _
thisMess & " 10" & vbCrLf), vbCrLf, "<br>")
Next
Else
Response.Write("Messages: None" & "<br>")
End If

thisResponse += Replace(SendCommand(netstream, "stat" & vbCrLf), _
vbCrLf, "<br>")

tmpArray = Split(thisResponse, "+OK")

Response.Write(thisResponse)

Dim msg As Integer
For msg = 1 To tmpArray.Length - 1
Response.Write("<h3>#" & msg & "</h1>" & tmpArray(msg) & "<p>")
Next

' close connection
thisResponse = SendCommand(netstream, "QUIT" & vbCrLf)
tcpC.Close()
End Function



Hope that helps you some, and gets you down the right path smile.gif
User is offlineProfile CardPM
+Quote Post

narmer93
RE: How To Receive Email's
29 Jul, 2008 - 08:59 AM
Post #11

D.I.C Head
**

Joined: 13 Mar, 2008
Posts: 240



Thanked: 1 times
My Contributions
i did as u said musicmania and here is my final code but still the same trouble,message not sent
vb

Imports System.Net.Mail
Public Class Form1
Public Sub SendEmail()
Dim client As New SmtpClient()
Dim sendTo As New MailAddress("mbkfa93@gmail.com", "meena")
Dim from As MailAddress = New MailAddress("mbkfa93@gmail.com")
Dim message As New MailMessage(from, sendTo)
message.IsBodyHtml = True
message.Subject = "The Subject"
message.Body = TextBox1.Text

Dim basicAuthenticationInfo As New System.Net.NetworkCredential("mbkfa93@gmail.com", "my password")

client.Host = "smtp.gmail.com"
client.UseDefaultCredentials = False
client.Credentials = basicAuthenticationInfo
client.EnableSsl = True
Try
client.Send(message)
MsgBox("Message Sent")
Catch ex As Exception
MsgBox("Message not sent.")
End Try
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.SendEmail()
End Sub
End Class

and if u want to make the code vb use
code=vb instead of code
and they both end with /code
User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 04:02PM

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month