Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sending email attachment in VB.NET

Status
Not open for further replies.

Hobeau

MIS
Apr 19, 2004
77
US
hey guys, I'm a vb newbie, so if someone can help me with this, I would appreciate it. I'm trying to build a quick little VB.net form that when opened will send me an email attachment with the event logs from my computers at work. I have about 10 of them and no one can seem to remember what error messages they get, so it would be helpful for me to be able to have a list of them, and I'm trying to learn more about SMTP in VB.net. Here's some of my code:

Code:
Imports system.web.mail
Public Class Form1
    Inherits System.Windows.Forms.Form
    Dim Attachment As System.Web.Mail.MailAttachment

 Private Sub newmailer(ByVal smtp, ByVal messageto, ByVal messagefrom, ByVal subject, ByVal message)
        Dim myMessage As New MailMessage
        Dim myMailServer As SmtpMail

        myMessage.BodyFormat = MailFormat.Html
        myMessage.Priority = MailPriority.Normal
        myMessage.From = messagefrom 
        myMessage.To = messageto
        myMessage.Subject = subject
        myMessage.Body = message & vbCrLf
        myMessage.Body &= "<font color=blue></font><br>" & vbCrLf & _
                          "<font color=red></font><br>" & vbCrLf
        myMessage.Attachments.Add("c:\path\filename.ext")
        myMailServer.SmtpServer = smtp 
        myMailServer.Send(myMessage)
        myMessage = Nothing
    End Sub
    Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click

    End Sub

'I have included the call statements in the form load so I 'can just schedule this executable to load, send the 'attachment and then close.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim smtp As String
        Dim subject As String
        Dim messageto As String
        Dim messagefrom As String
        Dim message As String

        smtp = "smtpservername"
        messageto = "myemail@domain.com"
        messagefrom = "fromusername@domain.com"
        subject = "subject line"
        message = "message"


        Call newmailer(smtp, messageto, messagefrom, subject, message)
        Me.Close()
    End Sub
End Class
 
Hobeau,

You have yet to ask a specific question. For example, are you having a problem?
 
sorry Dimandja,

I actually am having a specific problem that I failed to mention. When I run the program it gives me an error message on this line
Code:
     [u][b]myMailServer.SmtpServer = smtp[/b][/u] 
        myMailServer.Send(myMessage)
        myMessage = Nothing
It says something about "An unhandled exception of type 'System.InvalidCastException' occurred in system.web.dll
Additional information: Specified cast is not valid."
To be honest I'm not exactly sure what this means. Another thing is that I'm not exactly sure if I'm using the attachment right. I'm putting the filename in the paramater, and I think that is correct, but I'm not sure. Would it be better to create a variable that holds the path for the file name for the attachment?
 
Ok,, I just figured it out. I wasn't using the "mymessage.attachments.add()" correctly. I was trying to put the path and filename in the parameters instead of dimming a new instance of a mail attachment. for anyone who is interested here is my code now.

Code:
 Private Sub newmailer(ByVal smtp, ByVal messageto, ByVal messagefrom, ByVal subject, ByVal message)
        Dim myMessage As New MailMessage
        Dim myMailServer As SmtpMail
        Dim attachment As MailAttachment = New MailAttachment("c:\path\filename.ext")


        myMessage.BodyFormat = MailFormat.Html
        myMessage.Priority = MailPriority.Normal
        myMessage.From = messagefrom
        myMessage.To = messageto
        myMessage.Subject = subject
        myMessage.Body = message & vbCrLf
        myMessage.Body &= "<font color=blue></font><br>" & vbCrLf & _
                          "<font color=red></font><br>" & vbCrLf
        myMessage.Attachments.Add(attachment)


        myMailServer.SmtpServer = smtp
        myMailServer.Send(myMessage)
        myMessage = Nothing
    End Sub

Well you learn something new everyday. thanx for the response Dimandja!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top