A Simple Gmail Client

本文介绍如何使用 VB.NET 构建 Gmail 客户端来发送邮件,包括使用 System.Net.Mail 命名空间、界面布局及背景任务处理等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

In This Post I will Guide u To making a Simple Gmail Client  to send Your Emails using Gmail Account Using Vb.net

 Let me Show You how final Result will Look Like:



1. For Sending Mail Through Vb.net. This Client Uses  "Imports System.Net.Mail".
    So First Import it to your Code




Imports System.Net.Mail


2. Layout All the Controls As Shown In the Above Figure

3. There is Progress Bar after the send Button. I have used Backgroud Worker To Send the Mail. So that When Mail Sending is in Progress the Screen Wont Stop Working.

4. Now Here is the Complete Code

First Define This In your Form

Dim msg As New MailMessage 
 Dim i As Integer 


Form's Load Event

Me.ProgressBar1.MarqueeAnimationSpeed = 0


Write This to Send Button's Click Event

msg.To.Add(Me.txtto.Text) 
        msg.From = New MailAddress(Me.txtuser.Text) 
        msg.Subject = Me.txtsubject.Text 
        msg.Body = Me.txtbody.Text 
        '' for attachment 
        For i = 0 To Me.ListBox1.Items.Count - 1 
            msg.Attachments.Add(New Attachment(Me.ListBox1.Items(i).ToString)) 
        Next 
        Me.Button1.Enabled = False 
        Me.Button1.Text = "Sendign.." 
        Me.ProgressBar1.Visible = True 
        Me.ProgressBar1.MarqueeAnimationSpeed = 100 
        Me.BackgroundWorker1.RunWorkerAsync()  



Attach Button's Click Event ( Button beside the listbox)

Dim d As New OpenFileDialog 
        d.ShowDialog() 
        Me.ListBox1.Items.Add(d.FileName.ToString()) 
        Me.ListBox1.Visible = True  



Now its the Back Ground Worker ...
Background worker's Do work Event:

Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork 
        
        Dim i As Integer = 0 
        Dim smtp As New SmtpClient 
        smtp.Host = "smtp.gmail.com" 
        smtp.EnableSsl = True 
        smtp.Port = 587 
        smtp.Timeout = 300000 
        smtp.Credentials = New Net.NetworkCredential(Me.txtuser.Text, Me.txtpass.Text) 
        'Dim ms As MailMessage 
        'ms = e.Argument 
        Try 
            '  Label5.Text = "Sending Message......." 
            'Me.Button1.Text = "Sending...." 
            ' Me.Button1.Enabled = False 
            'Me.BackgroundWorker1.ReportProgress(i,  
            smtp.Send(msg) 
            e.Result = "Done" 
        Catch ex As Exception 
            'Label5.Text = "Sending Failed........" 
            MessageBox.Show(ex.ToString()) 
        End Try  


Background Worker's Run work Completed Event:

Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted 
        ' Label5.Text = "Message Sending Complete" 
        Me.ProgressBar1.MarqueeAnimationSpeed = 0 
        Me.Button1.Text = "Send" 
        Me.Button1.Enabled = True 
        MessageBox.Show("Sent") 
    End Sub  


And It's Done.

For Complete Project Email me at sandeepparekh9@gmail.com

转载于:https://www.cnblogs.com/samblog/archive/2012/04/01/2429274.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值