vijay

welcome Netizen

Share Your Knowledge.It is a way to achieve immortality

Tuesday, June 7, 2011

How to send mail with attachment in asp.net


Hi..

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

system.net.mail

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
        mail.To.Add(TextBox2.Text)
'checking CC and BCC textbox empty or Not
        If Not TextBox3.Text = "" Then
            mail.CC.Add(TextBox3.Text)
        End If
        If Not TextBox4.Text = "" Then
            mail.Bcc.Add(TextBox4.Text)
        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.gmail.com"
               smtp.Credentials = New System.Net.NetworkCredential("Gmailusername", "password")
        smtp.EnableSsl = True
        Label1.Text = "Your mail has sent"
        Try
            smtp.Send(mail)
        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.mail.yahoo.com"
smpt.port=25
smtp.EnableSsl = False
smtp.Credentials = New System.Net.NetworkCredential("Yahoousername", "password")


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

0 comments:

Post a Comment