[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

send email in asp.net

Posted on 2009-04-07
18
Medium Priority
?
814 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 54

Accepted Solution

by:
Ryan Chong earned 2000 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Author Comment

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

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 54

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 54

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 54

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 54

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 54

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 54

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

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

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

This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month18 days, 9 hours left to enroll

825 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