Private Sub sendbutton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles sendbutton.Click If con.State = ConnectionState.Open Then con.Close() End If Try Dim mail, atchmail As New MailMessage mail.Subject = subjecttextbox.Text mail.To.Add(totextbox.Text) mail.From = New MailAddress(usernametextbox.Text) mail.Body = bodytextbox.Text If attachmenttextbox.Text <> "" Then 'If attachment is there than this code will be executed Dim attachment As System.Net.Mail.Attachment attachment = New System.Net.Mail.Attachment(attachmenttextbox.Text) mail.Attachments.Add(attachment) Dim smtp As New SmtpClient("smtp.gmail.com") smtp.EnableSsl = True smtp.Credentials = New System.Net.NetworkCredential(usernametextbox.Text, passwordtextbox.Text) smtp.Port = "587" smtp.Send(mail) Else 'If there is no attachment than this code is executed Dim smtp As New SmtpClient("smtp.gmail.com") smtp.EnableSsl = True smtp.Credentials = New System.Net.NetworkCredential(usernametextbox.Text, passwordtextbox.Text) smtp.Port = "587" smtp.Send(mail) End If MsgBox("Message Sent Successfully", MsgBoxStyle.Information) totextbox.Clear() subjecttextbox.Clear() bodytextbox.Clear() attachmenttextbox.Clear() usernametextbox.Text = "sample_email@gmail.com" passwordtextbox.Clear() Catch ex As Exception MsgBox(ex.ToString) End Try End Sub
Saturday, 15 June 2013
Coding to Send Email on a Button Click With and Without attachment
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment