主题:高手请进。。。求救 vb.net修改错误
最近编一邮件发送程序。。。运行正常,可是不能正常发送邮件。有谁能给改一下。。。。
[em11]
以下为代码:
Imports System
Imports System.Web
Imports System.Data
Imports System.Drawing
Imports System.Web.Mail
Imports System.Collections
Imports System.Windows.Forms
Imports System.ComponentModel
Public Class Form1
Inherits System.Windows.Forms.Form
Dim message As New MailMessage
Dim sendto As String
Dim sendfrom As String
Dim subject As String
Dim attachs As MailAttachment
#Region " Windows 窗体设计器生成的代码 "
Public Sub New()
MyBase.New()
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
'在 InitializeComponent() 调用之后添加任何初始化
End Sub
'窗体重写 dispose 以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer
'注意: 以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
Friend WithEvents RichTextBox1 As System.Windows.Forms.RichTextBox
Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
Friend WithEvents TextBox3 As System.Windows.Forms.TextBox
Friend WithEvents TextBox4 As System.Windows.Forms.TextBox
Friend WithEvents TextBox5 As System.Windows.Forms.TextBox
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Button2 As System.Windows.Forms.Button
Friend WithEvents Button3 As System.Windows.Forms.Button
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents Label3 As System.Windows.Forms.Label
Friend WithEvents Label4 As System.Windows.Forms.Label
Friend WithEvents Label5 As System.Windows.Forms.Label
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.RichTextBox1 = New System.Windows.Forms.RichTextBox
Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.TextBox2 = New System.Windows.Forms.TextBox
Me.TextBox3 = New System.Windows.Forms.TextBox
Me.TextBox4 = New System.Windows.Forms.TextBox
Me.TextBox5 = New System.Windows.Forms.TextBox
Me.Button1 = New System.Windows.Forms.Button
Me.Button2 = New System.Windows.Forms.Button
Me.Button3 = New System.Windows.Forms.Button
Me.Label1 = New System.Windows.Forms.Label
Me.Label2 = New System.Windows.Forms.Label
Me.Label3 = New System.Windows.Forms.Label
Me.Label4 = New System.Windows.Forms.Label
Me.Label5 = New System.Windows.Forms.Label
Me.SuspendLayout()
'
'RichTextBox1
'
Me.RichTextBox1.Location = New System.Drawing.Point(16, 200)
Me.RichTextBox1.Name = "RichTextBox1"
Me.RichTextBox1.Size = New System.Drawing.Size(312, 112)
Me.RichTextBox1.TabIndex = 0
Me.RichTextBox1.Text = ""
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(128, 16)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(112, 21)
Me.TextBox1.TabIndex = 1
Me.TextBox1.Text = ""
'
'TextBox2
'
Me.TextBox2.Location = New System.Drawing.Point(128, 48)
Me.TextBox2.Name = "TextBox2"
Me.TextBox2.Size = New System.Drawing.Size(112, 21)
Me.TextBox2.TabIndex = 2
Me.TextBox2.Text = ""
'
'TextBox3
'
Me.TextBox3.Location = New System.Drawing.Point(128, 80)
Me.TextBox3.Name = "TextBox3"
Me.TextBox3.Size = New System.Drawing.Size(112, 21)
Me.TextBox3.TabIndex = 3
Me.TextBox3.Text = ""
'
'TextBox4
'
Me.TextBox4.Location = New System.Drawing.Point(128, 112)
Me.TextBox4.Name = "TextBox4"
Me.TextBox4.Size = New System.Drawing.Size(112, 21)
Me.TextBox4.TabIndex = 4
Me.TextBox4.Text = ""
'
'TextBox5
'
Me.TextBox5.Location = New System.Drawing.Point(128, 144)
Me.TextBox5.Name = "TextBox5"
Me.TextBox5.Size = New System.Drawing.Size(112, 21)
Me.TextBox5.TabIndex = 5
Me.TextBox5.Text = ""
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(264, 32)
Me.Button1.Name = "Button1"
Me.Button1.TabIndex = 6
Me.Button1.Text = "发送"
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(264, 80)
Me.Button2.Name = "Button2"
Me.Button2.TabIndex = 7
Me.Button2.Text = "附件"
'
'Button3
'
Me.Button3.Location = New System.Drawing.Point(264, 128)
Me.Button3.Name = "Button3"
Me.Button3.TabIndex = 8
Me.Button3.Text = "退出"
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(24, 24)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(72, 16)
Me.Label1.TabIndex = 9
Me.Label1.Text = "收件人地址"
'
'Label2
'
Me.Label2.Location = New System.Drawing.Point(24, 56)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(72, 16)
Me.Label2.TabIndex = 10
Me.Label2.Text = "发件人地址"
'
'Label3
'
Me.Label3.Location = New System.Drawing.Point(40, 88)
Me.Label3.Name = "Label3"
Me.Label3.Size = New System.Drawing.Size(32, 16)
Me.Label3.TabIndex = 11
Me.Label3.Text = "主题"
'
'Label4
'
Me.Label4.Location = New System.Drawing.Point(40, 120)
Me.Label4.Name = "Label4"
Me.Label4.Size = New System.Drawing.Size(32, 16)
Me.Label4.TabIndex = 12
Me.Label4.Text = "附件"
'
'Label5
'
Me.Label5.Location = New System.Drawing.Point(40, 152)
Me.Label5.Name = "Label5"
Me.Label5.Size = New System.Drawing.Size(32, 16)
Me.Label5.TabIndex = 13
Me.Label5.Text = "抄送"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(368, 333)
Me.Controls.Add(Me.Label5)
Me.Controls.Add(Me.Label4)
Me.Controls.Add(Me.Label3)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.Button3)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.TextBox5)
Me.Controls.Add(Me.TextBox4)
Me.Controls.Add(Me.TextBox3)
Me.Controls.Add(Me.TextBox2)
Me.Controls.Add(Me.TextBox1)
Me.Controls.Add(Me.RichTextBox1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
sendto = Me.TextBox1.Text
sendfrom = Me.TextBox2.Text
If sendto = "" Then
MessageBox.Show("没有邮件目的地址,请重新输入!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Exit Sub
End If
message.To = sendto
message.From = sendfrom
message.BodyFormat = MailFormat.Text
message.BodyEncoding = System.Text.Encoding.UTF8
subject = Me.TextBox3.Text
Dim result As DialogResult
If subject = "" Then
result = MessageBox.Show("这封信没有标题,不要标题吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation)
End If
If result = DialogResult.No Then Exit Sub
message.Subject = subject
message.Body = Me.RichTextBox1.Text
If Me.TextBox4.Text = "" Then
Else
attachs = New MailAttachment(Me.TextBox4.Text)
message.Attachments.Add(attachs)
End If
If Me.TextBox5.Text = "" Then
Else
message.Cc = Me.TextBox5.Text
End If
SmtpMail.Send(message)
Catch
MessageBox.Show("邮件没有发送成功!原因是:" & Err.Description, " 错误", MessageBoxButtons.OK, MessageBoxIcon.Error)
MessageBox.Show("邮件已经成功发送完毕!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
Catch ex As Exception
End Try
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
With Me.OpenFileDialog1.Title = "打开"
OpenFileDialog1.Filter = "所有文件(*.*)|*.*"
OpenFileDialog1.ShowDialog()
Me.TextBox4.Text = OpenFileDialog1.FileName
End With
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Me.Close()
End Sub
End Class
[img]http://[/img]
[em11]
以下为代码:
Imports System
Imports System.Web
Imports System.Data
Imports System.Drawing
Imports System.Web.Mail
Imports System.Collections
Imports System.Windows.Forms
Imports System.ComponentModel
Public Class Form1
Inherits System.Windows.Forms.Form
Dim message As New MailMessage
Dim sendto As String
Dim sendfrom As String
Dim subject As String
Dim attachs As MailAttachment
#Region " Windows 窗体设计器生成的代码 "
Public Sub New()
MyBase.New()
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
'在 InitializeComponent() 调用之后添加任何初始化
End Sub
'窗体重写 dispose 以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer
'注意: 以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
Friend WithEvents RichTextBox1 As System.Windows.Forms.RichTextBox
Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
Friend WithEvents TextBox3 As System.Windows.Forms.TextBox
Friend WithEvents TextBox4 As System.Windows.Forms.TextBox
Friend WithEvents TextBox5 As System.Windows.Forms.TextBox
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Button2 As System.Windows.Forms.Button
Friend WithEvents Button3 As System.Windows.Forms.Button
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents Label3 As System.Windows.Forms.Label
Friend WithEvents Label4 As System.Windows.Forms.Label
Friend WithEvents Label5 As System.Windows.Forms.Label
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.RichTextBox1 = New System.Windows.Forms.RichTextBox
Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.TextBox2 = New System.Windows.Forms.TextBox
Me.TextBox3 = New System.Windows.Forms.TextBox
Me.TextBox4 = New System.Windows.Forms.TextBox
Me.TextBox5 = New System.Windows.Forms.TextBox
Me.Button1 = New System.Windows.Forms.Button
Me.Button2 = New System.Windows.Forms.Button
Me.Button3 = New System.Windows.Forms.Button
Me.Label1 = New System.Windows.Forms.Label
Me.Label2 = New System.Windows.Forms.Label
Me.Label3 = New System.Windows.Forms.Label
Me.Label4 = New System.Windows.Forms.Label
Me.Label5 = New System.Windows.Forms.Label
Me.SuspendLayout()
'
'RichTextBox1
'
Me.RichTextBox1.Location = New System.Drawing.Point(16, 200)
Me.RichTextBox1.Name = "RichTextBox1"
Me.RichTextBox1.Size = New System.Drawing.Size(312, 112)
Me.RichTextBox1.TabIndex = 0
Me.RichTextBox1.Text = ""
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(128, 16)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(112, 21)
Me.TextBox1.TabIndex = 1
Me.TextBox1.Text = ""
'
'TextBox2
'
Me.TextBox2.Location = New System.Drawing.Point(128, 48)
Me.TextBox2.Name = "TextBox2"
Me.TextBox2.Size = New System.Drawing.Size(112, 21)
Me.TextBox2.TabIndex = 2
Me.TextBox2.Text = ""
'
'TextBox3
'
Me.TextBox3.Location = New System.Drawing.Point(128, 80)
Me.TextBox3.Name = "TextBox3"
Me.TextBox3.Size = New System.Drawing.Size(112, 21)
Me.TextBox3.TabIndex = 3
Me.TextBox3.Text = ""
'
'TextBox4
'
Me.TextBox4.Location = New System.Drawing.Point(128, 112)
Me.TextBox4.Name = "TextBox4"
Me.TextBox4.Size = New System.Drawing.Size(112, 21)
Me.TextBox4.TabIndex = 4
Me.TextBox4.Text = ""
'
'TextBox5
'
Me.TextBox5.Location = New System.Drawing.Point(128, 144)
Me.TextBox5.Name = "TextBox5"
Me.TextBox5.Size = New System.Drawing.Size(112, 21)
Me.TextBox5.TabIndex = 5
Me.TextBox5.Text = ""
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(264, 32)
Me.Button1.Name = "Button1"
Me.Button1.TabIndex = 6
Me.Button1.Text = "发送"
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(264, 80)
Me.Button2.Name = "Button2"
Me.Button2.TabIndex = 7
Me.Button2.Text = "附件"
'
'Button3
'
Me.Button3.Location = New System.Drawing.Point(264, 128)
Me.Button3.Name = "Button3"
Me.Button3.TabIndex = 8
Me.Button3.Text = "退出"
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(24, 24)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(72, 16)
Me.Label1.TabIndex = 9
Me.Label1.Text = "收件人地址"
'
'Label2
'
Me.Label2.Location = New System.Drawing.Point(24, 56)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(72, 16)
Me.Label2.TabIndex = 10
Me.Label2.Text = "发件人地址"
'
'Label3
'
Me.Label3.Location = New System.Drawing.Point(40, 88)
Me.Label3.Name = "Label3"
Me.Label3.Size = New System.Drawing.Size(32, 16)
Me.Label3.TabIndex = 11
Me.Label3.Text = "主题"
'
'Label4
'
Me.Label4.Location = New System.Drawing.Point(40, 120)
Me.Label4.Name = "Label4"
Me.Label4.Size = New System.Drawing.Size(32, 16)
Me.Label4.TabIndex = 12
Me.Label4.Text = "附件"
'
'Label5
'
Me.Label5.Location = New System.Drawing.Point(40, 152)
Me.Label5.Name = "Label5"
Me.Label5.Size = New System.Drawing.Size(32, 16)
Me.Label5.TabIndex = 13
Me.Label5.Text = "抄送"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(368, 333)
Me.Controls.Add(Me.Label5)
Me.Controls.Add(Me.Label4)
Me.Controls.Add(Me.Label3)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.Button3)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.TextBox5)
Me.Controls.Add(Me.TextBox4)
Me.Controls.Add(Me.TextBox3)
Me.Controls.Add(Me.TextBox2)
Me.Controls.Add(Me.TextBox1)
Me.Controls.Add(Me.RichTextBox1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
sendto = Me.TextBox1.Text
sendfrom = Me.TextBox2.Text
If sendto = "" Then
MessageBox.Show("没有邮件目的地址,请重新输入!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Exit Sub
End If
message.To = sendto
message.From = sendfrom
message.BodyFormat = MailFormat.Text
message.BodyEncoding = System.Text.Encoding.UTF8
subject = Me.TextBox3.Text
Dim result As DialogResult
If subject = "" Then
result = MessageBox.Show("这封信没有标题,不要标题吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation)
End If
If result = DialogResult.No Then Exit Sub
message.Subject = subject
message.Body = Me.RichTextBox1.Text
If Me.TextBox4.Text = "" Then
Else
attachs = New MailAttachment(Me.TextBox4.Text)
message.Attachments.Add(attachs)
End If
If Me.TextBox5.Text = "" Then
Else
message.Cc = Me.TextBox5.Text
End If
SmtpMail.Send(message)
Catch
MessageBox.Show("邮件没有发送成功!原因是:" & Err.Description, " 错误", MessageBoxButtons.OK, MessageBoxIcon.Error)
MessageBox.Show("邮件已经成功发送完毕!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
Catch ex As Exception
End Try
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
With Me.OpenFileDialog1.Title = "打开"
OpenFileDialog1.Filter = "所有文件(*.*)|*.*"
OpenFileDialog1.ShowDialog()
Me.TextBox4.Text = OpenFileDialog1.FileName
End With
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Me.Close()
End Sub
End Class
[img]http://[/img]