Click to See Complete Forum and Search --> : Can't send email with VB.NET code :(


cgipro2007
11-25-2007, 12:14 PM
I have a windows 2003 server that has a mail server running on it. I already tested this mail server if it's working or not. The way I tested it was through creating an email address on that server and then I added that email account to be one of my available email accounts in Microsoft Outlook.

Of couse you know that MS Outlook asks for the incoming POP3 and outgoing SMTP servers. So, I provided them to it when I was adding that email. I then tested the email for retrieval and sending and it worked just fine. So, I concluded the fact that my POP3 and SMTP servers were the correct ones.

So I created the following VB.NET function:

Function SendMail(ByVal SMTPServer As String, ByVal SenderEmail As String, ByVal SendTo As String, ByVal MsgSubject As String, ByVal MsgBody As String) As String

Dim MyMail As System.Web.Mail.SmtpMail
Dim MailMsg As New System.Web.Mail.MailMessage

MyMail.SmtpServer = SMTPServer

MailMsg.From = SenderEmail
MailMsg.To = SendTo
MailMsg.BodyFormat = MailFormat.Html
MailMsg.Subject = MsgSubject
MailMsg.Body = MsgBody

Try
MyMail.Send(MailMsg)
Return ""
Catch ex As Exception
Return ex.Message
End Try

End Function

As you can see, one of those function arguments is the SMTP server address.

Whenever I want to send email, I use this function and I pass to it the same SMTP server address I already tested in Outlook previously. However, the function throws the exception [The transport failed to connect to the server.].

Why is that?

Note: My Outgoing server requires authentication. So if this is the problem, then my function up there needs to set this authentication, but I don't know how!! And if this function doesn't work with outgoing servers needing authentication then is there another way of sending email with VB.NET?

chazzy
11-28-2007, 07:12 AM
Is it an exchange server? Maybe this would help you out... http://blogs.msdn.com/darrenj/archive/2003/10/22/51984.aspx

cgipro2007
11-28-2007, 12:45 PM
I don't know whther it's an exchange server or not, but I kinda feel it's not, all I'm sure of is my mail server address is the one I provided to the code which is the same one I tested in MS Outlook and worked fine there.

Regarding the link you provided, Thanks a lot, however if you read the comments bellow that post of that URL, you'll notice that people still complained about not having their problem solved yet.

However, I tried the solution out myself, and this time I got the following error message:

The message could not be sent to the SMTP server. The transport error code was 0x80040217. The server response was not available

chazzy
11-28-2007, 06:52 PM
it's likely a port issue. I'm not sure, but there's probably some way of specifying a port to the Mail object.

lmf232s
11-29-2007, 04:46 PM
you can specify a port and you can also add this to your web.config


<system.net>
<mailSettings>
<smtp deliveryMethod="network" from="Something@Somthing.com">
<network host="mail.somewhere.com" port="25" defaultCredentials="true"/>
</smtp>
</mailSettings>
</system.net>