如何将ASp代码转换为C#代码 [英] How to convert ASp code to C# code

查看:96
本文介绍了如何将ASp代码转换为C#代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

我现在在经典的ASP中编写了一个电子邮件代码,现在我希望在C#.net中也做同样的事情
请告诉我如何转换代码
下面是代码:

 <%
mailing = Request.Form(" )
paswordgen = Password_GenPass( 5 "  )
vartime = FormatDateTime(现在)
varadd = DateAdd("  1 ,vartime)
%>
<%
昏暗的strResult
设置邮件= Server.CreateObject(" )
Mail.Configuration.Fields.Item(" )=  2 

Mail.Configuration.Fields.Item(" )= " 
Mail.Configuration.Fields.Item(" )=  25 

Mail.Configuration.Fields.Item(" )=  1 

Mail.Configuration.Fields.Item(" )=  60 
Mail.Configuration.Fields.Item(" )=  1 
Mail.Configuration.Fields.Item(" )= "  ' 您还可以使用通过Google Apps设置的电子邮件地址.
Mail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword")="

Mail.Configuration.Fields.Update

Mail.Subject ="Hello"
Mail.From ="snehasishnandy@gmail.com"'这必须是实际的电子邮件地址或在您上面使用的gmail帐户上设置的别名
Mail.To =邮件
'  Mail.Bcc ="snehasishnandy12@gmail.com"抄送
' Mail.Cc = " 密件抄送

'  **以下是电子邮件正文的不同选项. *只能发送以下一种身体类型.
' Mail.TextBody = " 
Mail.HTMLBody = " &邮寄" & Paswordgen& " 
'  Mail.CreateMHTMLBody"http://www.mediconline.in/index.asp"发送包含正文的电子邮件指定网页的
' Mail.CreateMHTMLBody " 发送一封电子邮件,该邮件具有存储在您的计算机上的html文件的正文.该脚本必须位于提供脚本的服务器上.

' 如何添加附件
' myMail.AddAttachment " '再次必须在提供 this 脚本的服务器上.

邮件发送
设置Mail = Nothing
%>

<%
函数Password_GenPass(nNoChars,sValidChars)
    '  nNoChars =生成密码的长度
    ' sValidChars =有效字符.如果为零长度字符串
     ' (")然后
    ' 默认用于:A-Z AND a-z AND 0-9
 
    const szDefault = " 
    昏暗nCount
    点心
    暗号
    昏暗nLength
 
    随机化' 初始化随机
 
    如果sValidChars ="然后
        sValidChars = szDefault
    万一
    nLength = Len(sValidChars)
    对于nCount = 1到nNoChars
        nNumber = Int((nLength * Rnd)+1)
        sRet = sRet& Mid(sValidChars,nNumber,1)
    下一个
    Password_GenPass = sRet
结束功能
%>  



这是我的代码.请检查一次.感谢您的发帖.

解决方案

 受保护的 无效 btnSendmail_Click(对象发​​件人,EventArgs e)
      {
        //  System.Web.Mail.SmtpMail.SmtpServer在2.0版中已过时
        //  System.Net.Mail.SmtpClient是2.0中的替代类
        SmtpClient smtpClient =  SmtpClient();
        MailMessage消息=  MailMessage();

        尝试
        {
            MailAddress fromAddress =  MailAddress(txtEmail.Text,txtName.Text);

            // 您可以指定服务器的主机名或ip地址
            //  IIS中的默认值是localhost 
            smtpClient.Host = " ;

            // 默认端口为25 
            smtpClient.Port =  25 ;

            // 发件人的地址将作为MailAddress对象给出
            message.From = fromAddress;

            // 解决MailAddress的地址
            message.To.Add(" );
            message.Subject = " ;

            //  CC和BCC可选
            //  MailAddressCollection类用于将电子邮件发送给各个用户
            // 您可以将地址"指定为新的MailAddress("admin1@yoursite.com")
            message.CC.Add(" );
            message.CC.Add(" );

            // 您可以直接将地址指定为字符串
            message.Bcc.Add( MailAddress(" ));
            message.Bcc.Add( MailAddress(" ));

            // 正文可以是HTML或文本格式
            // 如果是html消息,则指定true 
            message.IsBodyHtml =  false ;

            // 邮件正文内容
            message.Body = txtMessage.Text;
         
            // 发送SMTP邮件
            smtpClient.Send(消息);

           //  lblStatus.Text =电子邮件发送成功."; 
        }
        捕获(例外)
        {
            //  lblStatus.Text =发送电子邮件失败." + ex.Message; 
        }
      } 


http://www.harding.edu/fmccown/vbnet_csharp_comparison.html [ ^ ]

检查CP中的以下帖子:
将经典Asp(.asp)项目转换为Asp.net project(.aspx) [<% mailing=Request.Form("txtemail") paswordgen = Password_GenPass( 5, "" ) vartime=FormatDateTime(Now) varadd=DateAdd("n",1,vartime) %> <% dim strResult Set Mail = Server.CreateObject("CDO.Message") Mail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 Mail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") ="smtp.gmail.com" Mail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 Mail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = 1 Mail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60 Mail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1 Mail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusername") ="" 'You can also use you email address thats setup through google apps. Mail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendpassword") ="" Mail.Configuration.Fields.Update Mail.Subject="Hello" Mail.From="snehasishnandy@gmail.com" 'This has to be an actual email address or an alias thats setup on the gmail account you used above Mail.To=mailing 'Mail.Bcc="snehasishnandy12@gmail.com" Carbon Copy 'Mail.Cc="snehasishnandy@gmail.com" Blind Carbon Copy '**Below are different options for the Body of an email. *Only one of the below body types can be sent. 'Mail.TextBody="This is an email message thats text only." Mail.HTMLBody="<html><body>Please Login using below details<br>Your ID is:" & mailing & "<br>Your Password is:" & paswordgen & "</body></html>" 'Mail.CreateMHTMLBody "http://www.mediconline.in/index.asp" Sends an email which has a body of a specified webpage 'Mail.CreateMHTMLBody "file://c:/mydocuments/email.htm" Sends an email which has a body of an html file that’s stored on your computer. This MUST be on the server that this script is being served from. ' How to add an attachment 'myMail.AddAttachment "c:\mydocuments\test.txt" ‘Again this must be on the server that is serving this script. Mail.Send set Mail = Nothing %> <% Function Password_GenPass( nNoChars, sValidChars ) ' nNoChars = length of generated password ' sValidChars = valid characters. If zerolength-string ' ( "" ) then ' default is used: A-Z AND a-z AND 0-9 Const szDefault = "abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVXYZ0123456789" Dim nCount Dim sRet Dim nNumber Dim nLength Randomize 'init random If sValidChars = "" Then sValidChars = szDefault End If nLength = Len( sValidChars ) For nCount = 1 To nNoChars nNumber = Int((nLength * Rnd) + 1) sRet = sRet & Mid( sValidChars, nNumber, 1 ) Next Password_GenPass = sRet End Function %>



This is my code..please check once..and thanks for ur post

解决方案

protected void btnSendmail_Click(object sender, EventArgs e)
      {
        // System.Web.Mail.SmtpMail.SmtpServer is obsolete in 2.0
        // System.Net.Mail.SmtpClient is the alternate class for this in 2.0
        SmtpClient smtpClient = new SmtpClient();
        MailMessage message = new MailMessage();

        try
        {
            MailAddress fromAddress = new MailAddress(txtEmail.Text, txtName.Text);

            // You can specify the host name or ipaddress of your server
            // Default in IIS will be localhost 
            smtpClient.Host = "localhost";

            //Default port will be 25
            smtpClient.Port = 25;

            //From address will be given as a MailAddress Object
            message.From = fromAddress;

            // To address collection of MailAddress
            message.To.Add("admin1@yoursite.com");
            message.Subject = "Feedback";

            // CC and BCC optional
            // MailAddressCollection class is used to send the email to various users
            // You can specify Address as new MailAddress("admin1@yoursite.com")
            message.CC.Add("admin1@yoursite.com");
            message.CC.Add("admin2@yoursite.com");

            // You can specify Address directly as string
            message.Bcc.Add(new MailAddress("admin3@yoursite.com"));
            message.Bcc.Add(new MailAddress("admin4@yoursite.com"));

            //Body can be Html or text format
            //Specify true if it  is html message
            message.IsBodyHtml = false;

            // Message body content
            message.Body = txtMessage.Text;
         
            // Send SMTP mail
            smtpClient.Send(message);

           // lblStatus.Text = "Email successfully sent.";
        }
        catch (Exception ex)
        {
            //lblStatus.Text = "Send Email Failed." + ex.Message;
        }
      }


http://www.harding.edu/fmccown/vbnet_csharp_comparison.html[^]


Check following post in CP:
Convert Classic Asp(.asp) Project to Asp.net project(.aspx)[^]


这篇关于如何将ASp代码转换为C#代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