Option Strict On
Imports System.Net.Mail ' Add ref Assemblies, Framework, System.Net
' Link for getting email settings. Search for other mail server settings if necessary at link.
' http://email.about.com/od/accessinggmail/f/Gmail_SMTP_Settings.htm
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.Location = New Point(CInt((Screen.PrimaryScreen.WorkingArea.Width / 2) - (Me.Width / 2)), CInt((Screen.PrimaryScreen.WorkingArea.Height / 2) - (Me.Height / 2)))
TextBox2.Text = "587"
TextBox3.Text = "smtp.gmail.com"
MaskedTextBox1.PasswordChar = CChar("*")
RadioButton1.Checked = True
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Button1.Enabled = False
Button1.BackColor = Color.OrangeRed
Button1.Text = "Mail is" & vbCrLf & "being" & vbCrLf & "SENT!"
Dim mail As New MailMessage()
Dim SmtpServer As New SmtpClient()
SmtpServer.Credentials = New Net.NetworkCredential(TextBox1.Text, MaskedTextBox1.Text)
SmtpServer.Port = CInt(TextBox2.Text) ' Some threads say to use port 587 and others 465. I don't have gmail so I can't try this code.
SmtpServer.Host = TextBox3.Text
SmtpServer.EnableSsl = True
mail = New MailMessage()
Try
' In the below line if you don't use a display name in the second field then the 2nd and 3rd fields are not necessary.
' The first field is the originating address.
mail.From = New MailAddress(TextBox1.Text) ' ", "Some display name goes here", System.Text.Encoding.UTF8)
' A list that you can add various mail addresses to for sending to.
For i = 0 To TextBox6.Lines.Count - 1
mail.To.Add(TextBox6.Lines(i))
Next
mail.Subject = TextBox4.Text
mail.Body = TextBox5.Text
' A list you can add various attachments to for the email. Of course if the attachment sizes max out what a sender or receiver
' is allowed or if a receiver will not accept a certain attachment type like a .exe (a .exe in a zip file works sending the zip
' file usually) then issues will occur.
If MessageBox.Show("Would you like to add attachments?", "Add email attachments", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) = Windows.Forms.DialogResult.OK Then
Dim OFD As New OpenFileDialog
OFD.Multiselect = True
OFD.InitialDirectory = "C:\Users\John\Desktop"
OFD.Title = "Add mail attachments."
Do Until OFD.ShowDialog = Windows.Forms.DialogResult.Cancel
For Each Item In OFD.FileNames
mail.Attachments.Add(New Attachment(Item))
Next
Loop
OFD.Dispose()
End If
If MessageBox.Show("Would you like to add an image?", "Add an image", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) = Windows.Forms.DialogResult.OK Then
Using OFD As New OpenFileDialog
With OFD
.Multiselect = False
.Filter = "Image .Png files (*.Png)|*.Png"
.InitialDirectory = "C:\Users\John\Desktop"
.Title = "Add an image."
End With
If OFD.ShowDialog = Windows.Forms.DialogResult.OK Then
'to embed images, we need to use the prefix 'cid' in the img src value
'the cid value will map to the Content-Id of a Linked resource.
'thus <img src='cid:myimage'> will map to a LinkedResource with a ContentId of 'myimage'
'Dim htmlView As AlternateView = AlternateView.CreateAlternateViewFromString("<p>There is an embedded image below.</p>" & "<img src=cid:myimage>" & _
' "<p>" & TextBox5.Text & "</p>", Nothing, "text/html")
Dim htmlView As AlternateView = AlternateView.CreateAlternateViewFromString("<html><head></head><body><header><p>There is an embedded image below.<p/></header><br><img src=cid:myimage alt=" & ChrW(34) & "Some image." & Chrw(34) & "><br><footer><p>" & TextBox5.Text & "<p/></footer></body></html>", Nothing, "text/html")
'create the LinkedResource (embedded image)
Dim logo As New LinkedResource(OFD.FileName, "image/png")
logo.ContentId = "myimage"
'add the LinkedResource to the appropriate view
htmlView.LinkedResources.Add(logo)
mail.AlternateViews.Add(htmlView)
End If
End Using
End If
If RadioButton1.Checked Then
mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure
ElseIf RadioButton2.Checked Then
mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess
ElseIf RadioButton3.Checked Then
mail.DeliveryNotificationOptions = DeliveryNotificationOptions.Delay
ElseIf RadioButton4.Checked Then
mail.DeliveryNotificationOptions = DeliveryNotificationOptions.None
ElseIf RadioButton5.Checked Then
mail.DeliveryNotificationOptions = DeliveryNotificationOptions.Never
End If
' I suppose this list is in case a delivery notification fails but not sure.
If TextBox7.Text <> "" Then
For i = 0 To TextBox7.Lines.Count - 1
mail.ReplyToList.Add(TextBox7.Lines(i))
Next
End If
SmtpServer.Send(mail)
Button1.Enabled = True
Button1.BackColor = Color.Lime
Button1.Text = "Select me" & vbCrLf & "when email" & vbCrLf & "is ready to" & vbCrLf & "send."
Catch ex As Exception
MessageBox.Show(ex.ToString)
Button1.Enabled = True
Button1.BackColor = Color.Lime
Button1.Text = "Select me" & vbCrLf & "when email" & vbCrLf & "is ready to" & vbCrLf & "send."
End Try
End Sub
Private Sub RadioButtons_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton1.CheckedChanged, RadioButton2.CheckedChanged, RadioButton3.CheckedChanged, RadioButton4.CheckedChanged, RadioButton5.CheckedChanged
For Each Ctl As RadioButton In Me.Controls.OfType(Of RadioButton)()
Ctl.BackColor = SystemColors.ActiveCaption
Next
If DirectCast(sender, RadioButton).Checked Then
DirectCast(sender, RadioButton).BackColor = Color.Lime
End If
End Sub
End Class