authentication problems sending mail (unable to relay)

G

Guest

Hi all

i need some help. i try to send mail with the following code.

Private Sub LinkButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LinkButton1.Click
Dim mailMsg As New MailMessage
mailMsg.From = (e-mail address removed)
mailMsg.To = (e-mail address removed)
mailMsg.Subject = "Testmail vom Webforum"
mailMsg.Body = "some body text"
SmtpMail.SmtpServer = "localhost"
SmtpMail.SmtpServer()
Try
SmtpMail.Send(mailMsg)
Catch ex As Exception
Response.Write(ex.Message)
End Try
End Sub
End Module

it works fine as long as i use a user which is located in ADS and the target address is on the same server.

if i try to send mail with the same account in outlook it works fine as long as i set the prperty "my server requires authentication" if i do not set this flag the email is not sent out with the message "unable to relay".

now my question is it possible to set this property within the code somehow or how to solve this?

thank you in advance

Rene
 
O

One Handed Man [ OHM# ]

Add a reference to system.web and try this code out.

Regards - OHM#

Option Explicit On

Option Strict On

Option Compare Binary

Imports System

Imports System.Text

Imports System.Web.Mail

Imports System.Windows.Forms



Public Class MainForm

Inherits System.Windows.Forms.Form

#Region " Vom Windows Form Designer generierter Code "

Public Sub New()

MyBase.New()

InitializeComponent()

' Eigener Initialisierungscode.

Me.txtFrom.Text = "(e-mail address removed)"

Me.txtTo.Text = "(e-mail address removed)"

Me.txtSubject.Text = "Enter title here..."

Me.txtBody.Text = "Write your message here..."

Me.cboPriority.SelectedIndex = 1

End Sub

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

Private components As System.ComponentModel.IContainer

Friend WithEvents btnSend 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 txtFrom As System.Windows.Forms.TextBox

Friend WithEvents txtTo As System.Windows.Forms.TextBox

Friend WithEvents txtCC As System.Windows.Forms.TextBox

Friend WithEvents txtBCC As System.Windows.Forms.TextBox

Friend WithEvents Label5 As System.Windows.Forms.Label

Friend WithEvents txtSubject As System.Windows.Forms.TextBox

Friend WithEvents txtBody As System.Windows.Forms.TextBox

Friend WithEvents lstAttachments As System.Windows.Forms.ListBox

Friend WithEvents Label6 As System.Windows.Forms.Label

Friend WithEvents cboPriority As System.Windows.Forms.ComboBox

Friend WithEvents btnRemoveAttachment As System.Windows.Forms.Button

Friend WithEvents btnAddAttachment As System.Windows.Forms.Button

Friend WithEvents Label7 As System.Windows.Forms.Label

Friend WithEvents Label8 As System.Windows.Forms.Label

<System.Diagnostics.DebuggerStepThrough()> _

Private Sub InitializeComponent()

Me.btnSend = New System.Windows.Forms.Button()

Me.Label1 = New System.Windows.Forms.Label()

Me.txtFrom = New System.Windows.Forms.TextBox()

Me.Label2 = New System.Windows.Forms.Label()

Me.txtTo = New System.Windows.Forms.TextBox()

Me.Label3 = New System.Windows.Forms.Label()

Me.txtCC = New System.Windows.Forms.TextBox()

Me.Label4 = New System.Windows.Forms.Label()

Me.txtBCC = New System.Windows.Forms.TextBox()

Me.Label5 = New System.Windows.Forms.Label()

Me.txtSubject = New System.Windows.Forms.TextBox()

Me.txtBody = New System.Windows.Forms.TextBox()

Me.lstAttachments = New System.Windows.Forms.ListBox()

Me.Label6 = New System.Windows.Forms.Label()

Me.cboPriority = New System.Windows.Forms.ComboBox()

Me.btnRemoveAttachment = New System.Windows.Forms.Button()

Me.btnAddAttachment = New System.Windows.Forms.Button()

Me.Label7 = New System.Windows.Forms.Label()

Me.Label8 = New System.Windows.Forms.Label()

Me.SuspendLayout()

'

'btnSend

'

Me.btnSend.FlatStyle = System.Windows.Forms.FlatStyle.System

Me.btnSend.Location = New System.Drawing.Point(187, 320)

Me.btnSend.Name = "btnSend"

Me.btnSend.TabIndex = 18

Me.btnSend.Text = "&Send"

'

'Label1

'

Me.Label1.FlatStyle = System.Windows.Forms.FlatStyle.System

Me.Label1.Location = New System.Drawing.Point(16, 18)

Me.Label1.Name = "Label1"

Me.Label1.Size = New System.Drawing.Size(48, 16)

Me.Label1.TabIndex = 0

Me.Label1.Text = "&From:"

'

'txtFrom

'

Me.txtFrom.Location = New System.Drawing.Point(64, 16)

Me.txtFrom.Name = "txtFrom"

Me.txtFrom.Size = New System.Drawing.Size(192, 20)

Me.txtFrom.TabIndex = 1

Me.txtFrom.Text = ""

'

'Label2

'

Me.Label2.FlatStyle = System.Windows.Forms.FlatStyle.System

Me.Label2.Location = New System.Drawing.Point(16, 42)

Me.Label2.Name = "Label2"

Me.Label2.Size = New System.Drawing.Size(48, 16)

Me.Label2.TabIndex = 2

Me.Label2.Text = "&To:"

'

'txtTo

'

Me.txtTo.Location = New System.Drawing.Point(64, 40)

Me.txtTo.Name = "txtTo"

Me.txtTo.Size = New System.Drawing.Size(192, 20)

Me.txtTo.TabIndex = 3

Me.txtTo.Text = ""

'

'Label3

'

Me.Label3.FlatStyle = System.Windows.Forms.FlatStyle.System

Me.Label3.Location = New System.Drawing.Point(16, 66)

Me.Label3.Name = "Label3"

Me.Label3.Size = New System.Drawing.Size(48, 16)

Me.Label3.TabIndex = 4

Me.Label3.Text = "&CC:"

'

'txtCC

'

Me.txtCC.Location = New System.Drawing.Point(64, 64)

Me.txtCC.Name = "txtCC"

Me.txtCC.Size = New System.Drawing.Size(192, 20)

Me.txtCC.TabIndex = 5

Me.txtCC.Text = ""

'

'Label4

'

Me.Label4.FlatStyle = System.Windows.Forms.FlatStyle.System

Me.Label4.Location = New System.Drawing.Point(16, 90)

Me.Label4.Name = "Label4"

Me.Label4.Size = New System.Drawing.Size(48, 16)

Me.Label4.TabIndex = 6

Me.Label4.Text = "&BCC:"

'

'txtBCC

'

Me.txtBCC.Location = New System.Drawing.Point(64, 88)

Me.txtBCC.Name = "txtBCC"

Me.txtBCC.Size = New System.Drawing.Size(192, 20)

Me.txtBCC.TabIndex = 7

Me.txtBCC.Text = ""

'

'Label5

'

Me.Label5.FlatStyle = System.Windows.Forms.FlatStyle.System

Me.Label5.Location = New System.Drawing.Point(16, 114)

Me.Label5.Name = "Label5"

Me.Label5.Size = New System.Drawing.Size(48, 16)

Me.Label5.TabIndex = 8

Me.Label5.Text = "S&ubject:"

'

'txtSubject

'

Me.txtSubject.Location = New System.Drawing.Point(64, 112)

Me.txtSubject.Name = "txtSubject"

Me.txtSubject.Size = New System.Drawing.Size(192, 20)

Me.txtSubject.TabIndex = 9

Me.txtSubject.Text = ""

'

'txtBody

'

Me.txtBody.Location = New System.Drawing.Point(16, 168)

Me.txtBody.Multiline = True

Me.txtBody.Name = "txtBody"

Me.txtBody.ScrollBars = System.Windows.Forms.ScrollBars.Both

Me.txtBody.Size = New System.Drawing.Size(416, 144)

Me.txtBody.TabIndex = 13

Me.txtBody.Text = ""

'

'lstAttachments

'

Me.lstAttachments.Location = New System.Drawing.Point(264, 32)

Me.lstAttachments.Name = "lstAttachments"

Me.lstAttachments.Size = New System.Drawing.Size(168, 95)

Me.lstAttachments.TabIndex = 15

'

'Label6

'

Me.Label6.FlatStyle = System.Windows.Forms.FlatStyle.System

Me.Label6.Location = New System.Drawing.Point(264, 16)

Me.Label6.Name = "Label6"

Me.Label6.Size = New System.Drawing.Size(64, 16)

Me.Label6.TabIndex = 14

Me.Label6.Text = "&Attachments:"

'

'cboPriority

'

Me.cboPriority.DropDownStyle =
System.Windows.Forms.ComboBoxStyle.DropDownList

Me.cboPriority.Items.AddRange(New Object() {"Low", "Normal", "High"})

Me.cboPriority.Location = New System.Drawing.Point(168, 136)

Me.cboPriority.Name = "cboPriority"

Me.cboPriority.Size = New System.Drawing.Size(88, 21)

Me.cboPriority.TabIndex = 11

'

'btnRemoveAttachment

'

Me.btnRemoveAttachment.FlatStyle = System.Windows.Forms.FlatStyle.System

Me.btnRemoveAttachment.Location = New System.Drawing.Point(352, 134)

Me.btnRemoveAttachment.Name = "btnRemoveAttachment"

Me.btnRemoveAttachment.Size = New System.Drawing.Size(56, 24)

Me.btnRemoveAttachment.TabIndex = 17

Me.btnRemoveAttachment.Text = "&Remove"

'

'btnAddAttachment

'

Me.btnAddAttachment.FlatStyle = System.Windows.Forms.FlatStyle.System

Me.btnAddAttachment.Location = New System.Drawing.Point(288, 134)

Me.btnAddAttachment.Name = "btnAddAttachment"

Me.btnAddAttachment.Size = New System.Drawing.Size(56, 24)

Me.btnAddAttachment.TabIndex = 16

Me.btnAddAttachment.Text = "A&dd..."

'

'Label7

'

Me.Label7.FlatStyle = System.Windows.Forms.FlatStyle.System

Me.Label7.Location = New System.Drawing.Point(120, 138)

Me.Label7.Name = "Label7"

Me.Label7.Size = New System.Drawing.Size(48, 16)

Me.Label7.TabIndex = 10

Me.Label7.Text = "&Priotity:"

'

'Label8

'

Me.Label8.FlatStyle = System.Windows.Forms.FlatStyle.System

Me.Label8.Location = New System.Drawing.Point(16, 152)

Me.Label8.Name = "Label8"

Me.Label8.Size = New System.Drawing.Size(48, 16)

Me.Label8.TabIndex = 12

Me.Label8.Text = "&Message:"

'

'MainForm

'

Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)

