Results 1 to 28 of 28

Thread: [RESOLVED] [2005] E-Mailing?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    178

    Resolved [RESOLVED] [2005] E-Mailing?

    I'm trying to make an e-mailprogram and got this code:
    VB Code:
    1. 'create the mail message
    2.         Dim mail As New System.Net.Mail.MailMessage()
    3.  
    4.         'set the addresses
    5.         mail.From = New System.Net.Mail.MailAddress("[email protected]")
    6.         mail.To.Add("[email protected]")
    7.  
    8.         'set the content
    9.         mail.Subject = "This is an email"
    10.         mail.Body = "this is the body content of the email."
    11.  
    12.         'send the message
    13.         Dim smtp As New System.Net.Mail.SmtpClient("127.0.0.1")
    14.         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
    VB Code:
    1. smtp.Send(mail)
    "SmtpException was unhandled" What does that mean?

    Cheers,
    Thomas
    Last edited by Mindstorms; Feb 16th, 2007 at 11:17 AM.

  2. #2
    Frenzied Member Asgorath's Avatar
    Join Date
    Sep 2004
    Location
    Saturn
    Posts
    2,036

    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."

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    178

    Re: [2005] E-Mailing?

    Ok, but I haven't, so what can I do now? Is there another option to mail?

  4. #4
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    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:
    1. Try
    2.  
    3. 'Your existing code#
    4.  
    5.  
    6. Catch ex As Exception
    7.     Messagebox.Show(ex.Message)
    8. End Try

    This wont solve it but may give you more info.

    EDIT: Or someone just might know straight away.
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    178

    Re: [2005] E-Mailing?

    Ok, in Dutch it is "Fout bij het verzenden van e-mail" in English: "Error at sending e-mail"

  6. #6
    Frenzied Member Asgorath's Avatar
    Join Date
    Sep 2004
    Location
    Saturn
    Posts
    2,036

    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."

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    178

    Re: [2005] E-Mailing?

    No, but it's in a network of some computers at home...

  8. #8
    Frenzied Member Asgorath's Avatar
    Join Date
    Sep 2004
    Location
    Saturn
    Posts
    2,036

    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."

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    178

    Re: [2005] E-Mailing?

    Ok, but there is not another option of sending e-mail?

  10. #10
    Frenzied Member Asgorath's Avatar
    Join Date
    Sep 2004
    Location
    Saturn
    Posts
    2,036

    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."

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    178

    Re: [2005] E-Mailing?

    Yes! I'll try now

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    178

    Re: [2005] E-Mailing?

    Gotcha! Thank you all very much for you're very fast help!

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    178

    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:
    1. Public Class Form1
    2.     Dim Stoppen
    3.  
    4.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    5.         'create the mail message
    6.         Dim mail As New System.Net.Mail.MailMessage()
    7.         Dim Van, Naar, Onderwerp, Tekst, Slaap, Host, Cc, Bijlage_1
    8.         Van = TextBox1.Text
    9.         Naar = TextBox2.Text
    10.         Onderwerp = TextBox3.Text
    11.         Tekst = TextBox4.Text
    12.         Slaap = TextBox6.Text
    13.         Host = TextBox7.Text
    14.         Cc = TextBox8.Text
    15.  
    16.         Dim Bijlage As String
    17.         Bijlage = OpenFileDialog1.ToString
    18.         OpenFileDialog1.ShowDialog()
    19.         OpenFileDialog1.ToString()
    20.         Bijlage_1 = System.Net.Mail.Attachment.CreateAttachmentFromString(Bijlage, Bijlage)
    21.  
    22.         'set the addresses
    23.         mail.From = New System.Net.Mail.MailAddress(Van)
    24.         mail.To.Add(Naar)
    25.         mail.CC.Add(Cc)
    26.         'set the content
    27.         mail.Subject = Onderwerp
    28.         mail.Body = Tekst
    29.  
    30.         mail.Attachments.Add(Bijlage_1)
    31.  
    32.         'send the message
    33.         Dim smtp As New System.Net.Mail.SmtpClient(Host)
    34.  
    35.         '//CURSOR
    36.         Me.Cursor = Cursors.WaitCursor
    37.  
    38.         smtp.Send(mail)
    39.  
    40.         Me.Cursor = Cursors.Default
    41.         pBar1.Visible = False
    42.         MsgBox("Operatie geslaagd!")
    43.  
    44.     End Sub
    45. End Class

    (It should be HOI.txt from C:\)
    Last edited by Mindstorms; Feb 14th, 2007 at 09:57 AM.

  14. #14
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    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:
    1. With OpenFileDialog1
    2.  
    3.             .Filter = ".txt | *.txt"
    4.             .InitialDirectory = "C:\"
    5.             .ShowDialog()
    6.  
    7.         End With
    8.  
    9.         Dim myFile As String = OpenFileDialog1.FileName   'Assign selected filename to var
    10.  
    11.         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:
    1. If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
    2.    'Assign file name to variable etc...
    3. 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.
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  15. #15

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    178

    Re: [2005] E-Mailing?

    Well, I've got this now:

    VB Code:
    1. Public Class Form1
    2.  
    3.     Dim Stoppen
    4.  
    5.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    6.         'create the mail message
    7.         Dim mail As New System.Net.Mail.MailMessage()
    8.         Dim Van, Naar, Onderwerp, Tekst, Slaap, Host, Cc, Bijlage_1
    9.         Van = TextBox1.Text
    10.         Naar = TextBox2.Text
    11.         Onderwerp = TextBox3.Text
    12.         Tekst = TextBox4.Text
    13.         Slaap = TextBox6.Text
    14.         Host = TextBox7.Text
    15.         Cc = TextBox8.Text
    16.         Dim myFile As String = OpenFileDialog1.FileName 'Assign selected filename to var
    17.         If OpenFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
    18.             MessageBox.Show(myFile)   'It gives full path
    19.         End If
    20.  
    21.         'set the addresses
    22.         mail.From = New System.Net.Mail.MailAddress(Van)
    23.         mail.To.Add(Naar)
    24.  
    25.         If CheckBox1.Checked Then
    26.             mail.CC.Add(Cc)
    27.         End If
    28.  
    29.         'set the content
    30.         mail.Subject = Onderwerp
    31.         mail.Body = Tekst
    32.  
    33.         mail.Attachments.Add(myFile)
    34.  
    35.         'send the message
    36.         Dim smtp As New System.Net.Mail.SmtpClient(Host)
    37.  
    38.         '//CURSOR
    39.         Me.Cursor = Cursors.WaitCursor
    40.  
    41.                 smtp.Send(mail)
    42.  
    43.         Me.Cursor = Cursors.Default
    44.         pBar1.Visible = False
    45.         MsgBox("Operatie geslaagd!")
    46.  
    47.     End Sub
    48. 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:
    1. mail.Attachments.Add(myFile)

    How can I solve that?

  16. #16
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    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:
    1. Dim myFile As String
    2.         Dim sMail As New Net.Mail.MailMessage
    3.  
    4.         With OpenFileDialog1
    5.  
    6.             .Filter = ".txt | *.txt"
    7.             .InitialDirectory = "C:\"
    8.             If .ShowDialog = Windows.Forms.DialogResult.OK Then
    9.                 myFile = OpenFileDialog1.FileName
    10.                 sMail.Attachments.Add(New Mail.Attachment(myFile))
    11.             End If
    12.  
    13.         End With
    Last edited by stimbo; Feb 14th, 2007 at 12:55 PM.
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  17. #17
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    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.
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  18. #18

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    178

    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?

  19. #19
    Hyperactive Member gnaver's Avatar
    Join Date
    Jul 2005
    Location
    Denmark/Sweden
    Posts
    289

    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:
    1. Dim SMTP As New Net.Mail.SmtpClient("SMTP.gmail.com") ' i use it for gmail :D, therefor SMTP.gmail.com
    2.         SMTP.Credentials = New System.Net.NetworkCredential(username, password)
    3.         SMTP.Port = Port
    4.         SMTP.Host = host
    5.         SMTP.EnableSsl = false
    6.         SMTP.Timeout = 600 * 1000 ' (10 minutes)
    7.         SMTP.Send(Mail)
    8.         SMTP = Nothing

  20. #20

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    178

    Re: [2005] E-Mailing?

    Great, but can I set the headers to Text/HTML? So that you can send HTML mail also?

  21. #21
    Hyperactive Member gnaver's Avatar
    Join Date
    Jul 2005
    Location
    Denmark/Sweden
    Posts
    289

    Re: [2005] E-Mailing?

    cant you use the mail.IsBodyHtml=True statement?

  22. #22
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    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:
    1. Dim sMail As New Mail.MailMessage
    2.         'You can create plain text views
    3.         Dim plainView As Mail.AlternateView = Mail.AlternateView.CreateAlternateViewFromString("A plain text view", Nothing, "text/plain")
    4.         'then we create the Html part
    5.         Dim htmlView As Mail.AlternateView = Mail.AlternateView.CreateAlternateViewFromString("String Here which shows as HTML", Nothing, "text/html")
    6.         sMail.AlternateViews.Add(plainView)
    7.         sMail.AlternateViews.Add(htmlView)

    Or what does: Mail.Headers.Add do??
    Last edited by stimbo; Feb 15th, 2007 at 11:15 AM.
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  23. #23

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    178

    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:
    VB Code:
    1. myFile = TextBox9.Text
    But that is not working...

  24. #24

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    178

    Re: [2005] E-Mailing?

    Anyone who can solve this last problem?

  25. #25
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    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:
    1. sMail.Attachments.Add(New Net.Mail.Attachment(Me.TextBox9.Text))
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  26. #26

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    178

    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:
    1. If TextBox9.SelectionLength >= 1 Then
    2.             mail.Attachments.Add(New Net.Mail.Attachment(TextBox9.Text))
    3.             TextBox6.Value = "1000"
    4.         End If

    and
    VB Code:
    1. With OpenFileDialog1
    2.  
    3.             .Filter = "*.* | *.*"
    4.             .InitialDirectory = "C:\"
    5.             If .ShowDialog = Windows.Forms.DialogResult.OK Then
    6.                 myFile = OpenFileDialog1.FileName
    7.  
    8.                 Bijlage = True
    9.                 TextBox9.Text = myFile
    10.                 TextBox6.Text = "1000"
    11.             End If

    No errors, but just not doing what I want that he is doing

  27. #27
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: [2005] E-Mailing?

    VB Code:
    1. If Not Me.TextBox9.Text = String.Empty Then
    2.             If IO.File.Exists(Me.TextBox9.Text) Then
    3.                 sMail.Attachments.Add(New Net.Mail.Attachment(Me.TextBox9.Text))
    4.             Else : MessageBox.Show("A file could not be found with that path", "Not Found", MessageBoxButtons.OK)
    5.             End If
    6.         End If


    Or a little easier to read and with a little more error messaging:

    VB Code:
    1. If Not Me.TextBox9.Text = String.Empty And IO.File.Exists(Me.TextBox9.Text) Then
    2.             sMail.Attachments.Add(New Net.Mail.Attachment(Me.TextBox9.Text))
    3.         Else : MessageBox.Show("A file could not be found with that path", "Not Found", MessageBoxButtons.OK)
    4.    End If
    Last edited by stimbo; Feb 16th, 2007 at 10:44 AM.
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  28. #28

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    178

    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
  •  



Click Here to Expand Forum to Full Width