Click here to Skip to main content
15,920,675 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all

I did one email code in classic ASP now i want the same thing in C#.net
Please tell me how to convert the code
Below is the code:
HTML
<%
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
Posted
Updated 22-Aug-12 20:22pm
v2
Comments
Kuthuparakkal 23-Aug-12 1:33am    
plz post the code, will try... also see how to send emails on my answer
[no name] 23-Aug-12 2:01am    
<%
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

C#
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;
        }
      }
 
Share this answer
 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900