Me.ClientSize = New System.Drawing.Size(448, 360)

Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Label8,
Me.Label7, Me.btnAddAttachment, Me.btnRemoveAttachment, Me.cboPriority,
Me.Label6, Me.lstAttachments, Me.txtBody, Me.Label5, Me.txtSubject,
Me.Label4, Me.txtBCC, Me.Label3, Me.txtCC, Me.Label2, Me.txtTo, Me.Label1,
Me.txtFrom, Me.btnSend})

Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle

Me.MaximizeBox = False

Me.Name = "MainForm"

Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen

Me.Text = "SendMail"

Me.ResumeLayout(False)

End Sub

#End Region

Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnSend.Click

Dim mailmsg As New MailMessage()

With mailmsg

..From = Me.txtFrom.Text.Trim()

..To = Me.txtTo.Text.Trim()

..Cc = Me.txtCC.Text.Trim()

..Bcc = Me.txtBCC.Text.Trim()

..Subject = Me.txtSubject.Text.Trim()

..Body = Me.txtBody.Text.Trim()

Select Case Me.cboPriority.SelectedIndex

Case 0

..Priority = MailPriority.Low

Case 1

..Priority = MailPriority.Normal

Case 2

..Priority = MailPriority.High

End Select

Dim s As String

For Each s In Me.lstAttachments.Items

