Click here to Skip to main content
15,390,740 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I coded for mailing in the form but now presently its not working, so i asked with my colleague about it, he replied : we cant mail before deployment it wont work. so I want to confirm it ,is it right?

I tried with this below code

public void sendMail()
        {
            try
            {
                MailMessage objeto_mail = new MailMessage();
                SmtpClient client = new SmtpClient();
                client.Port = 25;
                client.Host = "smtp.removed.in";
                client.Timeout = 10000;
 client.EnableSsl = true;
                client.DeliveryMethod = SmtpDeliveryMethod.Network;
                client.UseDefaultCredentials = false;
                client.Credentials = new System.Net.NetworkCredential("mdawood@XXREMOVEDXX", "XXREMOVEDXX");
                objeto_mail.From = new MailAddress("xxx@removed.in");
                objeto_mail.To.Add(new MailAddress(txtEmailId.Text));
                objeto_mail.Subject = "Password Recover";
                objeto_mail.Body = "Message";
                client.Send(objeto_mail);
            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString());
            }
}


there throwing an error : Failure sending mail.
Posted
Updated 20-Apr-15 23:37pm
v2

1 solution

This is the code that worked without any issues on our project:

C#
        //////////////////////////////////////////////////////////////////////////
        /// <summary>
        /// Sends mail
        /// </summary>
        /// <param name="recipients"></param>
        /// <param name="subject"></param>
        /// <param name="message"></param>
        /// <param name="attachments"></param>
        internal override void Send(string[] recipients, string subject, string message, string[] attachments)
        {
#pragma warning disable 612,618
            var mail = new MailMessage();
#pragma warning restore 612,618

            mail.Fields[SmtpServer] = _smtpHost;
            mail.Fields[SmtpServerPort] = _smtpPort;
            mail.Fields[SendUsing] = 2;
            mail.Fields[SmtpUseSsl] = true;
            mail.Fields[SmtpAuthenticate] = 1; // 0=anonymous, 1=basic, 2=NTLM
            mail.Fields[SendUsername] = _username;
            mail.Fields[SendPassword] = _password;

            // set unicode encoding - it is also used for encoding email headers (not only the body)
            mail.BodyEncoding = Encoding.UTF8;
            // set email properties
            mail.From = string.Format("{0} <{1}>", _displayName, _username);
            mail.To = string.Join(";", recipients);
            mail.Subject = subject;
            mail.Body = message;
            //mail.Priority = MailPriority.Low;
            //mail.BodyFormat = MailFormat.Html;
            if (attachments != null)
            {
                foreach (var attachment in attachments)
                {
#pragma warning disable 612,618
                    //TODO: replace with System.Net.Mail.Attachment if supported
                    mail.Attachments.Add(new MailAttachment(Path.GetFullPath(attachment), MailEncoding.Base64));
#pragma warning restore 612,618
                }
            }
#pragma warning disable 612,618
            SmtpMail.Send(mail);
#pragma warning restore 612,618
        }
        //////////////////////////////////////////////////////////////////////////
    }
    ///////////////////////////////////////////////////////////////////
///////

You can try to use it
   
v2
Comments
dawood abbas 22-Apr-15 2:43am
   
I used it but at many places displaying error (red mark)

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