Solved

send email in asp.net

Posted on 2009-04-07
18
802 Views
Last Modified: 2012-08-13
i want to send email from my web site with using smtp server
my email is ajay@diamondforgood.com
how i send this using c# coding
0
Comment
Question by:chandresh43
  • 8
  • 8
  • 2
18 Comments
 
LVL 49

Accepted Solution

by:
Ryan Chong earned 500 total points
ID: 24084779
you can try customize this:

using System.Net.Mail;

//..

public static Boolean sendMail(String SenderName, String SenderEmail, String ReceiverName, String ReceiverEmail, String Subject, String Body, Boolean isHTML, String SMTPHost, int SMTPPort, Boolean isAuthenticationNeeded, String SMTPUserName, String SMTPUserPwd)
        {
            try
            {
                SmtpClient sclient = new SmtpClient(SMTPHost, SMTPPort);
                sclient.DeliveryMethod = SmtpDeliveryMethod.Network;
                if (isAuthenticationNeeded)
                {
                    sclient.Credentials = new System.Net.NetworkCredential(SMTPUserName, SMTPUserPwd);                    
                }

                MailMessage mail = new MailMessage();
                mail.Subject = Subject;
                mail.IsBodyHtml = isHTML;
                mail.Body = Body;
                mail.BodyEncoding = System.Text.Encoding.UTF8;
                if (SenderName == "")
                {
                    mail.From = new MailAddress(SenderEmail);
                }
                else
                {
                    mail.From = new MailAddress(SenderEmail, SenderName);
                }
                mail.SubjectEncoding = System.Text.Encoding.UTF8;
                if (ReceiverName == "")
                {
                    mail.To.Add(ReceiverEmail);
                }
                else
                {
                    mail.To.Add(new MailAddress(ReceiverEmail, ReceiverName));
                }
                sclient.Send(mail);
                return true;
            }
            catch (SmtpException)
            {
                return false;
            }
            catch (Exception)
            {
                return false;
            }      
        }



study on System.Net.Mail classes for more info.
http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.aspx
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 24084782
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 24084783
0
 
LVL 5

Author Comment

by:chandresh43
ID: 24085250
which host i use
my web address is www.diamondforgood.com
0
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 24085271
>>which host i use

basically something like smtp.yourdomain.com or mail.yourdomain.com

You need to check with the Admin or ping the domain to find out..
0
 
LVL 5

Author Comment

by:chandresh43
ID: 24085339
it give error
 Failure sending mail.
0
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 24085351
What error message you get here?

Make sure your smtp settings are correct.
0
 
LVL 5

Author Comment

by:chandresh43
ID: 24085354
here is my coding

 protected void Page_Load(object sender, EventArgs e)
    {
        sendMail("ajay", "ajay@diamondforgood.com", "ajay", "ohgod143@yahoo.com", "diamond", "ajay", true, "mail@diamondforgood.com", 8433, true, "ajay", "master");
           

    }


    public static Boolean sendMail(String SenderName, String SenderEmail, String ReceiverName, String ReceiverEmail, String Subject, String Body, Boolean isHTML, String SMTPHost, int SMTPPort, Boolean isAuthenticationNeeded, String SMTPUserName, String SMTPUserPwd)
    {
            SmtpClient sclient = new SmtpClient(SMTPHost, SMTPPort);
            sclient.DeliveryMethod = SmtpDeliveryMethod.Network;
            if (isAuthenticationNeeded)
            {
                sclient.Credentials = new System.Net.NetworkCredential(SMTPUserName, SMTPUserPwd);
            }

            MailMessage mail = new MailMessage();
            mail.Subject = Subject;
            mail.IsBodyHtml = isHTML;
            mail.Body = Body;
            mail.BodyEncoding = System.Text.Encoding.UTF8;
            if (SenderName == "")
            {
                mail.From = new MailAddress(SenderEmail);
            }
            else
            {
                mail.From = new MailAddress(SenderEmail, SenderName);
            }
            mail.SubjectEncoding = System.Text.Encoding.UTF8;
            if (ReceiverName == "")
            {
                mail.To.Add(ReceiverEmail);
            }
            else
            {
                mail.To.Add(new MailAddress(ReceiverEmail, ReceiverName));
            }
            sclient.Send(mail);
            return true;
        }
       

0
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 24085418
"mail@diamondforgood.com" is invalid, try get the correct SMTP Host and other settings from the site admin...
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 5

Author Comment

by:chandresh43
ID: 24085671
where i found correct mail setting
0
 
LVL 5

Author Comment

by:chandresh43
ID: 24085681
you said to ping then
i ping mail@diamondforgood.com -t
and it give me replay
so i use it
0
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 24086550
I get this:

C:\Documents and Settings\Administrator>ping mail@diamondforgood.com
Ping request could not find host mail@diamondforgood.com. Please check the name
and try again.

please recheck your SMTP settings... ?
0
 
LVL 5

Author Comment

by:chandresh43
ID: 24086875
ping mail.diamondforgood.com -t
and this give me replay
type type in run

0
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 24087247
So now you still not able to send out emails?

If you not able to, when you debugging your scripts, what's the error message you captured from the exception?
0
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 24087260
>>what's the error message you captured from the exception?

I mean the detailed message you get..
0
 
LVL 5

Author Comment

by:chandresh43
ID: 24150034
Bad sequence of commands. The server response was: This mail server requires authentication when attempting to send to a non-local e-mail address. Please check your mail client settings or contact your administrator to verify that the domain or address is defined for this server.



thats error i got now
i use host
<system.net>
    <mailSettings>
      <smtp>
        <network host ="mail.diamondforgood.com" userName ="user" password ="pass" defaultCredentials="true"  />        
     
       
      </smtp>
0
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 24155268
can you post the scripts you using?
0
 
LVL 5

Author Comment

by:chandresh43
ID: 24207002
MailMessage mail = new MailMessage();
        mail.From = new MailAddress(Txtfrom.Text);
        mail.To.Add(Txtto.Text);
        mail.Subject = txtsub.Text;
        mail.Body = txtbody.Text;
        SmtpClient smp = new SmtpClient();


        smp.Send(mail);

0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now