Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi expert,

In my asp application I have some code which is used to send mail

VB
<%
Email=Request.Form("Email")

htmlMsg=htmlMsg &< font size=""2"" face=""Verdana, Arial, Helvetica, sans-serif"">"&Email&"</font>"


Dim iMsg
Set iMsg = CreateObject("CDO.Message") 
Dim iConf
Set iConf = CreateObject("CDO.Configuration") 
Dim Flds
Set Flds = iConf.Fields
Flds( "http://schemas.microsoft.com/cdo/configuration/sendusing") = 1 
Flds("http://schemas.microsoft.com/cdo/configuration/smtpserverpickupdirectory") = "c:\inetpub\mailroot\pickup"
Flds.Update 
Set iMsg.Configuration = iConf 
iMsg.To ="max@axestra.com" 
iMsg.Cc ="shibus@expo.com, mahiss@expo.com"
iMsg.From = Email
iMsg.Subject = "Food Dubai Advertising"
iMsg.HTMLBody =htmlMsg
iMsg.Send
set iMsg=nothing


How to Convert it into C sharp asp.net

Please help me .
Thanks...............
Posted
Updated 15-Sep-11 20:49pm
v2
Comments
Uday P.Singh 16-Sep-11 2:51am    
What is your question? You want to send mail or you want to convert VB to C#?

VB to C#
and so many online tools avilable to conversion.
 
Share this answer
 
Comments
Dalek Dave 16-Sep-11 3:19am    
There are conversion tools, but they are still a little messy, so you do need to go through an check carefully.
public int SendUserMail(string fromad, string toad, string body, string header, string subjectcontent)
   {
       int result = 0;
       MailMessage usermail = Mailbodplain(fromad, toad, body, header, subjectcontent);
       SmtpClient client = new SmtpClient();
       //Add the Creddentials- use your own email id and password
       client.Credentials = new System.Net.NetworkCredential("your user id ", "pwd"); ;

       client.Host = "smtp.gmail.com";
       client.Port = 587;
       client.EnableSsl = true;
       try
       {
           client.Send(usermail);
           result = 1;
       }
       catch (Exception ex)
       {
           result = 0;
       } // end try

       return result;

   }
   public MailMessage Mailbodplain(string fromad, string toad, string body, string header, string subjectcontent)
   {
       System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
       try
       {
           string from = fromad;
           string to = toad;
           mail.To.Add(to);
           mail.From = new MailAddress(from, header, System.Text.Encoding.UTF8);
           mail.Subject = subjectcontent;
           mail.SubjectEncoding = System.Text.Encoding.UTF8;
           mail.Body = body;
           mail.BodyEncoding = System.Text.Encoding.UTF8;
           mail.IsBodyHtml = true;
           mail.Priority = MailPriority.High;
       }
       catch (Exception ex)
       {
           throw;
       }
       return mail;
   }
 
Share this answer
 
v3
Comments
Uday P.Singh 16-Sep-11 2:50am    
my 5!
udusat13 16-Sep-11 2:53am    
Thanks For reply.

But
why there are 2 methods please help me.

Thanks again
Manfred Rudolf Bihy 16-Sep-11 5:24am    
It's a typical example of good software design. A method should be only responsible for some basic functionality. Here we need two functionalities. First step is creating the email and the second step is sending it. So if the construction process changes this change will not contaminate the mail sending code.

Well done, Salini!
P.Salini 16-Sep-11 3:02am    
You can write it as a single method
I just divide the message part as separate method for my convinience
rkthiyagarajan 17-Sep-11 12:12pm    
Nice Salini
use this refrances
C#
using System.Net;
using System.Net.Mail;



C#
MailMessage ResetPassMail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient();
ResetPassMail.To.Add(new MailAddress("TO EAMIL ID"));
ResetPassMail.From = new MailAddress("FROM ID", "FROM NAME");
ResetPassMail.Sender = new MailAddress("SENDER ID", "SENDER NAME");
ResetPassMail.Subject = "Password Reset";
ResetPassMail.IsBodyHtml = true;
ResetPassMail.Body = "YOUR MAIL BODY"
ResetPassMail.Priority = MailPriority.Normal;
SmtpServer.Host = "smtpout.secureserver.net";
SmtpServer.Port = 25;
SmtpServer.Credentials = new NetworkCredential("EMAIL ID", "EMAIL PASS");
SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;
try
{
    SmtpServer.Send(ResetPassMail);
    sucess = true;
}
catch (SmtpException smtpExe)
{
    throw smtpExe;
}
 
Share this answer
 
http://www.aspheute.com/english/20000918.asp[^]


Sending Email through ASP. NET[^]
public int sendMail(string to,string cc,string bcc,string subject,string body)

{ 

    try 

        {  

           
       SmtpMail.SmtpServer="your_server_address"; 

       MailMessage msg = new MailMessage(); 

            msg.From = "your_email_id";
                
       msg.To = to; 

            msg.Cc = cc; 

            msg.Bcc = bcc; 

            msg.Subject = subject; 

            msg.Body = body; 

       SmtpMail.Send(msg); 

            return(1); 
        } 
        catch 
        { 
               return (0); 
        } 
}


----
  Dim msg as New MailMessage()

     msg.To = "das@silicomm.com"
     msg.From = "das@aspalliance.com"
     msg.Subject = "test"
     'msg.BodyFormat = MailFormat.Html
     msg.BodyFormat = MailFormat.Text
     msg.Body = "hi"

     SmtpMail.SmtpServer = "localhost"

     SmtpMail.Send(msg)
     msg = Nothing
     lblMsg.Text = "An Email has been send to " & "das@silicomm.com"
 
Share this answer
 
v2
Comments
udusat13 16-Sep-11 23:51pm    
thanks expert.

Giving me result according to by result
PrahladMca 17-Sep-11 6:06am    
This code is working on Godaddy Web Server.
NaniCh 7-Dec-12 2:16am    
We are using same code. but mails sends multiple times y?
This works Fine:

MailMessage msg = new MailMessage();
msg.To.Add(new MailAddress(TextBox1.Text));
msg.From = new MailAddress(TextBox2.Text, "name");
msg.Subject = "testing";

msg.Body = TextBox3.Text;
msg.Priority = MailPriority.High;
SmtpClient c = new SmtpClient();
c.Host = "smtp.gmail.com"; /*"smtp.mail.yahoo.com" not working*/
c.Port = 587;
c.EnableSsl = true;
c.Credentials = new NetworkCredential ("mail id","password");
c.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
try
{
c.Send(msg);
}
catch (SmtpException ex)
{
Response.Write(ex.Message);
}
catch (Exception exe)
{
Response.Write(exe.Message);

}

This code work properly when using a gmail smtp address. But yahoo smtp protocol is not working and i think its from yahoo's server.
 
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