Send Email Using VB 2008 Express

  • Thread starter Thread starter ryguy7272
  • Start date Start date
ryguy7272 said:
No errors in Error List, but I got this message when the code fired:
SMTPFailedRecipientException was unhandled
Mailbox unavailable. The server was: 5.7.1 Unable to relay

This line is now yellow:
client.Send(mail)

Any thoughts on that? Another Reference maybe?

It means you didn't authenticate (login).

We write a commercial email server and participate in the IETF SMTP
working groups. I know something about this.

By tradition, the public SMTP port 25 has the following rule:

- local recipients, no authentication is required
- remote recipients, authentication is required (optional)

The first one is practical a must otherwise the email industry would
break down if you had to authenticate. No one would be able send you
mail.

However, sending to remote addresses often requires authentication
otherwise the BOX becomes an open relay which is what spammers look
for. So these days, a OPEN RELAY is a taboo and you must authenticate.

That is what the "5.7.1 unable to relay" response meant. Actually,
5.7.1 is an extended code and not anything official. The full
responses would had a 55x or 45x number like:

55x 5.7.1 Unable to relay.

In other words, the strong after the 55x is for logging and humans,
but the 55x is the state machine control reply code.

55x ---> Permanent Failure, STOP TRYING
45x ---> Temporary Failure, You MAY try again later

So based on what you said, it looks like the code is correct, but you
need the add the lines for the userid and password login.

Alternatively, the SERVER may have other means to authenticate you.
The general features are:

o ESMTP AUTH

login with id and password

o ALLOW IP RELAY TABLE

The server sees your IP in a table and allows you to relay.

o POP BEFORE SMTP (POPB4SMTP)

This is a popular trick of the ALLOW IP TABLE. Most
MUA (Mail User Agents) like OE will do a POP before SEND
mail.

Since the POP3 Server and SMTP Server are usually within
the same domain network, the POP3 server can send a
signal to the SMTP server to open a temporary time window
for Allow IP Relay.

The latter is very popular among our ISPs because

- it reduces support headaches with their users - less need to
explain how to login via the MUA

- They don't need to permanent add an IP to the table,

- And since most users are roamers, the IP changes, it resolves
the open relay situation for their users.

So if you have control of your server or you can POP into it first
before sending, then maybe you don't need to login.

--
 
ryguy7272 said:
No errors in Error List, but I got this message when the code fired:
SMTPFailedRecipientException was unhandled
Mailbox unavailable. The server was: 5.7.1 Unable to relay

This line is now yellow:
client.Send(mail)

Any thoughts on that? Another Reference maybe?

It means you didn't authenticate (login).

We write a commercial email server and participate in the IETF SMTP
working groups. I know something about this.

By tradition, the public SMTP port 25 has the following rule:

- local recipients, no authentication is required
- remote recipients, authentication is required (optional)

The first one is practical a must otherwise the email industry would
break down if you had to authenticate. No one would be able send you
mail.

However, sending to remote addresses often requires authentication
otherwise the BOX becomes an open relay which is what spammers look
for. So these days, a OPEN RELAY is a taboo and you must authenticate.

That is what the "5.7.1 unable to relay" response meant. Actually,
5.7.1 is an extended code and not anything official. The full
responses would had a 55x or 45x number like:

55x 5.7.1 Unable to relay.

In other words, the strong after the 55x is for logging and humans,
but the 55x is the state machine control reply code.

55x ---> Permanent Failure, STOP TRYING
45x ---> Temporary Failure, You MAY try again later

So based on what you said, it looks like the code is correct, but you
need the add the lines for the userid and password login.

Alternatively, the SERVER may have other means to authenticate you.
The general features are:

o ESMTP AUTH

login with id and password

o ALLOW IP RELAY TABLE

The server sees your IP in a table and allows you to relay.

o POP BEFORE SMTP (POPB4SMTP)

This is a popular trick of the ALLOW IP TABLE. Most
MUA (Mail User Agents) like OE will do a POP before SEND
mail.

Since the POP3 Server and SMTP Server are usually within
the same domain network, the POP3 server can send a
signal to the SMTP server to open a temporary time window
for Allow IP Relay.

The latter is very popular among our ISPs because

- it reduces support headaches with their users - less need to
explain how to login via the MUA

- They don't need to permanent add an IP to the table,

- And since most users are roamers, the IP changes, it resolves
the open relay situation for their users.

So if you have control of your server or you can POP into it first
before sending, then maybe you don't need to login.

--
 
Mike said:
It means you didn't authenticate (login).

We write a commercial email server and participate in the IETF SMTP
working groups. I know something about this.

By tradition, the public SMTP port 25 has the following rule:

- local recipients, no authentication is required
- remote recipients, authentication is required (optional)

Small follow ups.

