zoukankan      html  css  js  c++  java
  • Send email

    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:UsersJohnDesktop"
                    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:UsersJohnDesktop"
                            .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
  • 相关阅读:
    泛社交泛泛之交也很重要
    iOS 切后台挂机
    iOS 导航栏translucent用法
    iOS 中UIButton中文字换行
    iOS 给UIimageView添加UITapGestureRecognizer手势点击事件
    iOS15UITableView多了白条,导航栏和Tabbar变成白色和标题变黑处理总结属性变化和原来基本的导航栏属性总结记录(看到就更新)
    iOS延时定时功能
    iOS uiview添加背景图案
    iOS 导航栏返回把样式带回前面怎么办
    iOS 识别图片二维码demo,复制粘贴即用
  • 原文地址:https://www.cnblogs.com/tony-MSDN/p/4387117.html
Copyright © 2011-2022 走看看