welcome Netizen

Share Your Knowledge.It is a way to achieve immortality

Tuesday, June 7, 2011

How to send mail with attachment in


In this article i am going to explain to send mail by using multiple attachment in ASP.NET.

Now In this article you can send a mail with attachment.Its very easy process, add fileupload control from the toolbox
and design a page like below ,then add the following code..
First you have to import the namespace....

Then write the below code in Send Button Click Event.......

 Dim mail As System.Net.Mail.MailMessage = New System.Net.Mail.MailMessage()
        Dim attach1 As String
'checking CC and BCC textbox empty or Not
        If Not TextBox3.Text = "" Then
        End If
        If Not TextBox4.Text = "" Then
        End If
 mail.Subject = TextBox5.Text
        mail.From = New MailAddress(TextBox1.Text)
        mail.Body = TextBox6.Text
If attachment.HasFile Then
            mail.Attachments.Add(New Attachment(attachment.PostedFile.InputStream, attachment.FileName))
        End If
        If attachment2.HasFile Then
            mail.Attachments.Add(New Attachment(attachment2.PostedFile.InputStream, attachment2.FileName))
        End If

        mail.IsBodyHtml = True
        Dim smtp As SmtpClient = New SmtpClient()
        smtp.Host = ""
               smtp.Credentials = New System.Net.NetworkCredential("Gmailusername", "password")
        smtp.EnableSsl = True
        Label1.Text = "Your mail has sent"
        Catch ex As Exception
            Label1.Text = "The email cannot be sent" + ex.ToString()
        End Try

For Sending mail through yahoo you have to change following thing in the above code...

smtp.Host = ""
smtp.EnableSsl = False
smtp.Credentials = New System.Net.NetworkCredential("Yahoousername", "password")

i hope this helpful to you..........
plz continue with comments...


Post a Comment