Solved

send email in asp.net

Posted on 2009-04-07
18
805 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 50

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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

 
LVL 5

Author Comment

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

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 50

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 50

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 50

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 50

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 50

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 50

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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

809 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