English 中文(简体)
Sending Email through .NET code
原标题:
  • 时间:2009-11-20 11:20:56
  •  标签:
  • c#
  • email
  • smtp

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.

问题回答

For Gmail:

var client = new SmtpClient("smtp.gmail.com", 587);
client.EnableSsl = true;
client.Credentials = new NetworkCredential("youraccount@gmail.com", "secret");

var mail = new MailMessage();
mail.From = new MailAddress("youraccount@gmail.com");
mail.To.Add("youraccount@gmail.com");
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("youraccount@yahoo.com", "secret");

var mail = new MailMessage();
mail.From = new MailAddress("youraccount@yahoo.com");
mail.To.Add("destaccount@gmail.com");
mail.Subject = "Test mail";
mail.Body = "test body";
client.Send(mail);            

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,...).

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

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 = Usermail@gmail.com;
  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 ( "Yourmail@gmail.com", "Usermail@gmail.com" ) )//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.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签