Solved

send email in asp.net

Posted on 2009-04-07
18
806 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 8
  • 2
18 Comments
 
LVL 51

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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 5

Author Comment

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

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 51

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 51

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
 
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 51

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 51

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 51

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 51

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.

696 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