Click here to Skip to main content
15,879,096 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
Hello,

i am using asp.net c#, i want to send email to applicants registered on my website,
i am using this code to send but it is giving error "Failure sending mail."
private void SendActivationEmail()
    {
        try
        {

            using (MailMessage mm = new MailMessage("sender@sender.com", txtemail.Text))
            {
                mm.Subject = "Account Activation";
                string body = "Hello ";
                body += "Mohallah ";
                body += "Your Password is ";
                body += "<br /><br />Thanks";
                mm.Body = body;
                mm.IsBodyHtml = true;
                SmtpClient smtp = new SmtpClient();
                smtp.Host = "sender@sender.com";
                smtp.EnableSsl = true;
                NetworkCredential NetworkCred = new NetworkCredential("sender@sender.com", "<password>");
                smtp.UseDefaultCredentials = true;
                smtp.Credentials = NetworkCred;
                smtp.Port = 587;
                smtp.Send(mm);
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
    }


please help .....
Posted
Comments
[no name] 22-Oct-14 6:36am    
smtp.UseDefaultCredentials = false;
Member 11014751 22-Oct-14 6:39am    
Same Error
Member 10398773 22-Oct-14 6:55am    
Set enablssl to false
Member 11014751 22-Oct-14 7:10am    
same error

Please change the code and check...

C#
MailAddress mailFrom = new MailAddress(GetAppSettingValue(Constants.K_APP_KEY_MAIL_FROM));
               MailAddress mailTo = new MailAddress(mailToAdd);

               using (MailMessage message = new MailMessage(mailFrom, mailTo))
               {
                   message.Priority = MailPriority.Normal;
                   message.Subject = "mailSubj";
                   message.Body = "mailBody";

                   // Additional user list is not empty, include them in 'To' list.
                   if (!additionalToAdd.IsNull())
                   {
                       message.To.Add(new MailAddress(additionalToAdd));
                   }

                   SmtpClient client = new SmtpClient();
                   client.Send(message);
               }


In web config add the following

XML
<system.net>
      <mailsettings>
          <network enablessl="true" host="smtp.gmail.com" port="587" username="test@abc.com" password="password" />
          <smtp from="password" deliverymethod="SpecifiedPickupDirectory">
            <specifiedpickupdirectory pickupdirectorylocation="D:\00TestEmails\" />
          </smtp>
      </mailsettings>  
    </system.net>


Use any of the above line either network or smtp.
The first one is used to send mail..while the 2nd is used to stored the mail in the specified directory place.

Hope it will help you.
 
Share this answer
 
v3
Comments
Member 11014751 22-Oct-14 7:10am    
Not working
IpsitaMishra 22-Oct-14 7:20am    
please see the updated answer.
Quote:

smtp.Host = "sender@sender.com";

There's your problem - you're putting an email address into a property which expects either the IP address or name of the computer which will send your email.

Gets or sets the name or IP address of the host used for SMTP transactions.

Set the host to a valid SMTP server.
 
Share this answer
 
you can try this ...

/// <summary>
        /// Send mail to admins.
        /// </summary>
        /// <param name="mailText">Message to be send</param>
        /// <returns>True if mail sent otherwise false if mail sending failed.</returns>
        private Boolean SendMail(String mailText)
        {
            Boolean sendMailResult;
            try
            {
                SmtpClient smtpServer = new SmtpClient();
                smtpServer.Credentials = new System.Net.NetworkCredential(mailFromId, mailFromPassword); //Add Email-Id and Password here.
                smtpServer.Port = 587;
                smtpServer.Host = "mail.hostname.com";
                smtpServer.EnableSsl = true; //Do not use this as to bypass exception from AntiVirus/Firewall.
                // smtpServer.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
                MailMessage alertMail = new MailMessage();
                alertMail.From = new MailAddress(mailFromId); //Add Mail From.
                alertMail.Subject = "Application Daily Status";
		String mailId=""; //recepients emailid
 		alertMail.To.Add(mailId);
                alertMail.Body = mailText;
                alertMail.IsBodyHtml = true;
                System.Net.ServicePointManager.ServerCertificateValidationCallback +=
               delegate(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate,
                       System.Security.Cryptography.X509Certificates.X509Chain chain,
                       System.Net.Security.SslPolicyErrors sslPolicyErrors)
               {
                   return true; // **** Always accept
               };
                smtpServer.Send(alertMail);
                sendMailResult = true;
                alertMail.Dispose();
                smtpServer.Dispose();
                return sendMailResult;
            }
            catch (Exception ex)
            {
                string mailSendingFailMessage = "Mail sending failed.";
                mailSendingFailMessage += ex.GetType().ToString() + " " + ex.Message;
                Logger.AppendMessageToStringBuilder(mailSendingFailMessage, 2);
                sendMailResult = false;
                return sendMailResult;
            }
            finally
            {
               
            }
        }
 
Share this answer
 
Comments
CHill60 22-Oct-14 7:47am    
Which of your two solutions should be used? At the moment I can work out which one was posted first, but that won't be the case tomorrow. Use the Improve solution link if you want to update the information in your solution
member 8888995 31-Oct-14 7:17am    
Thanks for suggession
Member 11014751 23-Oct-14 0:35am    
giving error "not all code paths return a value"
member 8888995 31-Oct-14 7:17am    
Just delete the finally block.
XML
HI,
  Use  using System.Net.Mail;

        /// <summary>
        /// Method to send mail.
       /// </summary>
        /// <param name="mailTo">Recipient's Email-Id</param>
        /// <param name="User">Recipient's User Name </param>
        /// <returns>true if mail sent; otherwise, false.</returns>
        private Boolean SendMail(String mailTo, String User)
        {
            Boolean sendMailResult;
            try
            {
                SmtpClient smtpServer = new SmtpClient();
                smtpServer.Credentials = new System.Net.NetworkCredential("", ""); //Add sender’s Email-Id and Password here.
                smtpServer.Port = 25;
                smtpServer.Host = "smtp.gmail.com";
                MailMessage alertMail = new MailMessage();
                alertMail.From = new MailAddress(""); //Add Sender’s Email-Id.
                alertMail.Subject = "Alert Mail";
                alertMail.To.Add(mailTo);
                alertMail.Body = "Dear " + User + "," + " Please Check";
                smtpServer.Send(alertMail);
                sendMailResult = true;
                return sendMailResult;

            }
            catch (Exception ex)
            {
                string exceptionMail=ex.Message;
                sendMailResult = false;
                return sendMailResult;
            }
            finally
            {

            }

        }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900