How to send e-mail?
As the question, how to send e-mail?
I don't know what's the code for send e-mail. Can anyone teach me how to?
I have a button on the Menu Tool. I want when user presses that button, it will directly link to the Microsoft Office to send e-mail to me.
Please teach me.
By the way, I also want to know how to load a text file. I want to make another button on the menu tool so when user presses it, a text file will pop up.
Thank you
Hello.
I don't speak english so much, but I'll say all I know with VS 2005.
I know how to send a email form your application and not form MS Office.
With the class System.Net.Mail.MailMessage, you put: to, from, subject, cc, bcc, Body.
With the class System.Net.Mail.Attachment, you use Mailmessage.Attachments.add(Attachment).
With the class System.Net.Mail.SmtpClient, you use SmtpClient.Send(MailMessage).
To read a text file: Create a System.IO.StreamReader object with the keyword 'New' and the path.
Create a String.
Asign the string the value StreamReader.ReadToEnd()
Then execute the StreamReader.Close() method.
That's all.
i am sry but it is a readonly prop.?
'-START OF CODE
Public Function OpenEmail(ByVal EmailAddress As String, _
ByVal FromAddress As String, _
Optional ByVal CopyTo As String = "", _
Optional ByVal BackupCopyTo As String = "", _
Optional ByVal Subject As String = "", _
Optional ByVal Body As String = "") As Boolean
CopyTo = TextBox3.Text 'OPTIONAL, actually the CARBON COPY CC: field.
BackupCopyTo = TextBox4.Text 'OPTIONAL, actually the BLIND COPY TO BCC: field.
Subject = TextBox5.Text
Body = FromAddress & vbCrLf & vbCrLf & TextBox6.Text
If EmailAddress = "" Then
MsgBox("Please enter a valid TO email address.", _
MsgBoxStyle.OKOnly, "Please ENTER the TO: email address and click the SEND button.")
Exit Function
End If
If FromAddress = "" Then
MsgBox("Please enter a valid FROM email address.", _
MsgBoxStyle.OKOnly, "Please ENTER your email address and click the SEND button.")
Exit Function
End If
Dim bAns As Boolean = True
Dim sParams As String
sParams = EmailAddress
sParams = "mailto:" & sParams
If Subject <> "" Then sParams = sParams & _
"?subject=" & Subject
'-Note i could not get CC:
and BCC: parameters to work for some reason
'so they are commented out.
'If CopyTo <> "" Then sParams = sParams & _
'"?CC:=" & CopyTo
'If BackupCopyTo <> "" Then sParams = sParams & _
'"?BCC:=" & BackupCopyTo
sParams = sParams & IIf(Subject = "", "?", "&")
sParams = sParams & "body=" & "From: " & Body
Try
System.Diagnostics.Process.Start(sParams)
Catch
bAns = False
End Try
Return bAns
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'From: address is TextBox1.text
'To: address is Textbox2.text
'CC: address is Textbox3.text
'BCC: address is Textbox4.text
'SUBJECT is Textbox5.text
'BODY of email is TEXTBOX6.text
OpenEmail(TextBox2.Text, TextBox1.Text)
End Sub
'-END OF CODE-
Regards,
S_DS
Since you are so EXPERT about sending email, can you tell me why after writing the right-code , I get a message sayig :
'Impossible to read from the transport connection: net_io_conectionclosed !'
Can you tell me how to open that connection( that ir says it is closed).
HELP please !
Hi there!
I saw that someone already provided an answer about this topic...but i want to show you an alternative.
In the code behind file do the following:
Imports System.Net
Imports System.Net.Mail
Private Sub SendMessageButton_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) _
Handles SendMessageButton.Click
''Disable the button
Me.SendMessageButton.IsEnabled = False
'Create the message
Dim fromPerson As String = Me.FromPersonTextBox.Text
Dim toPerson As String = Me.ToPersonTextBox.Text
Dim MessageSubject As String = Me.MessageSubjectTextBox.Text
Dim MessageContent As String = Me.MessageBodyTextBox.Text
'This is the SMTP Server - you should replace it with your own provider
Dim TheHost As New SmtpClient("mail.upcnet.ro")
Try
''This is the message
Dim TheMessage As New MailMessage(fromPerson, toPerson, MessageSubject, MessageContent)
If (Me.FromPersonTextBox.Text = "" OrElse Me.ToPersonTextBox.Text = "") Then
MessageBox.Show("Error! - Fields cannot be empty", "Error", MessageBoxButton.OK, MessageBoxImage.Error)
Else
''Send the message
TheHost.Send(TheMessage)
''Display message delivery notification
MessageBox.Show("Your message has been sent", "Validation", MessageBoxButton.OK, MessageBoxImage.Information)
''clear the form
fromPerson = ""
toPerson = ""
MessageSubject = ""
MessageContent = ""
Me.SendMessageButton.IsEnabled = True
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
Me.SendMessageButton.IsEnabled = True
End Try
End Sub
I hope this example will clarify your problem. [let me know if so]
Here is a good example on how to send emails using code without the office. Hope this helps.
Imports System.Net.Mail
Module Module1
Public Sub mySendEmail(ByVal myfrom As String, ByVal myTo As String, ByVal mySub As String, ByVal myMessage As String)
Dim oMsg As New MailMessage
Dim myCredentials As New System.Net.NetworkCredential
myCredentials.UserName = "myemail@something.com"
myCredentials.Password = "password"oMsg.IsBodyHtml = False
' TODO: Replace with the name of your remote SMTP server.
Dim mySmtpsvr As New SmtpClient()
mySmtpsvr.Host = "internetproviderserveraddress"
mySmtpsvr.Port = 25
mySmtpsvr.UseDefaultCredentials = False
mySmtpsvr.Credentials = myCredentials
Try
oMsg.From = New MailAddress(myfrom)
oMsg.To.Add(myTo)
oMsg.Subject = mySub
oMsg.Body = myMessage
mySmtpsvr.Send(oMsg)
MessageBox.Show("email send")
Catch ex As Exception
MessageBox.Show(Err.Number & Err.Description & ex.Message & ex.StackTrace.ToString)
End Try
End Sub
End Module
Hi,
it is very example for sending email in VB.net. Actually, I would like to send a complex document, such as word document, I will use my contact personal information to insert them into my document, also I would like to put my document in the email body, anybody knows how to do that?
Thank you very much!
You probably need a .doc SDK or something for reading it to plain text, theres too much formatting to just ReadAllText and send the result. You probably want to use some pre-done code, check out attachment_doc:
http://sourceforge.net/projects/attachment-doc/with some slight tweaks, your ready to go!
EDIT: Probably more tweaks than a little...
Hi,
This is the exact code that I need, but I am getting the following message when I run it.
System.Net.Mail.SmtpException was caught
Message="Failure sending mail."
Source="System"
StackTrace:
at System.Net.Mail.SmtpClient.Send(MailMessage message)
at BTSXML.Form1.mySendEmail(String myfrom, String myTo, String mySub, String myMessage) in S:\FXE_MED\EONFileGenerator\Form1.vb:line 736
with
{"Unable to connect to the remote server"}
message
Sorry - sorted the problem.
My virus scanner was blocking traffic from port 25 to my email program only.
Thanks
Flip
Hello,
I was looking into your code and it works fine if you know the users smtp-server. But what if you don't know it?
Regards
Bart
You could add a textbox for the user/pass and server/port. On run time, .net mail will use this to connect/send.
Hi , I am new to vb , and I just saw your thread. I am facing a similar problem. My front end is however in visual basic ver 5.00, and my database 9s access 2003. i need a pgm which attaches a generated report from the application and send it by email as an attach ment on a weekly basis. This process should run in the background, without the knowledge of end use . Please help