How to send email from ASP application on Windows 2008 Server - asp-classic

My ASP, classic ASP application is giving me a sever error on a Windows 2008 Server. It works fine on Windows 2003 server. The error is a 500 internal server error. Does CDO not work on Windows 2008?
EDIT
THe error is: The transport failed to connect to the server.
Here is my mail function:
function SendMail(mailFrom, mailTo, mailSubject, mailBody, bHtml)
Const cdoSendUsingMethod = _
"http://schemas.microsoft.com/cdo/configuration/sendusing"
Const cdoSendUsingPort = 2
Const cdoSMTPServer = _
"http://schemas.microsoft.com/cdo/configuration/smtpserver"
Const cdoSMTPServerPort = _
"http://schemas.microsoft.com/cdo/configuration/smtpserverport"
Const cdoSMTPConnectionTimeout = _
"http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout"
Const cdoSMTPAuthenticate = _
"http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"
Const cdoBasic = 1
Const cdoSendUserName = _
"http://schemas.microsoft.com/cdo/configuration/sendusername"
Const cdoSendPassword = _
"http://schemas.microsoft.com/cdo/configuration/sendpassword"
Const smtpServer = "localhost"
Dim objConfig ' As CDO.Configuration
Dim objMessage ' As CDO.Message
Dim Fields ' As ADODB.Fields
' Get a handle on the config object and it's fields
Set objConfig = Server.CreateObject("CDO.Configuration")
Set Fields = objConfig.Fields
' Set config fields we care about
With Fields
.Item(cdoSendUsingMethod) = cdoSendUsingPort
.Item(cdoSMTPServer) = smtpServer
.Item(cdoSMTPServerPort) = 25
.Item(cdoSMTPConnectionTimeout) = 10
.Item(cdoSMTPAuthenticate) = cdoBasic
.Item(cdoSendUserName) = "username"
.Item(cdoSendPassword) = "password"
.Update
End With
Set objMessage = Server.CreateObject("CDO.Message")
Set objMessage.Configuration = objConfig
With objMessage
.To = mailTo
.From = mailFrom
.Subject = mailSubject
if bHtml then
.HtmlBody = mailBody
else
.TextBody = mailBody
end if
.Send
End With
Set Fields = Nothing
Set objMessage = Nothing
Set objConfig = Nothing
end function

It appears that the CDO/MAPI libraries aren't installed by default in Windows 2008:
You can download them from Microsoft.
Referenced from this blogpost:
If you want to write client
applications to run on computers that
use MAPI or CDO (for example, web
servers) and you don't want to install
(or can't install) either the Outlook
client or the Exchange management
tools, then you need to install the
MAPI/CDO libraries.

Related

Authenticating using OATH2 on Azure app registration works fine on IIS express, but fails in IIS

I have an application that I am trying to add a layer of SSO, authenticating against Azure AD app registration. The code works fine running in IIS Express, but fails with a 400 Bad Request whenever I attempt to run it from any IIS environment, including localhost.
I have a working function that requests an authorisation code, which works with no issues and the code is returned in the querystring. The issue happens in the next stage, where I use that code to retrieve the user's Sub ID from Microsoft. This is the code I have:
'Get the base azure values
Dim AzureClient As String = ClientInfo
Dim AzureSecret As String = AzureSecret
Dim AuthRedirectUri As String = "The address of the page"
Dim TenantID As String = AzureTenantID
Dim codeVerifier = Verifier string passed in earlier function
Dim httpWReq As HttpWebRequest = DirectCast(WebRequest.Create("https://login.microsoftonline.com/" & TenantID & "/oauth2/v2.0/token"), HttpWebRequest)
httpWReq.Method = "POST"
httpWReq.Host = "login.microsoftonline.com"
httpWReq.ContentType = "application/x-www-form-urlencoded"
Dim postData As String = "client_id=" & AzureClient
postData += "&scope=openid&20email&20profile"
postData += "&code=" & Code
postData += "&redirect_uri=" & AuthRedirectUri
postData += "&grant_type=authorization_code"
postData += "&code_verifier=" & codeVerifier
postData += "&client_secret=" & AzureSecret
Dim encoding As New ASCIIEncoding()
Dim byteArray As Byte() = encoding.GetBytes(postData)
' Set the ContentLength property of the WebRequest.
httpWReq.ContentLength = byteArray.Length
Using streamWriter = New StreamWriter(httpWReq.GetRequestStream())
streamWriter.Write(postData)
End Using
' Get the response.
Dim response As WebResponse = httpWReq.GetResponse() <--- This is where the 400 Bad Request is thrown in IIS, but not IIS Express
Dim responseString As String = New StreamReader(response.GetResponseStream()).ReadToEnd()
Dim o As OAuth2AccessTokenReponse = DirectCast(JsonConvert.DeserializeObject(responseString, GetType(OAuth2AccessTokenReponse)), OAuth2AccessTokenReponse)
Dim IDToken As String = o.id_token
Dim stream = IDToken
Dim handler = New JwtSecurityTokenHandler()
Dim jsonToken = handler.ReadToken(stream)
Dim tokenS = TryCast(jsonToken, JwtSecurityToken)
Dim subID = tokenS.Claims.First(Function(claim) claim.Type = "sub").Value
Return subID
I've compared the calls coming from both environments and they are identical. I have both localhost addresses (localhost IIS address and IIS Express port) so the only differences are the port numbers used in the redirect URI field.
Does anyone have any idea what could be throwing this?
Problem sorted. After much, much digging about I found that IIS was still supporting TLS1.0 - removed that and everything works fine now.

AWS Simple Email Sdk in asp classic

I already tried AWS .net sdk and create com component, but that's not working because it depends on aws.core.dll.
Do Amazon or third party has Simple Email SDK for asp classic VB Script?
Here is an example taken from here:
' http://msdn.microsoft.com/en-us/library/ms526318(v=exchg.10).aspx
' Its good practice to use the module constants defined in the
' type library for the names. The full names are used here to
' indicate that this is what is going on
Const cdoSendUsingMethod = "http://schemas.microsoft.com/cdo/configuration/sendusing"
Const cdoSendUsingPort = 2
Const cdoSMTPServer = "http://schemas.microsoft.com/cdo/configuration/smtpserver"
Const cdoSMTPServerPort = "http://schemas.microsoft.com/cdo/configuration/smtpserverport"
Const cdoSMTPConnectionTimeout = "http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout"
Const cdoSMTPAuthenticate = "http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"
Const cdoBasic = 1
Const cdoSendUserName = "http://schemas.microsoft.com/cdo/configuration/sendusername"
Const cdoSendPassword = "http://schemas.microsoft.com/cdo/configuration/sendpassword"
'Use SSL for the connection (False or True)
Const cdoSendTLS = "http://schemas.microsoft.com/cdo/configuration/smtpusessl"
' create CDOSYS objects
Set myMail.Configuration = CreateObject("CDO.Configuration")
Set Fields = myMail.Configuration.Fields
' CDOSYS SMTP configuration
With Fields
.Item(cdoSendUsingMethod) = cdoSendUsingPort
.Item(cdoSMTPServer) = "smtp.example.com"
.Item(cdoSMTPServerPort) = 25 ' SMTP port, 587 is possible too
.Item(cdoSMTPAuthenticate) = cdoBasic ' send using SMTP authentication
.Item(cdoSendUserName) = "emailadress#example.com" ' email address username
.Item(cdoSendPassword) = "password"
.Item(cdoSendTLS) = true 'send using TLS
.Update 'update the configuration
End With

Unable to send email with IIS7

I'm migrating a website from IIS6 to IIS7. I have everything working but the site will not send emails.
I have confirmed that the server is handling emails correctly, because when I drop a text document into the pickup folder it is delivered, but when I try to send a message via code I either get an error or just nothing.
The SMTP virtual server is setup with standard settings.
The website in question is using 4.0 Framework, Integrated Application pool, I've tried both the ApplicationPoolIdentity, and the NetworkService with no effect. For the site's SMTP module I've tried an SMTP server of 127.0.0.1, localhost and the server's domain name, all on port 25.
For testing I've been using VBS and classic asp to send an email as well as asp.net.
I've been trying very basic scripts copied off the internet where all the comments say that it works. For example
vb.net. This code shows no error, but no message is sent and I've found nothing in the event viewer
Public Shared errorEmailTo As String = System.Configuration.ConfigurationManager.AppSettings("errorEmailTo")
Public Shared errorEmailFrom As String = System.Configuration.ConfigurationManager.AppSettings("errorEmailFrom")
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim email As String = "This is a test email."
show.Text = "To: " + System.Configuration.ConfigurationManager.AppSettings("errorEmailTo") + "<br/>"
show.Text += "From: " + System.Configuration.ConfigurationManager.AppSettings("errorEmailFrom") + "<br/>"
Try
Helper.SendError(email)
show.Text += "No Error"
Catch ex As Exception
show.Text += "Error: " + ex.Message
End Try
End Sub
Public Shared Sub SendError(ByVal strBody As String)
Dim Email As New System.Net.Mail.MailMessage(errorEmailFrom, errorEmailTo)
Email.Subject = "Error Message"
Email.Body = strBody
Dim mailClient As New System.Net.Mail.SmtpClient()
mailClient.UseDefaultCredentials = True
mailClient.EnableSsl = False
Try
mailClient.Send(Email)
Catch ex As Exception
End Try
End Sub
VBS. This code returns the error "The transport failed to connect to the server." For which I've not found anything other than a new nearly identical code block
strSMTPFrom = "donotreply#here.com"
strSMTPTo = "me#here.com"
strSMTPRelay = "localhost"
strTextBody = "Body of your email"
strSubject = "Subject line"
Set oMessage = CreateObject("CDO.Message")
oMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
oMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = strSMTPRelay
oMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
oMessage.Configuration.Fields.Update
oMessage.Subject = strSubject
oMessage.From = strSMTPFrom
oMessage.To = strSMTPTo
oMessage.TextBody = strTextBody
oMessage.Send
Classic ASP gives the same error message.
<%
Dim sch, cdoConfig, cdoMessage
sch = "http://schemas.microsoft.com/cdo/configuration/"
Set cdoConfig = CreateObject("CDO.Configuration")
With cdoConfig.Fields
.Item(sch & "sendusing") = 2 ' cdoSendUsingPort
.Item(sch & "smtpserver") = "localhost"
.Item(sch & "smtpserverport") = 25
.update
End With
Set cdoMessage = CreateObject("CDO.Message")
With cdoMessage
Set .Configuration = cdoConfig
.From = "donotreply#here.com"
.To = "me#here.com"
.Subject = "Email test"
.TextBody = "This is the test body of the email"
.Send
End With
Set cdoMessage = Nothing
Set cdoConfig = Nothing
%>
I've found example after example that give these code blocks as working examples, what am I doing wrong?
Brought in a fresh pair of eyes, and we found the answer.
The virtual SMTP server was listening on port 465(SSL), not 25. So with that corrected, classic ASP and VBS worked, but not VB.Net. We created a new Virtual SMTP server, listening on port 587. And as long as VB.Net had SSL disabled it would work. It appears that the new server didn't receive the SSL certificate during migration.

Sending email through CDOSYS but written in C#

I am sick and tired of my hosting company. I think you can help me here.
I have simple need that I have my complete website written in Classic ASP. Now I want to have a page which sends email using CDOSYS. I wanted to have that script in ASP from my hosting company that what setting should i have. They always send me code in C#.
here is they send always:
CDOSYS is part of the System.Web.Mail namespace and is installed by default on Windows 2000 and Windows XP platforms. It replaces CDONTS for sending SMTP email messages and can be used with our IIS 6 and IIS 7 Windows hosting accounts. The following code sample demonstrates how to create, format, and send email.
private void SendEmail()
{
const string SERVER = "relay-hosting.secureserver.net";
MailMessage oMail = new System.Web.Mail.MailMessage();
oMail.From = "emailaddress#domainname";
oMail.To = "emailaddress#domainname";
oMail.Subject = "Test email subject";
oMail.BodyFormat = MailFormat.Html; // enumeration
oMail.Priority = MailPriority.High; // enumeration
oMail.Body = "Sent at: " + DateTime.Now;
SmtpMail.SmtpServer = SERVER;
SmtpMail.Send(oMail);
oMail = null; // free up resources
}
Here is my ASP which I written to use same thing but doesn't work:
Set objCDOSYSMail = Server.CreateObject("CDO.Message")
Set objCDOSYSCon = Server.CreateObject ("CDO.Configuration")
objCDOSYSCon.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "relay-hosting.secureserver.net"
objCDOSYSCon.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objCDOSYSCon.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objCDOSYSCon.Fields("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
objCDOSYSCon.Fields.Update
With objCDOSYSMail
.To = strEmail
.BCc = "a#gmail.com"
.Cc = "b#gmail.com"
.From = "sales#b.com"
.Subject = "Thank you!"
.HTMLBody = "Hello<br></br><h3>Thank you for your enquiry. <br/>"
Set objCDOSYSMail = Nothing
Set objCDOSYSCon = Nothing
I don't know how to use their code.... please help
You can use the following code in a classic ASP page to send email with CDOSYS:
Set objMail = Server.CreateObject("CDO.Message")
Set objConf = Server.CreateObject("CDO.Configuration")
Set objFlds = objConf.Fields
objFlds.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 'cdoSendUsingPort
objFlds.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.your-site-url.com" 'your smtp server domain or IP address goes here
objFlds.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 'default port for email
'uncomment next three lines if you need to use SMTP Authorization
'objFlds.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "your-username"
'objFlds.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "your-password"
'objFlds.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1 'cdoBasic
objFlds.Update
objMail.Configuration = objConf
objMail.FromName = "Your Name"
objMail.From = "your#address.com"
objMail.To = "destination#address.com"
objMail.Subject = "Email Subject Text"
objMail.TextBody = "The message of the email..."
objMail.Send
Set objFlds = Nothing
Set objConf = Nothing
Set objMail = Nothing

How to add CDO reference in an asp web page

I'm trying to create a new email in asp and send it to a mail server using CDO. I believe I need a reference for CDO or Send Email functionality. In the book it says use this:
Set objNewMail = Server.CreateObject("CDONTS.NewMail")
Unfortunately that is now working as it errors out in asp. Now sure how to add the reference, or com object so that it will work through iis using asp. The book I'm referring to is: ASP In a nut shell 2nd addition. "The CDO Object Model" I'm using windows xp or windows server 2003.
use this instead of cdonts
<!--
METADATA
TYPE="typelib"
UUID="CD000000-8B95-11D1-82DB-00C04FB1625D"
NAME="CDO for Windows 2000 Library"
-->
<%
Function SendMail(sFrom, ToA, Subject, Mybody)
Dim iMsg,iConf
Set iMsg = CreateObject("CDO.Message")
Set iConf = CreateObject("CDO.Configuration")
Dim Flds
Set Flds = iConf.Fields
With Flds
' assume constants are defined within script file
.Item(cdoSendUsingMethod) = cdoSendUsingPort
.Item(cdoSMTPServer) = MAILSERVER
.Item(cdoSMTPConnectionTimeout) = 60
.Item(cdoURLGetLatestVersion) = True
.Update
End With
With iMsg
Set .Configuration = iConf
.To = ToA
.From = sFrom
.Subject = Subject
.TextBody = Mybody
.Send
End With
Set iConf = nothing
Set iMsg = nothing
If Err.Number = 0 Then
SendMail = True
Else
SendMail = Err.Number&":"&Err.Description
End If
On Error Goto 0
set objSendMail = Nothing
End Function
%>

Resources