当前位置: 动力学知识库 > 问答 > 编程问答 >

asp.net - 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 "[email protected]"

Mailer.AddAddress "[email protected]"

Mailer.AddAddress "[email protected]"

Mailer.AddAddress Request.Form("AgentEmail")

Mailer.Body = "stuff in my email"

Mailer.Username = "[email protected]"

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.

网友答案:

Assuming you're using Azure Websites (and not an Azure VM), you can use Classic ASP provided you jump through some hoops: https://khailiangtech.wordpress.com/2011/06/03/windows-azure-how-to-enable-classic-asp-support/

Windows Azure seems to support CDO (the built-in COM SMTP service) whereas your code is using Persits.MailSender - it might be possible to install the Persits.MailSender component via the <ServiceDefinition> XML - but I don't recommend this because of the 32/64-bit problem.

I suggest changing your script to use CDO instead, here's a reference: http://theholmesoffice.com/using-sendgrid-with-classic-asp-to-send-emails/ (the page is for using SendGrid's SMTP server, but you can use any SMTP server (just don't use port 25).

网友答案:

You're trying to instantiate an object from a DLL that is not installed: Server.CreateObject("Persits.MailSender")

You can't install any external COM object when using Web Apps. One option is to use a Virtual Machine and install your COM DLL.

网友答案:

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;
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: