桌面邮件飞机源码——VB.NET网络编程简单实例

最近闲来无事,用VB.NET制作了一个快速发送邮件的小程序,关于界面的控件设计我就不再做过多的说明啦,关于VB.NET的新手来说,可以先学习一下控件的基本使用方法,然后对本代码进行修改和完善,希望大家都能改的愉快,改的开心,也希望大家给我提出意见或建议。

闲话少说,上代码:

Imports System.Net.Mail
Imports System.Net.Sockets

Public Class Form1
    '判断是否需要身份验证
    Private Sub CheckBox1_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
        If CheckBox1.Checked = True Then
            txtName.Enabled = True
            txtPassword.Enabled = True
        Else
            txtName.Enabled = False
            txtPassword.Enabled = False
        End If
    End Sub


    Private Sub btnSend_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSend.Click
        Dim mail As New MailMessage()
        Try
            mail.From = New MailAddress(txtFrom.Text.ToString.Trim) '设置发件人
            mail.To.Add(txtTo.Text.ToString.Trim) '设置收件人
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "貌似您的邮件没有发出去,您在试一下吧~如果还是不行,我们表示抱歉,我们将继续优化~")
        End Try
        '设置邮件内容
        mail.Subject = txtSubject.Text
        mail.Body = RichTextBox1.Text

        '发送附件
        If Txtattach.Text.Trim <> "" Then
            mail.Attachments.Add(New Net.Mail.Attachment(Txtattach.Text))
        End If

        '设置邮件优先级别
        If ComboBox1.Text.Trim = "紧急" Then
            mail.Priority = MailPriority.High
        End If
        If ComboBox1.Text.Trim = "一般" Then
            mail.Priority = MailPriority.Normal
        End If
        If ComboBox1.Text.Trim = "不着急" Then
            mail.Priority = MailPriority.Low
        End If

        Dim smtp As New SmtpClient(txtSMTP.Text)
        '验证帐号与密码
        If CheckBox1.Checked = True Then
            smtp.Credentials = New System.Net.NetworkCredential(txtName.Text, txtPassword.Text)
        End If
        Try
            smtp.Send(mail)
            MsgBox("恭喜您,您的邮件已经飞走了,即将到达指定位置。", MsgBoxStyle.Information, "桌面邮件飞机V1.0公测版")
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "啊哦,貌似网络不通哦,检查一下网络,喝杯茶再试试吧~")
        End Try
    End Sub

    Private Sub Butaddattach_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Butaddattach.Click
        '设置允许添加的附件类型
        OpenFileDialog1.filter = "文本文件 (*.txt)|*.txt|Word 文 档 (*.doc)|*.doc|图 像 文 件(*.BMP;*.JPG;*.GIF;*.PNG;*.JPEG)|*.BMP;*.JPG;*.GIF;*.PNG;*.JPEG|所有文件(*.*)|**"
        OpenFileDialog1.showDialog()
        Txtattach.Text = OpenFileDialog1.FileName.Trim
    End Sub
    '刚启动时候账号密码框不可用
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        txtName.Enabled = False
        txtPassword.Enabled = False
        ComboBox1.Items.Add("紧急")
        ComboBox1.Items.Add("一般")
        ComboBox1.Items.Add("不着急")
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub
    Private Sub Label8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label8.Click

    End Sub

    Private Sub 导入TXT文件IToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 导入TXT文件IToolStripMenuItem.Click
        Dim f As New OpenFileDialog
        If f.ShowDialog <> Windows.Forms.DialogResult.OK Then
            Exit Sub
        End If
        Dim Inputdata As String
        OpenFileDialog2.FileName = "*.txt"
        OpenFileDialog2.InitialDirectory = "C:\ "
        OpenFileDialog2.Filter = " Text files(*.Txt)| *.* | All files(*.*)| *.* "
        OpenFileDialog2.FilterIndex = 2
        OpenFileDialog2.ShowDialog()
        FileOpen(1, OpenFileDialog2.FileName, OpenMode.Input)
     
            RichTextBox1.Text = ""
            Do While Not EOF(1)
                Inputdata = LineInput(1)
                RichTextBox1.Text = RichTextBox1.Text + Inputdata + vbCrLf
            Loop

            FileClose(1)
    End Sub
       
    Private Sub 保存邮件草稿SToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 保存邮件草稿SToolStripMenuItem.Click
        SaveFileDialog1.FileName = "DMFdefault.txt"
        SaveFileDialog1.DefaultExt = "txt"
        SaveFileDialog1.ShowDialog()
        FileOpen(1, SaveFileDialog1.FileName, OpenMode.Output)
        Print(1, RichTextBox1.Text)
        FileClose(1)
    End Sub

    Private Sub 退出程序eToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 退出程序eToolStripMenuItem.Click
        Dim i As Integer
        i = MsgBox("您的最近编辑的邮件草稿未保存,是否保存?", 3, "友情提示")
        If i = 2 Then
        ElseIf i = 6 Then
            SaveFileDialog1.FileName = "defalut.txt"
            SaveFileDialog1.DefaultExt = "txt"
            SaveFileDialog1.ShowDialog()
            FileOpen(1, SaveFileDialog1.FileName, OpenMode.Output)
            Print(1, RichTextBox1.Text)
            FileClose(1)
            End
        ElseIf i = 7 Then
            End
        End If


    End Sub

    Private Sub 复制CToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 复制CToolStripMenuItem.Click
        RichTextBox1.Copy()
    End Sub

    Private Sub 粘贴VToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 粘贴VToolStripMenuItem.Click
        RichTextBox1.Paste()

    End Sub

    
    Private Sub 剪切ZToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 剪切ZToolStripMenuItem.Click
        RichTextBox1.Cut()
    End Sub

    Private Sub 全选SToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 全选SToolStripMenuItem.Click
        RichTextBox1.SelectAll()
    End Sub

    Private Sub ContextMenuStrip1_Opening(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles ContextMenuStrip1.Opening

    End Sub

    Private Sub 剪切XToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 剪切XToolStripMenuItem.Click
        RichTextBox1.Cut()

    End Sub

    Private Sub 复制CToolStripMenuItem1_Click(sender As Object, e As EventArgs) Handles 复制CToolStripMenuItem1.Click
        RichTextBox1.Copy()

    End Sub

    Private Sub 粘贴VToolStripMenuItem1_Click(sender As Object, e As EventArgs) Handles 粘贴VToolStripMenuItem1.Click
        RichTextBox1.Paste()

    End Sub

    Private Sub 全选SToolStripMenuItem1_Click(sender As Object, e As EventArgs) Handles 全选SToolStripMenuItem1.Click
        RichTextBox1.SelectAll()

    End Sub

    Private Sub 字体FToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 字体FToolStripMenuItem.Click
        FontDialog1.ShowDialog()
        RichTextBox1.Font = FontDialog1.Font
    End Sub

    Private Sub 字体ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 字体ToolStripMenuItem.Click
        FontDialog1.ShowDialog()
        RichTextBox1.Font = FontDialog1.Font
    End Sub

    Private Sub 关于我们AToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 关于我们AToolStripMenuItem.Click
        AboutBox1.ShowDialog()

    End Sub

    Private Sub 意见反馈LToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 意见反馈LToolStripMenuItem.Click
        MsgBox("感谢您使用桌面邮件飞机V1.2正式版!在您的使用过程中, 如果您有问题, 想法, 意见或建议, 请您和我们取得联系!联系E -mail : lubianbianjibu@ 163.com 鸣谢:核心代码:阿丘;用户UI:昊洋;界面设计:林峰。感谢您的使用! ", 1, "意见反馈")
    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        Me.ToolStripStatusLabel2.Text = "系统时间:" + TimeOfDay
        Me.ToolStripStatusLabel3.Text = "系统日期:" + System.DateTime.Today

    End Sub
End Class

最近自己做了四集VB.NET的最为基础的任务驱动教程,对于VB.NET不是很熟悉,想快速切入的童鞋可以看看,希望对大家有些帮助:

任务一:http://v.youku.com/v_show/id_XNjc4NTk1NzYw.html

任务二:http://v.youku.com/v_show/id_XNjc4NjM3ODA0.html

任务三:http://v.youku.com/v_show/id_XNjgxNTQxMzY4.html

任务四:http://v.youku.com/v_show/id_XNjkyMjAwMzMy.html

欢迎大家提出各种意见和建议,谢谢大家!

你可能感兴趣的:(学术心得)