1

I'm unable to send an email to yahoo server as my code is throwing exception as 'Failure Sending mail' in C# 2008. Please provide SMTP HostName, PortName for yahoo server and gmail server.

And also kindly provide a good working C# code with which i can send an email directly to any of the mail servers.

Please provide complete working code...so that i will copy into Visual studio environment and execute the same. As i'm getting exception since morning....unable to resolve the issue. Kindly help me in this regard.

2
  • Could you post your code (without real username and password) Commented Nov 20, 2009 at 11:23
  • I used the below code only....but getting the exception Commented Nov 20, 2009 at 12:09

4 Answers 4

14

For Gmail:

var client = new SmtpClient("smtp.gmail.com", 587);
client.EnableSsl = true;
client.Credentials = new NetworkCredential("[email protected]", "secret");

var mail = new MailMessage();
mail.From = new MailAddress("[email protected]");
mail.To.Add("[email protected]");
mail.Subject = "Test mail";
mail.Body = "test body";
client.Send(mail);

For Yahoo:

var client = new SmtpClient("smtp.mail.yahoo.com", 587);
client.Credentials = new NetworkCredential("[email protected]", "secret");

var mail = new MailMessage();
mail.From = new MailAddress("[email protected]");
mail.To.Add("[email protected]");
mail.Subject = "Test mail";
mail.Body = "test body";
client.Send(mail);            
Sign up to request clarification or add additional context in comments.

3 Comments

Kindly help me in this regard. Very utmost help is required. As i copied the same above code and provided my credentials accordingly in the above code and executed in console as well as in windows....but getting exception as 'Failure Sending mail'. Please help me...........
Is it possible that you are behind a firewall that could block port 587?
Before posting the code I tested both with yahoo and gmail and it worked perfectly fine, so probably there's something related to your environment. Could post the entire exception stack trace you are getting?
0

Bear in mind that some ISPs (including mine) force their clients to use their SMTP server (as a relay). Spamming protection is the reason.

So you should avoid sending e-mail to the Internet from a client application, unless you give user a chance to specify his SMTP hostname or your app relies on the user's e-mail software (MAPI,...).

Comments

0

Imagine how much easier it would be for us to help you, if you posted the complete exception message, along with a stack trace.

Also, go one step farther and enable logging for System.Net.Mail, so we can see any possible failures at the network level.

If you don't know how to enable logging for SNM, here is a link:

http://systemnetmail.com/faq/4.10.aspx

Thanks!

Dave

Comments

0

There are two ways in which We can send the mail,

1)First is using javascript link "mailTo".This will not send the mail automatically but it will just open the mail window.Refer to the below code

   <a class="label" onclick='javascript:buildEmail(this)'>Send Mail</a>

Find below the js method

     function buildEmail(el) {
 var emailId = [email protected];
  var subject="Hi";
 var body="Hello";
  el.href = "mailto:" + emailId + "?Subject=" + escape(subject) +
                                            "&Body=" + escape(body);
}

2)The second way is to use System.Net.Mail which will automatically send the mail to the recipients in the secured manner.

    string subject="Hello";
       string body="Data"; 
       using ( MailMessage objMail = new MailMessage ( "[email protected]", "[email protected]" ) )//From and To address respectively
                {
                    objMail.IsBodyHtml = false;// Message format is plain text
                    objMail.Priority = MailPriority.High;// Mail Priority = High
                    objMail.Body = "Hello";
                    ArrayList CCarr = new ArrayList();//Assume we add recipients here

                   // populate additional recipients if specified
                    if ( ( CCarr != null ) && ( CCarr .Count > 0 ) )
                    {
                        foreach ( string recipient in CCarr )
                        {
                            if ( recipient != "Please update the email address" )
                            {
                                objMail.CC.Add ( new MailAddress ( recipient ) );
                            }
                        }
                    }

                     // Set the subject of the message - and make sure it is CIS Compliant
                        if ( !subject.StartsWith ( "SigabaSecure:" ) )
                        {
                            subject = "SigabaSecure: " + subject;
                        }   
                  objMail.Subject = subject;

                   // setup credentials for the smpthost
                    string username =  "Username";
                    string passwd =     "xxxxxx";
                    string smtpHost =  "mail.bankofamerica.com";

                    SmtpClient ss = new SmtpClient ();
                    ss.EnableSsl= true;
                    ss.Host = smtpHost;
                    ss.Credentials = new NetworkCredential ( username, passwd );
                    ss.Send ( objMail );
}

Sigaba Secure Email secures e-mail from client to client through the use of desktop plug-ins and Web-based authentication and decryption.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.