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!
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.
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
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
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!
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?
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.
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?
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
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)
' 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
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