I created a program in VB to send text messages to cell phones a while back but never got it to work. I using my friends mail server(I had to block out the username and password when posting the code below, but his server works) to relay the message. Here is the class I made:

Imports System.Net.Mail




Public Class SendEmail

    Public Sub SendEmailMessage(ByVal strFrom As String, ByVal strSubject As String, ByVal strMessage As String, ByVal PicFilePath As String, ByVal UserName As String, ByVal strServer As String, ByVal strPort As String, ByVal Password As String, ByVal strTo As String)

        Dim auth As New System.Net.NetworkCredential

        Dim count As Integer = 1



        auth.UserName = UserName.TrimEnd

        auth.Password = Password.TrimEnd

    Dim mailmsg As New MailMessage(New MailAddress(strFrom.Trim()), New MailAddress(strTo))



        mailmsg.Subject = strSubject

        mailmsg.Body = strMessage


        mailmsg.From = New MailAddress(strFrom.TrimEnd, "Security")



        Dim sendemail As New SendEmail



        'Smtpclient to send the mail message

        Dim SmtpMail As New SmtpClient(strServer, strPort)

        SmtpMail.Credentials = auth

        SmtpMail.Credentials = auth.GetCredential(strServer, Convert.ToInt16(strPort), "login")



        SmtpMail.Send(mailmsg)

        



        'End If
    End Sub

    Public Sub SendFakeEmailMessage(ByVal login As String, ByVal pass As String, ByVal strTo As String, ByVal strServer As String, ByVal port As Integer, ByVal strFrom As String, ByVal strSubject As String, ByVal strMessage As String, ByVal file As String)

        Dim auth As New System.Net.NetworkCredential



        auth.UserName = login

        auth.Password = pass



        Dim MailMsg As New MailMessage(New MailAddress(strFrom.Trim()), New MailAddress(strTo))

        MailMsg.Subject = strSubject

        MailMsg.Body = strMessage

        MailMsg.From = New MailAddress(strFrom, "Security")
       'Smtpclient to send the mail message

        Dim SmtpMail As New SmtpClient(strServer, Convert.ToInt16(port))

Dim sendemail As New SendEmail

SmtpMail.Credentials = auth

        SmtpMail.Credentials = auth.GetCredential(strServer, Convert.ToInt16(port), "login")

        SmtpMail.Send(MailMsg)
End Sub

End Class

The exception is being thrown during the Smtp.Send(MailMsg).

Now here is the rest of it. Sorry about the length:

Private Sub BtnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSend.Click
        Dim sender2 As New SendEmail

        Dim carriers(58) As String


        carriers(0) = "@itelemigcelular.com.br"
        carriers(1) = "@message.alltel.com"
        carriers(2) = "@message.pioneerenidcellular.com"
        carriers(3) = "@messaging.cellone-sf.com"
        carriers(4) = "@messaging.centurytel.net"
        carriers(5) = "@messaging.sprintpcs.com"
        carriers(6) = "@mobile.att.net"
        carriers(7) = "@mobile.cell1se.com"
        carriers(8) = "@mobile.celloneusa.com"
        carriers(9) = "@mobile.dobson.net"
        carriers(10) = "@mobile.mycingular.com"
        carriers(11) = "@mobile.mycingular.net"
        carriers(12) = "@mobile.surewest.com"
        carriers(13) = "@msg.acsalaska.com"
        carriers(14) = "@msg.clearnet.com"
        carriers(15) = "@msg.mactel.com"
        carriers(16) = "@msg.myvzw.com"
        carriers(17) = "@msg.telus.com"
        carriers(18) = "@mycellular.com"
        carriers(19) = "@mycingular.com"
        carriers(20) = "@mycingular.net"
        carriers(21) = "@mycingular.textmsg.com"
        carriers(22) = "@o2.net.br"
        carriers(23) = "@ondefor.com"
        carriers(24) = "@pcs.rogers.com"
        carriers(25) = "@personal-net.com.ar"
        carriers(26) = "@personal.net.py"
        carriers(27) = "@portafree.com"
        carriers(28) = "@qwest.com"
        carriers(29) = "@qwestmp.com"
        carriers(30) = "@sbcemail.com"
        carriers(31) = "@sms.bluecell.com"
        carriers(32) = "@sms.cwjamaica.com"
        carriers(33) = "@sms.edgewireless.com"
        carriers(34) = "@sms.hickorytech.com"
        carriers(35) = "@sms.net.nz"
        carriers(36) = "@sms.pscel.com"
        carriers(37) = "@smsc.vzpacifica.net"
        carriers(38) = "@speedmemo.com"
        carriers(39) = "@suncom1.com"
        carriers(40) = "@sungram.com"
        carriers(41) = "@telesurf.com.py"
        carriers(42) = "@teletexto.rcp.net.pe"
        carriers(43) = "@text.houstoncellular.net"
        carriers(44) = "@text.telus.com"
        carriers(45) = "@timnet.com"
        carriers(46) = "@timnet.com.br"
        carriers(47) = "@tms.suncom.com"
        carriers(48) = "@tmomail.net"
        carriers(49) = "@txt.att.net"
        carriers(50) = "@tsttmobile.co.tt"
        carriers(51) = "@txt.bellmobility.ca"
        carriers(52) = "@typetalk.ruralcellular.com"
        carriers(53) = "@unistar.unifon.com.ar"
        carriers(54) = "@uscc.textmsg.com"
        carriers(55) = "@voicestream.net"
        carriers(56) = "@vtext.com"
        carriers(57) = "@wireless.bellsouth.com"
        For Each ext As String In carriers
            sender2.SendEmailMessage("email", "", MessageBox.Text, "", "name of person", "mailserver.net", port#, "password", TxtPhone.Text + ext)
        Next
    End Sub

I obviously changed it around so i basically didn't give out my friends mail server and password but that part works. Anybody have any ideas?

Was it SendEmailMessage and/or SendFakeEmailMessage that throws the error?

Anyway, change

SmtpMail.Send(mailmsg)

to

Try
  SmtpMail.Send(mailmsg)
Catch ex As Exception
  MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
  Debug.Print(ex.Message)
End Try

and then please post the error message you receive.

See also Explain Code Tags. For VB.NET it's [/ CODE] (without [B]any[/B] spaces)[CODE = VB.NET] [/ CODE] (without any spaces)

The error that prints out is "Failure sending mail" from the System.Net.Mail.SmtpException. The inner exception says "unable to connect to remote server". I know that the mail server works because its being used right now. Is there anything else that could be generating the error?

More like a connection issue than VB.NET issue.

Can you send email through that SMTP server and port with the same credentials using an email client (Outlook etc.)? Have you checked that you really pass the correct SMTP server, port, user name and password information to your sub?

You could also try (from the command prompt): telnet mail.yoursmtpserver.com 25
to see if you can connect to SMTP server. That 25 is the "standard" SMTP port unless told to use some other port number.

One point in your code is auth.UserName = UserName.TrimEnd . You trim spaces from the end only. Why don't you use auth.UserName = UserName.Trim . The same thing with the password too.

Yeah i use the mail server for outlook but i guess this is a server issue. Ill work on it some more and maybe ill get it to work eventually. Thanks anyay

Yeah i use the mail server for outlook but i guess this is a server issue.

Check from the Outlook menu "Tools\Email Accounts..." the settings for your outgoing SMTP server (i.e. port, user information, server name). Your code should work with those same settings. At least I didn't notice anything wrong in the VB.NET code itself.

Actually i took your advice and i found out i was using the wrong damn port! Im such an idiot. Thanks for your help!

This question has already been answered. Start a new discussion instead.