The reason I made the distinction of "public SMTP port 25" is because
we also have the private SMTP port 587 A.K.A. Submit Protocol.

Port 587 gives the server (legal) authorization to enforce
authentication at all levels, sending mail to local users as well.

If port 587 was used, then you would have gotten an earlier failure
response like "Authentication Required" before it even allowed you to
send the recipient address to check if its local or remote.
Alternatively, the SERVER may have other means to authenticate you. The
general features are:

o ESMTP AUTH

login with id and password

The E in ESMTP means Extended which means optional feature,
authentication is not required unless the SUBMIT PROTOCOL (port 587)
is used where AUTH is required.

Port 587 is the direction most ISP are pushing people to send mail
via SMTP to alleviate many of the abusive issues evolved from the
legacy Local Recipient, No Authentication practice for Port 25.

--
 
Mike said:
It means you didn't authenticate (login).

We write a commercial email server and participate in the IETF SMTP
working groups. I know something about this.

By tradition, the public SMTP port 25 has the following rule:

- local recipients, no authentication is required
- remote recipients, authentication is required (optional)

Small follow ups.

The reason I made the distinction of "public SMTP port 25" is because
we also have the private SMTP port 587 A.K.A. Submit Protocol.

Port 587 gives the server (legal) authorization to enforce
authentication at all levels, sending mail to local users as well.

If port 587 was used, then you would have gotten an earlier failure
response like "Authentication Required" before it even allowed you to
send the recipient address to check if its local or remote.
Alternatively, the SERVER may have other means to authenticate you. The
general features are:

o ESMTP AUTH

login with id and password

The E in ESMTP means Extended which means optional feature,
authentication is not required unless the SUBMIT PROTOCOL (port 587)
is used where AUTH is required.

Port 587 is the direction most ISP are pushing people to send mail
via SMTP to alleviate many of the abusive issues evolved from the
legacy Local Recipient, No Authentication practice for Port 25.

--
 
Everyone, thank you soooo much for the help! I started this project 1 week
ago, and just finished today. As I alluded to before, I am a VBA guy, trying
to become a VB.NET guy. Everyone's guidance was helpful, but in the end I
just couldn't figure out how the SMTP server stuff worked, and that was the
hangup for me. I eventually got help from a guy in my office. Thanks
Sankalp!!

So, anyway, I want to share the final product, so others can benefit from
this experience. FYI, you need a Gmail account:

Create a Form, and name it 'frmMain'
Create a To TextBox, and name it 'txtTo'
Create a CC TextBox, and name it 'txtCC'
Create a BCC TextBox, and name it 'txtBCC'
Create a Subject TextBox, and name it 'txtSubject'
Create an email Body TextBox, and name it 'txtBody'
Then, create a Send Button, and name it 'btnSend'

This code goes under the Form:
Imports System.Net.Mail

Public Class frmMain

Private _Username As String
Private _Password As String

Public Property UserName() As String
Get
Return _Username

End Get
Set(ByVal value As String)
_Username = value

End Set
End Property

Public Property Password() As String
Get
Return _Password

End Get
Set(ByVal value As String)
_Password = value

End Set
End Property

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


'Start by creating a mail message object
Dim MyMailMessage As New MailMessage()

'From requires an instance of the MailAddress type
Dim _from As String = UserName & "@gmail.com"

MyMailMessage.From = New MailAddress(_from)

'To is a collection of MailAddress types
If txtTo.Text <> "" Then
MyMailMessage.To.Add(txtTo.Text)
Else
MessageBox.Show("You need to enter atlest one e-mail address")
End If


If txtBCC.Text <> "" Then
MyMailMessage.Bcc.Add(txtBCC.Text)
End If

If txtCC.Text <> "" Then
MyMailMessage.CC.Add(txtCC.Text)
End If

MyMailMessage.Subject = txtSubject.Text
MyMailMessage.Body = txtBody.Text

'Create the SMTPClient object and specify the SMTP GMail server
Dim SMTPServer As New SmtpClient("smtp.gmail.com")
SMTPServer.Port = 587
' SMTPServer.Credentials = New
System.Net.NetworkCredential("sankalppande", "Bittu1541982")
SMTPServer.Credentials = New System.Net.NetworkCredential(UserName,
Password)
SMTPServer.EnableSsl = True

Try
SMTPServer.Send(MyMailMessage)
MessageBox.Show("Email Sent")
Catch ex As SmtpException
MessageBox.Show(ex.Message)
End Try
End Sub
End Class


Finally...yes, almost done...
Create another Form, and name it 'LoginForm'
Create a UserName TextBox and name it 'UsernameTextBox'
Create a Password TextBox and name it 'PasswordTextBox'
Create a Button named 'Cancel' and one more Button named 'OK'
This code goes under the Form:
Public Class LoginForm

