0

I am trying to send an email using asp.net with VB. The email send through company server. I have many suggestion from the internet but the error always the same. 'Failure Sending Mail'

Here is my code so far:

Protected Sub btnSend_Click(sender As Object, e As EventArgs) Handles btnSend.Click
    Try
        Dim message As New MailMessage()
        Dim client As New SmtpClient("blablabla@myHost.com")
        message.From = New MailAddress(txtFrom.Text)
        message.To.Add(New MailAddress(txtTo.Text))
        message.Subject = txtSubject.Text
        message.Body = txtMessage.Text
        message.IsBodyHtml = True
        message.Priority = MailPriority.High
        client.Port = 25
        client.EnableSsl = True
        client.Send(message)
        lblInfo.Visible = True
        lblInfo.Text = "Your message has been sent"

    Catch ex As Exception
        lblInfo.Visible = True
        lblInfo.Text = ex.Message

    End Try
End Sub

and here is my web.config

<system.net>
<mailSettings>
  <smtp deliveryMethod ="Network" from="MyEmailAddress.com">
   <network host="localhost" port="25" defaultCredentials ="true" />
  </smtp>
</mailSettings>

I already tried to change the enableSSL = False, but it still give me the same error.

Thanks in advances.

Pierre-Luc Pineault
  • 8,993
  • 6
  • 40
  • 55
Tri.PS
  • 189
  • 4
  • 21
  • AFAIK the SmtpClient should be initialized with a server name or IP, not an email address. Also, make sure the from attribute in you web.config is a valid email address format. – Jens Ehrich Jun 02 '14 at 02:59
  • can you check your stack trace and provide more stack information. YOu can get it via ex.StackTrace. There are multiple reasons why this occurs see following link.http://stackoverflow.com/questions/2209617/smtpclient-failure-sending-mail – Jalpesh Vadgama Jun 02 '14 at 05:28
  • @JensEhrich Assume that smtpclient is server name. that was my mistake when typing it. – Tri.PS Jun 02 '14 at 06:12
  • @JalpeshVadgama I tried your suggestion and get the following : at System.Net.Mail.SmtpClient.Send(MailMessage message) at ForEmail.Email.btnSend_Click(Object sender, EventArgs e) in H:\Project 3\ForEmail\ForEmail\Email.aspx.vb:line 36 – Tri.PS Jun 02 '14 at 06:16
  • @JalpeshVadgama Do you know what does it means? I assume that the error is in client.send(message)... am I right? – Tri.PS Jun 02 '14 at 06:17
  • If you are using local host then make sure you have SMTP server installed on your machine because there might be a problem. – Jalpesh Vadgama Jun 02 '14 at 06:44
  • @JalpeshVadgama I want it to be able to run in both localhost and web.... I test it with localhost and then I will publish it later. should I install it as well? – Tri.PS Jun 02 '14 at 07:36
  • Yes you need to install on both web server as well as web. If you are using third party smtp service then you don't need to install. But if you are using localhost as local smtp server you need to install – Jalpesh Vadgama Jun 02 '14 at 08:33

1 Answers1

0

To find the exact cause of the SMTP error, you should test the SMTP server manually using telnet. First, make sure that the telnet client is installed (hint: "Turn Windows features on and off") then start a command prompt and type the following:

> telnet my.servername.com 25 [ENTER]

You should get a welcome message from the server. Then type the following commands, one at a time and pressing [ENTER] after each line. Substitute your from and to addresses accordingly. Do not type the '>' character, it represents the prompt:

> helo
> mail from: user@sending-domain.com
> rcpt to: user@recipient-domain.com
> data
> Subject: test
> Some test text that goes in the body
> . 

You will either get a message that mail was queued for delivery, in which case the error lies elsewhere in your program, or you will get an error message that you can troubleshoot. Likely causes are relaying denied, server unavailable, mailbox full, user doesn't exist, etc.

PS, you don't need to use the built in telnet client, you can use any other telnet client (ex: PuTTY) if you prefer.

Jens Ehrich
  • 573
  • 1
  • 6
  • 19