I need a code in vb.net for sending mail with attachment...

Kindly help ...

Vivek

Ok But never ask for code again. Please ..

I give u the code because i have it now opened.

Post ur problem and we will solve this.

Public Class smail

    ''one static method for sending e-mails
    Public Sub SendMail(ByVal [From] As String, ByVal [To] As String, _
                        ByVal Subject As String, ByVal Body As String, ByVal MailServer _
                        As String, Optional ByVal IsBodyHtml As Boolean = True, _
                        Optional ByVal MailPort As Integer = 25, _
                        Optional ByVal Attachments() As String = Nothing, Optional _
                        ByVal AuthUsername As String = Nothing, Optional ByVal _
                        AuthPassword As String = Nothing)
        ''create a SmtpClient object to allow applications to send 
        ''e-mail by using the Simple Mail Transfer Protocol (SMTP).
        Dim MailClient As System.Net.Mail.SmtpClient = _
        New System.Net.Mail.SmtpClient(MailServer, MailPort)
        ''create a MailMessage object to represent an e-mail message
        ''that can be sent using the SmtpClient class
        Dim MailMessage = New System.Net.Mail.MailMessage( _
        [From], [To], Subject, Body)
        ''sets a value indicating whether the mail message body is in Html.
        MailMessage.IsBodyHtml = IsBodyHtml
        ''sets the credentials used to authenticate the sender
        If (AuthUsername IsNot Nothing) AndAlso (AuthPassword _
                                                 IsNot Nothing) Then
            MailClient.Credentials = New  _
            System.Net.NetworkCredential(AuthUsername, AuthPassword)
        End If
        ''add the files as the attachments for the mailmessage object
        If (Attachments IsNot Nothing) Then
            For Each FileName In Attachments
                MailMessage.Attachments.Add( _
                New System.Net.Mail.Attachment(FileName))
            Next
        End If
        MailClient.Send(MailMessage)
    End Sub

End Class

Create an object of sMail and enjoy........

Is it through an Exchange server or something else?

Could u please explain how to use this code ...

em.SendMail("vivekanandaan@gmail.com", "vivekanandaan@gmail.com", "Hai !", "Body", "smtp.gmail.com", False, 25, "Nothing", "vivekanandaan@gmail.com", "yahoo@78")

value of type 'String' cannot be converted to '1-dimensional array of String'.

Kindly advice ...

ok try this one..

Dim SmtpServer As New SmtpClient()
        SmtpServer.Credentials = New Net.NetworkCredential("username", "password")
        SmtpServer.Port = 587
        SmtpServer.Host = "smtp.gmail.com"
        SmtpServer.EnableSsl = True

        Dim mail As New MailMessage()
        Dim addr() As String = TextBox1.Text.Split(",")
        Try
            mail.From = New MailAddress("abc@gmail.com", "abc def", System.Text.Encoding.UTF8)

            Dim i As Byte
            For i = 0 To addr.Length - 1
                mail.To.Add(addr(i))
            Next
            mail.Subject = TextBox3.Text
            mail.Body = TextBox4.Text
            If ListBox1.Items.Count <> 0 Then ' This listbox holds the path of the files...
                For i = 0 To ListBox1.Items.Count - 1
                    mail.Attachments.Add(New Attachment(ListBox1.Items.Item(i)))
                Next
            End If
            mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure
            mail.ReplyTo = New MailAddress(TextBox1.Text)
            SmtpServer.Send(mail)

@vivekanandaan, please make sure you didn't end up with an extra () on the end of one of your strings when you pasted the code.

The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required

I am getting this error ...

hello !
check this out

'first import these classes
Imports System.Web
Imports System.IO
Imports System.Net.Mail
Imports System.Net.Mail.Attachment
Imports System.Runtime
'now use this code to send emails with attachments

    Sub mail()
        On Error Resume Next
        Dim mail As New MailMessage()
        Dim SmtpServer As New SmtpClient
        SmtpServer.Credentials = New Net.NetworkCredential("YourEmail ID", "Password")
        SmtpServer.Port = 587
        SmtpServer.Host = "smtp.gmail.com"
        SmtpServer.EnableSsl = True
        SmtpServer.EnableSsl = True
        mail.To.Add("id on which u want to receive email")
        mail.From = New MailAddress("id which send email")
        mail.Subject = Me.txtsubject.Text
        mail.Body = txtbody.Text
        Dim myfile As New Attachment(Me.txtfileaddress.Text)'here you have to give the path of attached file.
        mail.Attachments.Add(myfile)
        SmtpServer.Send(mail)
        MsgBox("Email Send SuccessFully. :)", MsgBoxStyle.Information)
    End Sub

if this code helps you :) please vote me up

even though the code is executed without any errors i am not receiving any mail... I checked after giving wrong password and it executes without any error saying that mail sent ... Kindly advice...

please use gmail id to send emails :)

I am using gmail id only ...

can you please post your code here so that i can check it , because it is working at my end .

Sub mail()
        On Error Resume Next
        Dim mail As New MailMessage()
        Dim SmtpServer As New SmtpClient
        SmtpServer.Credentials = New Net.NetworkCredential("vivekanandaan@gmail.com", "mypassword")
        SmtpServer.Port = 587
        SmtpServer.Host = "smtp.gmail.com"
        SmtpServer.EnableSsl = True
        SmtpServer.EnableSsl = True
        mail.To.Add("vivekanandaan@gmail.com")
        mail.From = New MailAddress("vivekanandaan@gmail.com")
        mail.Subject = "Subject"

        mail.Body = "Body"
        SmtpServer.Send(mail)
        MsgBox("Email Send SuccessFully. :)", MsgBoxStyle.Information)
    End Sub

hmmmm , ok do you import these classes ?

Imports System.Web
Imports System.IO
Imports System.Net.Mail
Imports System.Net.Mail.Attachment
Imports System.Runtime

then please check you id and password again at www.gmail.com. because there is nothing wrong with that code .

commented: spontaneous help ..... +2

For attachments....

mail.Attachments.Add(New Attachment("Path"))

You might need to change your email settings under Forwarding and POP/IMAP inside your GMail settings (to Enable POP).

Even though this example is in C#, you can compare and contrast.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.