Send email from ASP.NET application in IIS7 on Windows server 2008 R2

  • Thread starter Thread starter Sean_Microsec
  • Start date Start date
S

Sean_Microsec

Guest
Firstly, I apologise if this is the wrong forum. I looked through the list and couldnt find a better specific forum.

My issue is trying to send email from my ASP.NET application that resides in IIS7 on a Windows Server 2008 R2 server via a remote smtpServer.

The function in my app...

Private Function SendEmail(ByVal sRecipient As String, ByVal sFrom As String, sServer As String, _
sUsername As String, sPassword As String, ByVal sSubject As String, _
ByVal sBody As String, ByVal aAttachmentFilenames As ArrayList) As Boolean

Dim MailClient As SmtpClient

Dim m As MailMessage
Dim bEmailedOk As Boolean = True

Dim FromAddress As MailAddress
Dim ToAddress As MailAddress
Try

FromAddress = New MailAddress(sFrom)
ToAddress = New MailAddress(sRecipient)

m = New MailMessage(FromAddress, ToAddress)

MailClient = New SmtpClient(sServer, 25)
MailClient.Credentials = New System.Net.NetworkCredential(sUsername, sPassword)

m.Subject = sSubject
m.IsBodyHtml = False
m.Body = sBody

For Each sAttachmentName As String In aAttachmentFilenames
m.Attachments.Add(New Attachment(sAttachmentName))
Next

MailClient.Send(m)

Catch ex As Exception
bEmailedOk = False
End Try

Return bEmailedOk
End Function

I have also set SMTP Email details in IIS7 to those of the remote server.

I have also opened the local port 25 with an outgoing rule.

When I run the app, I get the following error (inner exception):
InnerException = {"An attempt was made to access a socket in a way forbidden by its access permissions ##.###.##.###:25"}

What we have tried:

I have tried running the app with the following in the web.config:

<system.net>
<mailSettings>
<smtp from="fromEmailAddress
<network host="smtp.smptServer" port="25" userName="userName" password="userPwd"/>
</smtp>
</mailSettings>
</system.net>

We have run a stand-alone desktop test application as an Admin user and as a standard user and the email gets sent.

We have also tested using a Windows 7 machine (thats how weve seen the exception) as it is presumed that the same errors/successes will occur.


Recap:

So, what we want to do is have our ASP.Net application send email via a remote SMPTServer. We know that we can successfully connect to and send emails via the remote SMTPServer from the ASP.Net app server using a desktop test app, just not from our ASP.Net app.

Continue reading...
 
Back
Top