' TODO: Insert code to perform custom authentication using the provided
username and password
' (See http://go.microsoft.com/fwlink/?LinkId=35339).
' The custom principal can then be attached to the current thread's
principal as follows:
' My.User.CurrentPrincipal = CustomPrincipal
' where CustomPrincipal is the IPrincipal implementation used to perform
authentication.
' Subsequently, My.User will return identity information encapsulated in
the CustomPrincipal object
' such as the username, display name, etc.

Private Sub OK_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles OK.Click

frmMain.UserName = Me.UsernameTextBox.Text
frmMain.Password = Me.PasswordTextBox.Text
frmMain.Show()
Me.Visible = False

End Sub

Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Cancel.Click
Me.Close()
End Sub
End Class

HTH,
Ryan---
 
Everyone, thank you soooo much for the help! I started this project 1 week
ago, and just finished today. As I alluded to before, I am a VBA guy, trying
to become a VB.NET guy. Everyone's guidance was helpful, but in the end I
just couldn't figure out how the SMTP server stuff worked, and that was the
hangup for me. I eventually got help from a guy in my office. Thanks
Sankalp!!

So, anyway, I want to share the final product, so others can benefit from
this experience. FYI, you need a Gmail account:

Create a Form, and name it 'frmMain'
Create a To TextBox, and name it 'txtTo'
Create a CC TextBox, and name it 'txtCC'
Create a BCC TextBox, and name it 'txtBCC'
Create a Subject TextBox, and name it 'txtSubject'
Create an email Body TextBox, and name it 'txtBody'
Then, create a Send Button, and name it 'btnSend'

This code goes under the Form:
Imports System.Net.Mail

Public Class frmMain

Private _Username As String
Private _Password As String

Public Property UserName() As String
Get
Return _Username

End Get
Set(ByVal value As String)
_Username = value

End Set
End Property

Public Property Password() As String
Get
Return _Password

End Get
Set(ByVal value As String)
_Password = value

End Set
End Property

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


'Start by creating a mail message object
Dim MyMailMessage As New MailMessage()

'From requires an instance of the MailAddress type
Dim _from As String = UserName & "@gmail.com"

MyMailMessage.From = New MailAddress(_from)

'To is a collection of MailAddress types
If txtTo.Text <> "" Then
MyMailMessage.To.Add(txtTo.Text)
Else
MessageBox.Show("You need to enter atlest one e-mail address")
End If


If txtBCC.Text <> "" Then
MyMailMessage.Bcc.Add(txtBCC.Text)
End If

If txtCC.Text <> "" Then
MyMailMessage.CC.Add(txtCC.Text)
End If

MyMailMessage.Subject = txtSubject.Text
MyMailMessage.Body = txtBody.Text

'Create the SMTPClient object and specify the SMTP GMail server
Dim SMTPServer As New SmtpClient("smtp.gmail.com")
SMTPServer.Port = 587
' SMTPServer.Credentials = New
System.Net.NetworkCredential("sankalppande", "Bittu1541982")
SMTPServer.Credentials = New System.Net.NetworkCredential(UserName,
Password)
SMTPServer.EnableSsl = True

Try
SMTPServer.Send(MyMailMessage)
MessageBox.Show("Email Sent")
Catch ex As SmtpException
MessageBox.Show(ex.Message)
End Try
End Sub
End Class


Finally...yes, almost done...
Create another Form, and name it 'LoginForm'
Create a UserName TextBox and name it 'UsernameTextBox'
Create a Password TextBox and name it 'PasswordTextBox'
Create a Button named 'Cancel' and one more Button named 'OK'
This code goes under the Form:
Public Class LoginForm

' TODO: Insert code to perform custom authentication using the provided
username and password
' (See http://go.microsoft.com/fwlink/?LinkId=35339).
' The custom principal can then be attached to the current thread's
principal as follows:
' My.User.CurrentPrincipal = CustomPrincipal
' where CustomPrincipal is the IPrincipal implementation used to perform
authentication.
' Subsequently, My.User will return identity information encapsulated in
the CustomPrincipal object
' such as the username, display name, etc.

Private Sub OK_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles OK.Click

frmMain.UserName = Me.UsernameTextBox.Text
frmMain.Password = Me.PasswordTextBox.Text
frmMain.Show()
Me.Visible = False

End Sub

Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Cancel.Click
Me.Close()
End Sub
End Class

HTH,
Ryan---
 
ryguy7272 said:
' SMTPServer.Credentials = New
System.Net.NetworkCredential("X," "X")

You'd better get him to change his gmail password right now. Always blank
out things like real usernames and passwords when posting.

Andrew
 
ryguy7272 said:
' SMTPServer.Credentials = New
System.Net.NetworkCredential("X," "X")

You'd better get him to change his gmail password right now. Always blank
out things like real usernames and passwords when posting.

Andrew
 
Back
Top