ASP WebMail: Set up SMTP authentication?

2019-08-27 16:36发布

问题:

I'm developing web pages using Razor C# language in WebMatrix. I have a hosted website, I'm trying to incorporate an email system into it.

As per this article on WebMail I have set up the WebMail settings in my _AppStart.cshtml page. I've got my settings from my service provider. He's provided me a sample code using CDO object:

    dim config, sch     
    set config = CreateObject("CDO.Configuration")
    sch = "http://schemas.microsoft.com/cdo/configuration/"

    with config.Fields
        .item(sch & "sendusing") = 2 ' cdoSendUsingPort
        .item(sch & "smtpserver") = "myserver"
        .item(sch & "smtpserverport") = 25
        .item(sch & "smtpusessl") = False
        .item(sch & "smtpconnectiontimeout") = 60           
        .item(sch & "smtpauthenticate") = 1 'basic auth
        .item(sch & "sendusername") = "myemail@email.com"
        .item(sch & "sendpassword") = "password"
        .update
    end with

    Set myMail=CreateObject("CDO.Message")

    With myMail
        .Configuration = config
        .Subject = Request.Form("txtSubject")
        .From = Request.Form("txtFrom")
        .To = Request.Form("txtTo")
        .TextBody = Request.Form("txtMessage")
        Call .Send()
    End With    

As you can see the above code is made in CDO. I'm trying to use the WebMail in Razor. The only point where I'm stuck is that my email server is not SSL but requires basic auth. I can't find any authentication setting in WebMail. How do I set the SMTP authentication in WebMail? This is my current code:

WebMail.SmtpServer = "myserver";
WebMail.SmtpPort = 25;
WebMail.EnableSsl = false;
WebMail.UserName = "myemail@email.com";
WebMail.Password = "password";
WebMail.From = "Support <myemail@email.com>";

Thanks in advance!

回答1:

Here's a basic example in c#. The Smtp class takes username password.

 MailMessage mail = new MailMessage("emailfrom","emailto");


        mail.From = new MailAddress("emailfrom");
        mail.Subject = txtsbjct.Text;
        string Body = txtmsg.Text;
        mail.Body = Body;

        mail.IsBodyHtml = true;
        SmtpClient smtp = new SmtpClient();
        smtp.Host = "localhost"; 
        smtp.Credentials = new System.Net.NetworkCredential
      ("youremail", "yourpassword");


回答2:

Basic authentication with mail servers usually consists of providing a user name and password. You set those using the WebMail.UserName and WebMail.Password properties.

By the way, your provider has given you sample code for sending mail using CDO in classic ASP. It is of no use to you.