Search code examples
asp.netemailazurevbscript

How do I get my old VBScript ASP sendemail to work on Azure?


I recently migrated my ASP.Net website from a traditional windows 2003 shared server to Azure as a Web App. My VBScript forms which send e-mails to me have stopped working since the migration. I have tried a few different approaches to get my VBScript email code to work but have had no luck so far. Part of the problem is that I can't see what the error is.

The first part of my question is: How do I make the ASP.Net errors on my VBScript ASP page visible? I have set debug='true' in my web.config and I tried to set it on my ASP page (see below) but this hasn't worked. Currently I just get an 'Internal error 500' page after attempting to send the email with no indication of what went wrong.

Here is the code that sends the e-mail and appears to be the source of the problem. Can do I change this to work under Azure without rewriting my entire page in C#?

<%@ Language=VBScript Debug='true' %>  'Debug=true doesn't work

Set Mailer = Server.CreateObject("Persits.MailSender")
Mailer.Host = "mail.mydomain.com" ' Specify a valid SMTP server
Mailer.From = Request.Form("AgentEmail") ' Specify sender's address
Mailer.FromName = Request.Form("AgentsName") ' Specify sender's name
Mailer.Port = 587
Mailer.isHTML = True

Mailer.AddAddress "person1@email.com"
Mailer.AddAddress "person2@email.net"
Mailer.AddAddress "person3@email.com"
Mailer.AddAddress Request.Form("AgentEmail")
Mailer.Body = "stuff in my email"


Mailer.Username = "me@emailcom"
Mailer.Password = "123456"
On Error Resume Next
Mailer.Send
If Err <> 0 Then
   Response.Write "Error encountered: " & Err.Description
Else
   Response.Write "Success"
End If

This code did work on my old Windows server. I've left out all of the HTML since that part appears to work just fine.


Solution

  • For future reference, I ended up solving my problem by converting my code to C# and using to smtpClient. This is the general idea here:

    SmtpClient smtpClient = new SmtpClient("mail.domain.com", 587);
    smtpClient.UseDefaultCredentials = false;
    smtpClient.Credentials = new System.Net.NetworkCredential(From, "password");
    smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
    smtpClient.Port = 587;
    MailMessage message = new MailMessage();
    
    try
    {
        MailAddress fromAddress = new MailAddress(From, "Me");
    
        smtpClient.Host = "mail.domain.com";
    
        message.From = fromAddress;
        message.To.Add(To);
        message.Subject = Subject;
        message.IsBodyHtml = true;
        message.Body = Body;
    
        smtpClient.Send(message);
        Label_Results.Text = "Email successfully sent.";
    }
    catch (Exception ex)
    {
        ErrorLabel.Text = "<p>Send Email Failed:<p>" + ex.Message;
    }