..Attachments.Add(New MailAttachment(s))

Next s

End With

Try

SmtpMail.Send(mailmsg)

MessageBox.Show("Your mail has been successfully sent!")

Catch ex As Exception

MessageBox.Show("The following problem occurred when attempting to send your
mail: " & ex.Message)

End Try

End Sub

Private Sub btnAddAttachment_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnAddAttachment.Click

Dim ofdlg As New OpenFileDialog()

ofdlg.CheckFileExists = True

ofdlg.CheckPathExists = True

If ofdlg.ShowDialog(Me) = DialogResult.OK Then

Me.lstAttachments.Items.Add(ofdlg.FileName)

End If

End Sub

Private Sub btnRemoveAttachment_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnRemoveAttachment.Click

If Me.lstAttachments.SelectedIndex >= 0 Then

Me.lstAttachments.Items.Remove(Me.lstAttachments.SelectedItem)

End If

End Sub

End Class


Hi all

i need some help. i try to send mail with the following code.

Private Sub LinkButton1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles LinkButton1.Click Dim
mailMsg As New MailMessage mailMsg.From = (e-mail address removed)
mailMsg.To = (e-mail address removed)
mailMsg.Subject = "Testmail vom Webforum"
mailMsg.Body = "some body text"
SmtpMail.SmtpServer = "localhost"
SmtpMail.SmtpServer()
Try
SmtpMail.Send(mailMsg)
Catch ex As Exception
Response.Write(ex.Message)
End Try
End Sub
End Module

it works fine as long as i use a user which is located in ADS and the
target address is on the same server.

if i try to send mail with the same account in outlook it works fine
as long as i set the prperty "my server requires authentication" if i
do not set this flag the email is not sent out with the message
"unable to relay".

now my question is it possible to set this property within the code
somehow or how to solve this?

thank you in advance

Rene

Regards - OHM# (e-mail address removed)
 
G

Guest

I will tra your code also. thank you for help

i've found a solution which work fine for me.


Private Sub LinkButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LinkButton1.Click
Dim mailMsg As New MailMessage

mailMsg.From = "(e-mail address removed)"
mailMsg.To = "(e-mail address removed)"
mailMsg.Subject = "Testmail from Info"
mailMsg.Body = "some body Text"
SmtpMail.SmtpServer = System.Configuration.ConfigurationSettings.AppSettings.Get("localhost")

Try
SmtpMail.Send(mailMsg)
Catch ex As Exception
Response.Write(ex.Message)
End Try
End Sub

Regards, Rene
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top