Pages

Tuesday, May 12, 2009

Sending attachment with an email from fileupload control

VB.NET

If FileUpload1.HasFile Then

Dim toAddress As String = "you@yourprovider.com"
Dim fromAddress As String = "you@yourprovider.com"
Dim mailServer As String = "smtp.yourprovider.com"

Dim myMailMessage As MailMessage = New MailMessage()

myMailMessage.To.Add(toAddress)
myMailMessage.From = New MailAddress(fromAddress)
myMailMessage.Subject = "Test Message"

Dim fileName As String = Path.GetFileName(FileUpload1.PostedFile.FileName)
Dim myAttachment As New Attachment(FileUpload1.FileContent, fileName)
myMailMessage.Attachments.Add(myAttachment)

Dim mySmtpClient As New SmtpClient(mailServer)

mySmtpClient.Send(myMailMessage)

End If

No comments:

Post a Comment