|
-
Feb 12th, 2007, 11:28 AM
#1
Thread Starter
Addicted Member
[RESOLVED] [2005] E-Mailing?
I'm trying to make an e-mailprogram and got this code:
VB Code:
'create the mail message
Dim mail As New System.Net.Mail.MailMessage()
'set the addresses
'set the content
mail.Subject = "This is an email"
mail.Body = "this is the body content of the email."
'send the message
Dim smtp As New System.Net.Mail.SmtpClient("127.0.0.1")
smtp.Send(mail)
But can he e-mail from 127.0.0.1 (wich is my computer -> localhost) and what is the strange error he gives for "SmtpException was unhandled" What does that mean?
Cheers,
Thomas
Last edited by Mindstorms; Feb 16th, 2007 at 11:17 AM.
-
Feb 12th, 2007, 11:35 AM
#2
Re: [2005] E-Mailing?
You need to have a email server running your pc.
"The dark side clouds everything. Impossible to see the future is."
-
Feb 12th, 2007, 11:37 AM
#3
Thread Starter
Addicted Member
Re: [2005] E-Mailing?
Ok, but I haven't, so what can I do now? Is there another option to mail?
-
Feb 12th, 2007, 11:38 AM
#4
Re: [2005] E-Mailing?
If you put your code in a Try/Catch block it might give you more exact information as to what the error is:
VB Code:
Try
'Your existing code#
Catch ex As Exception
Messagebox.Show(ex.Message)
End Try
This wont solve it but may give you more info.
EDIT: Or someone just might know straight away.
-
Feb 12th, 2007, 11:45 AM
#5
Thread Starter
Addicted Member
Re: [2005] E-Mailing?
Ok, in Dutch it is "Fout bij het verzenden van e-mail" in English: "Error at sending e-mail"
-
Feb 12th, 2007, 11:49 AM
#6
Re: [2005] E-Mailing?
If your computer is part of a domain you can use the smpt server from the domain.
"The dark side clouds everything. Impossible to see the future is."
-
Feb 12th, 2007, 11:50 AM
#7
Thread Starter
Addicted Member
Re: [2005] E-Mailing?
No, but it's in a network of some computers at home...
-
Feb 12th, 2007, 11:52 AM
#8
Re: [2005] E-Mailing?
You need qmail or ms exchange installed on your computer to send email.
"The dark side clouds everything. Impossible to see the future is."
-
Feb 12th, 2007, 11:54 AM
#9
Thread Starter
Addicted Member
Re: [2005] E-Mailing?
Ok, but there is not another option of sending e-mail?
-
Feb 12th, 2007, 11:56 AM
#10
Re: [2005] E-Mailing?
Doesn't your isp provides your smtp server ? It sould..
"The dark side clouds everything. Impossible to see the future is."
-
Feb 12th, 2007, 11:58 AM
#11
Thread Starter
Addicted Member
-
Feb 12th, 2007, 12:01 PM
#12
Thread Starter
Addicted Member
Re: [2005] E-Mailing?
Gotcha! Thank you all very much for you're very fast help!
-
Feb 14th, 2007, 09:51 AM
#13
Thread Starter
Addicted Member
Re: [RESOLVED] [2005] E-Mailing?
I'm now trying to mail an attachment, but the attatchment I've got is "System.Windows.Forms.OpenFileDialog_ Title_ FileName_"
VB Code:
Public Class Form1
Dim Stoppen
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'create the mail message
Dim mail As New System.Net.Mail.MailMessage()
Dim Van, Naar, Onderwerp, Tekst, Slaap, Host, Cc, Bijlage_1
Van = TextBox1.Text
Naar = TextBox2.Text
Onderwerp = TextBox3.Text
Tekst = TextBox4.Text
Slaap = TextBox6.Text
Host = TextBox7.Text
Cc = TextBox8.Text
Dim Bijlage As String
Bijlage = OpenFileDialog1.ToString
OpenFileDialog1.ShowDialog()
OpenFileDialog1.ToString()
Bijlage_1 = System.Net.Mail.Attachment.CreateAttachmentFromString(Bijlage, Bijlage)
'set the addresses
mail.From = New System.Net.Mail.MailAddress(Van)
mail.To.Add(Naar)
mail.CC.Add(Cc)
'set the content
mail.Subject = Onderwerp
mail.Body = Tekst
mail.Attachments.Add(Bijlage_1)
'send the message
Dim smtp As New System.Net.Mail.SmtpClient(Host)
'//CURSOR
Me.Cursor = Cursors.WaitCursor
smtp.Send(mail)
Me.Cursor = Cursors.Default
pBar1.Visible = False
MsgBox("Operatie geslaagd!")
End Sub
End Class
(It should be HOI.txt from C:\)
Last edited by Mindstorms; Feb 14th, 2007 at 09:57 AM.
-
Feb 14th, 2007, 10:00 AM
#14
Re: [2005] E-Mailing?
It's because of the way you are assigning your variable a value. You shouldn't be using ToString like that.
Here, this will help give you an idea of what you should do:
VB Code:
With OpenFileDialog1
.Filter = ".txt | *.txt"
.InitialDirectory = "C:\"
.ShowDialog()
End With
Dim myFile As String = OpenFileDialog1.FileName 'Assign selected filename to var
MessageBox.Show(myFile) 'It gives full path
EDIT: I should warn you though that you will want to check that the user has clicked OK and not Cancel on the dialogbox.
VB Code:
If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
'Assign file name to variable etc...
End If
May also want to add a few other checks too to make sure everything is okay (use try/Catch).
Last edited by stimbo; Feb 14th, 2007 at 10:15 AM.
-
Feb 14th, 2007, 11:32 AM
#15
Thread Starter
Addicted Member
Re: [2005] E-Mailing?
Well, I've got this now:
VB Code:
Public Class Form1
Dim Stoppen
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'create the mail message
Dim mail As New System.Net.Mail.MailMessage()
Dim Van, Naar, Onderwerp, Tekst, Slaap, Host, Cc, Bijlage_1
Van = TextBox1.Text
Naar = TextBox2.Text
Onderwerp = TextBox3.Text
Tekst = TextBox4.Text
Slaap = TextBox6.Text
Host = TextBox7.Text
Cc = TextBox8.Text
Dim myFile As String = OpenFileDialog1.FileName 'Assign selected filename to var
If OpenFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
MessageBox.Show(myFile) 'It gives full path
End If
'set the addresses
mail.From = New System.Net.Mail.MailAddress(Van)
mail.To.Add(Naar)
If CheckBox1.Checked Then
mail.CC.Add(Cc)
End If
'set the content
mail.Subject = Onderwerp
mail.Body = Tekst
mail.Attachments.Add(myFile)
'send the message
Dim smtp As New System.Net.Mail.SmtpClient(Host)
'//CURSOR
Me.Cursor = Cursors.WaitCursor
smtp.Send(mail)
Me.Cursor = Cursors.Default
pBar1.Visible = False
MsgBox("Operatie geslaagd!")
End Sub
End Class
But:
Error 1 Value of type 'String' cannot be converted to 'System.Net.Mail.Attachment'. E:\Documents and Settings\The King\Mijn Documenten\Visual Studio 2005\Projects\Mail\Mail\Form1.vb 52 30 Mail
Wich is here:
VB Code:
mail.Attachments.Add(myFile)
How can I solve that?
-
Feb 14th, 2007, 12:13 PM
#16
Re: [2005] E-Mailing?
I think you have to use the New keyword along with Attachment.
I've not done this before so apologies if I end up sending you down the wrong path for a solution. There's a problem with the code you posted though. You assign the FileName to your variable BEFORE you check they have pressed ok. You can't do that I don't think or at least logically it doesn't seem correct to me.
VB Code:
Dim myFile As String
Dim sMail As New Net.Mail.MailMessage
With OpenFileDialog1
.Filter = ".txt | *.txt"
.InitialDirectory = "C:\"
If .ShowDialog = Windows.Forms.DialogResult.OK Then
myFile = OpenFileDialog1.FileName
sMail.Attachments.Add(New Mail.Attachment(myFile))
End If
End With
Last edited by stimbo; Feb 14th, 2007 at 12:55 PM.
-
Feb 14th, 2007, 12:58 PM
#17
Re: [2005] E-Mailing?
Having thought about it, the best thing to do would be to have them adding attachments in another button and saving the file names into a listbox or multi-line textbox. Then at the very end, when all the info has been entered and they are ready to send, process each line in the attachments list and add it to your attachments then.
The current way may be a little too error prone.
-
Feb 15th, 2007, 09:37 AM
#18
Thread Starter
Addicted Member
Re: [2005] E-Mailing?
The attachment is working now, but one last question, how can I set headers? (in PHP it is "text\HTML") How can I do that in VB?
-
Feb 15th, 2007, 10:01 AM
#19
Hyperactive Member
Re: [2005] E-Mailing?
maybe you also want to set the timeout value to a higher number when you are sending attachments.. large attachments can take a long time to upload to the smtp server and can cause a timeout.
too make your program a little more advanced add.
VB Code:
Dim SMTP As New Net.Mail.SmtpClient("SMTP.gmail.com") ' i use it for gmail :D, therefor SMTP.gmail.com
SMTP.Credentials = New System.Net.NetworkCredential(username, password)
SMTP.Port = Port
SMTP.Host = host
SMTP.EnableSsl = false
SMTP.Timeout = 600 * 1000 ' (10 minutes)
SMTP.Send(Mail)
SMTP = Nothing
-
Feb 15th, 2007, 10:44 AM
#20
Thread Starter
Addicted Member
Re: [2005] E-Mailing?
Great, but can I set the headers to Text/HTML? So that you can send HTML mail also?
-
Feb 15th, 2007, 11:01 AM
#21
Hyperactive Member
Re: [2005] E-Mailing?
cant you use the mail.IsBodyHtml=True statement?
-
Feb 15th, 2007, 11:07 AM
#22
Re: [2005] E-Mailing?
I was just going to ask that.
Alternatively (or in combination with the above), you may want to check this out. I found it on my computer but haven't saved the original link so don't know where I got it from. Google should help.
VB Code:
Dim sMail As New Mail.MailMessage
'You can create plain text views
Dim plainView As Mail.AlternateView = Mail.AlternateView.CreateAlternateViewFromString("A plain text view", Nothing, "text/plain")
'then we create the Html part
Dim htmlView As Mail.AlternateView = Mail.AlternateView.CreateAlternateViewFromString("String Here which shows as HTML", Nothing, "text/html")
sMail.AlternateViews.Add(plainView)
sMail.AlternateViews.Add(htmlView)
Or what does: Mail.Headers.Add do??
Last edited by stimbo; Feb 15th, 2007 at 11:15 AM.
-
Feb 15th, 2007, 01:54 PM
#23
Thread Starter
Addicted Member
Re: [2005] E-Mailing?
Thank you very much! Óne question left:
Now you have an OpenFileDialog wich is setting the attachment, I want that option staying, but also that you can type the path and file in an textbox, so what I've got:
But that is not working...
-
Feb 16th, 2007, 09:25 AM
#24
Thread Starter
Addicted Member
Re: [2005] E-Mailing?
Anyone who can solve this last problem?
-
Feb 16th, 2007, 10:16 AM
#25
Re: [2005] E-Mailing?
Could you be more specific, what exactly is not working? Are you getting an error? Have you tried setting a breakpoint to see what is going on?
Are you essentially trying to do this:
VB Code:
sMail.Attachments.Add(New Net.Mail.Attachment(Me.TextBox9.Text))
-
Feb 16th, 2007, 10:31 AM
#26
Thread Starter
Addicted Member
Re: [2005] E-Mailing?
When you type in TextBox9, he is making that attachment, but if TextBox9 is empty, make no attatchment, code:
VB Code:
If TextBox9.SelectionLength >= 1 Then
mail.Attachments.Add(New Net.Mail.Attachment(TextBox9.Text))
TextBox6.Value = "1000"
End If
and
VB Code:
With OpenFileDialog1
.Filter = "*.* | *.*"
.InitialDirectory = "C:\"
If .ShowDialog = Windows.Forms.DialogResult.OK Then
myFile = OpenFileDialog1.FileName
Bijlage = True
TextBox9.Text = myFile
TextBox6.Text = "1000"
End If
No errors, but just not doing what I want that he is doing
-
Feb 16th, 2007, 10:38 AM
#27
Re: [2005] E-Mailing?
VB Code:
If Not Me.TextBox9.Text = String.Empty Then
If IO.File.Exists(Me.TextBox9.Text) Then
sMail.Attachments.Add(New Net.Mail.Attachment(Me.TextBox9.Text))
Else : MessageBox.Show("A file could not be found with that path", "Not Found", MessageBoxButtons.OK)
End If
End If
Or a little easier to read and with a little more error messaging:
VB Code:
If Not Me.TextBox9.Text = String.Empty And IO.File.Exists(Me.TextBox9.Text) Then
sMail.Attachments.Add(New Net.Mail.Attachment(Me.TextBox9.Text))
Else : MessageBox.Show("A file could not be found with that path", "Not Found", MessageBoxButtons.OK)
End If
Last edited by stimbo; Feb 16th, 2007 at 10:44 AM.
-
Feb 16th, 2007, 11:16 AM
#28
Thread Starter
Addicted Member
Re: [2005] E-Mailing?
Great! Really thank you all!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|