I'm trying to create a simple form that sends an email using my
company's exchange server. I'm using the System.Net.Mail Namespace of
the .net framework 2.0.
I've googled everywhere and i think my code is ok, however i keep
getting a "A connection attempt failed because the connected party did
not properly respond after a period of time, or established connection
failed because connected host has failed to respond" error every time.
Any one has any ideas why this is happening?
Imports System.Net.Mail
'Imports System.Web.Mail
Public Class frmsendmail
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
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
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form
Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
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 txtServidor As System.Windows.Forms.TextBox
Friend WithEvents CmdEnviar As System.Windows.Forms.Button
Friend WithEvents txtDe As System.Windows.Forms.TextBox
Friend WithEvents txtPara As System.Windows.Forms.TextBox
Friend WithEvents txtAssunto As System.Windows.Forms.TextBox
Friend WithEvents txtEmail As System.Windows.Forms.TextBox
<System.Diagnostics.DebuggerStepThrough()Private Sub
InitializeComponent()
Me.txtServidor = New System.Windows.Forms.TextBox
Me.CmdEnviar = 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.txtDe = New System.Windows.Forms.TextBox
Me.txtPara = New System.Windows.Forms.TextBox
Me.txtAssunto = New System.Windows.Forms.TextBox
Me.txtEmail = New System.Windows.Forms.TextBox
Me.SuspendLayout()
'
'txtServidor
'
Me.txtServidor.Location = New System.Drawing.Point(136, 56)
Me.txtServidor.Name = "txtServidor"
Me.txtServidor.Size = New System.Drawing.Size(168, 20)
Me.txtServidor.TabIndex = 0
Me.txtServidor.Text = ""
Me.txtServidor.Visible = False
'
'CmdEnviar
'
Me.CmdEnviar.Location = New System.Drawing.Point(344, 296)
Me.CmdEnviar.Name = "CmdEnviar"
Me.CmdEnviar.TabIndex = 1
Me.CmdEnviar.Text = "Enviar"
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(72, 56)
Me.Label1.Name = "Label1"
Me.Label1.RightToLeft = System.Windows.Forms.RightToLeft.No
Me.Label1.Size = New System.Drawing.Size(48, 23)
Me.Label1.TabIndex = 2
Me.Label1.Text = "Servidor:"
Me.Label1.Visible = False
'
'Label2
'
Me.Label2.Location = New System.Drawing.Point(96, 96)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(32, 23)
Me.Label2.TabIndex = 3
Me.Label2.Text = "De:"
'
'Label3
'
Me.Label3.Location = New System.Drawing.Point(88, 136)
Me.Label3.Name = "Label3"
Me.Label3.Size = New System.Drawing.Size(40, 23)
Me.Label3.TabIndex = 4
Me.Label3.Text = "Para:"
'
'Label4
'
Me.Label4.Location = New System.Drawing.Point(80, 168)
Me.Label4.Name = "Label4"
Me.Label4.Size = New System.Drawing.Size(48, 23)
Me.Label4.TabIndex = 5
Me.Label4.Text = "Assunto:"
'
'txtDe
'
Me.txtDe.Location = New System.Drawing.Point(136, 96)
Me.txtDe.Name = "txtDe"
Me.txtDe.Size = New System.Drawing.Size(168, 20)
Me.txtDe.TabIndex = 6
Me.txtDe.Text = ""
'
'txtPara
'
Me.txtPara.Location = New System.Drawing.Point(136, 136)
Me.txtPara.Name = "txtPara"
Me.txtPara.Size = New System.Drawing.Size(168, 20)
Me.txtPara.TabIndex = 7
Me.txtPara.Text = ""
'
'txtAssunto
'
Me.txtAssunto.Location = New System.Drawing.Point(136, 176)
Me.txtAssunto.Name = "txtAssunto"
Me.txtAssunto.Size = New System.Drawing.Size(168, 20)
Me.txtAssunto.TabIndex = 8
Me.txtAssunto.Text = ""
'
'txtEmail
'
Me.txtEmail.Location = New System.Drawing.Point(40, 224)
Me.txtEmail.Multiline = True
Me.txtEmail.Name = "txtEmail"
Me.txtEmail.Size = New System.Drawing.Size(264, 88)
Me.txtEmail.TabIndex = 9
Me.txtEmail.Text = ""
'
'frmsendmail
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(456, 390)
Me.Controls.Add(Me.txtEmail)
Me.Controls.Add(Me.txtAssunto)
Me.Controls.Add(Me.txtPara)
Me.Controls.Add(Me.txtDe)
Me.Controls.Add(Me.Label4)
Me.Controls.Add(Me.Label3)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.CmdEnviar)
Me.Controls.Add(Me.txtServidor)
Me.Name = "frmsendmail"
Me.Text = "sendmail"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub CmdEnviar_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles CmdEnviar.Click
Dim oEmail As New MailMessage
Dim oSmtpServer As New Net.Mail.SmtpClient()
Try
With oEmail
.From = New MailAddress(txtDe.Text)
.To.Add(txtPara.Text)
.IsBodyHtml = False
.Subject = Me.txtAssunto.Text
.Body = Me.txtEmail.Text
.Priority = MailPriority.High
End With
oSmtpServer.Host = "my.exchange.server"
oSmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network
oSmtpServer.Port = 25
oSmtpServer.Timeout = 900000
oSmtpServer.Credentials =
Net.CredentialCache.DefaultNetworkCredentials
oSmtpServer.Send(oEmail)
MsgBox("Email was sent", MsgBoxStyle.Information, "Info:")
Catch ex As Exception
MsgBox(ex.InnerException.InnerException.Message,
MsgBoxStyle.Exclamation, ex.Message)
Finally
oEmail = Nothing
oSmtpServer = Nothing
End Try
End Sub
End Class