Email program

juggernot

Well-known member
Joined
Sep 28, 2006
Messages
173
Programming Experience
Beginner
I'm having trouble sending attachments with my emails in my newest program. I believe it is because I am trying to access my mailmessage in a different sub, and it isn't working. Making my mailmessage form level doesn't seem to work either. I have one button to load an attachment, and one button to send the email. I apologize for not following naming conventions. I'm building this program in a hurry and hadn't counted on needing to show others my code. Here it is.

VB.NET:
 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim intcounter As Integer
        Dim email As System.Web.Mail.SmtpMail
            Dim message As New System.Web.mail.MailMessage
            message.Body = Me.TextBox2.Text
            message.From = Me.TextBox1.Text
            message.To = Me.TextBox4.Text
            message.Subject = Me.TextBox3.Text
            email.SmtpServer = Me.TextBox6.Text
            email.Send(message)
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim attach As String
        Dim message As New System.Web.mail.MailMessage
        Me.OpenFileDialog1.ShowDialog()
        Me.OpenFileDialog1.CheckPathExists = True
        attach = Me.OpenFileDialog1.FileName
        Me.OpenFileDialog1.Multiselect = False
        message.Attachments.Add(attach)
        Me.Label5.Text += "  " & attach
    End Sub
End Class
 
If you want to access the same object in more than one method then you need to assign it to a member variable plain and simple. You obviously must then refer to that same variable in both methods.

You also should not be creating an instance of the SmtpMail class. The only members that are useful are Shared, so you should calling them on the class itself, NOT a variable of that type.
 
I'm having trouble sending attachments with my emails in my newest program. I believe it is because I am trying to access my mailmessage in a different sub, and it isn't working. Making my mailmessage form level doesn't seem to work either. I have one button to load an attachment, and one button to send the email. I apologize for not following naming conventions. I'm building this program in a hurry and hadn't counted on needing to show others my code. Here it is.

VB.NET:
 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim intcounter As Integer
        Dim email As System.Web.Mail.SmtpMail
            [B][COLOR="Red"]Dim message As New System.Web.mail.MailMessage
            [/COLOR][/B]message.Body = Me.TextBox2.Text
            message.From = Me.TextBox1.Text
            message.To = Me.TextBox4.Text
            message.Subject = Me.TextBox3.Text
            email.SmtpServer = Me.TextBox6.Text
            email.Send(message)
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim attach As String
        [COLOR="red"][B]Dim message As New System.Web.mail.MailMessage
        [/B][/COLOR]Me.OpenFileDialog1.ShowDialog()
        Me.OpenFileDialog1.CheckPathExists = True
        attach = Me.OpenFileDialog1.FileName
        Me.OpenFileDialog1.Multiselect = False
        message.Attachments.Add(attach)
        Me.Label5.Text += "  " & attach
    End Sub
End Class

Youre gonna have a bit of a problem if your Button1 makes a new mail message and puts some text in it and sends it, and button 2 makes a new message and attaches something to it and then throws it away

Engage brain! Write some comments! Establish some logical flow! :)
 
I got it working now. What I had to do was dim a mail attachment on form level. Then, in the button2_click I had to make that mail attachment = new mailattachment (Attachment Path). Then I just had to add this code to my button1, before I send the email. message.attachments.add(myattachment).
 
Back
Top