Najim Mulla

Najim Mulla

  • 839
  • 971
  • 75.1k

this code working in localhost but ot working online server

May 20 2015 5:17 AM
  try
        {
            bool enableSSL = true;
            string smtp_name = "";
            int port_name;
            HttpRequest request = HttpContext.Current.Request;
            string url = request.Url.ToString();
            SmtpClient smtpClient = new SmtpClient();
            MailMessage message = new MailMessage(txt_frm_email_add.Text, txt_to_email_add.Text);
            // Prepare two email addresses
            MailAddress fromAddress = new MailAddress(txt_frm_email_add.Text); //, txt_sub.Text
            MailAddress toAddress = new MailAddress(txt_to_email_add.Text);//, txt_sub.Text
            // MailAddress cc = new MailAddress("[email protected]", "Error");txt_to_email_add.Text, txt_sub.Text
            if (FileUpload1.HasFile)
            {
                string FileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
                message.Attachments.Add(new Attachment(FileUpload1.PostedFile.InputStream, FileName));
            }
            // Prepare the mail message
            message.From = fromAddress;
            message.To.Add(toAddress);
            //  message.CC.Add(cc);
            message.Subject = string.Concat(txt_sub.Text + " :-  ", DateTime.Now.ToString("dd-MMM-yyyy hh:mm tt"));
            message.Body = txt_mater.Text;

            string[] smtp_name1 = (txt_frm_email_add.Text).Split('@');
            string smtp_name2 = smtp_name1[1];
            if (smtp_name2 == "gmail.com")
            {
                smtp_name = "smtp.gmail.com";
                port_name = 587;
                smtpClient.Host = smtp_name;// "smtp.deltalabsangli.in"; //for softlayer server
                smtpClient.Port = port_name;
                smtpClient.UseDefaultCredentials = false;
                smtpClient.Credentials = new NetworkCredential(txt_frm_email_add.Text, txt_pass.Text);
                smtpClient.EnableSsl = enableSSL;
            }
            else if (smtp_name2 == "yahoo.com")
            {
                smtp_name = "smtp.mail.yahoo.com";
                port_name = 587;//465;
                smtpClient.Host = smtp_name;// "smtp.deltalabsangli.in"; //for softlayer server
                smtpClient.Port = port_name;
               // smtpClient.UseDefaultCredentials = false;
                smtpClient.Credentials = new NetworkCredential(txt_frm_email_add.Text, txt_pass.Text);
                smtpClient.EnableSsl = enableSSL;
            }
            else if (smtp_name2 == "live.com")
            {
                smtp_name = "smtp.live.com";
                port_name = 587;// 465;
                smtpClient.Host = smtp_name;// "smtp.deltalabsangli.in"; //for softlayer server
                smtpClient.Port = port_name;
                smtpClient.UseDefaultCredentials = false;
                smtpClient.Credentials = new NetworkCredential(txt_frm_email_add.Text, txt_pass.Text);
                smtpClient.EnableSsl = enableSSL;
            }
            else if (smtp_name2 == "rediffmail.com")
            {
                smtp_name = "smtp.rediffmail.com";
                port_name = 587;
                smtpClient.Host = smtp_name;// "smtp.deltalabsangli.in"; //for softlayer server
                smtpClient.Port = port_name;
                smtpClient.UseDefaultCredentials = false;
                smtpClient.Credentials = new NetworkCredential(txt_frm_email_add.Text, txt_pass.Text);
                smtpClient.EnableSsl = enableSSL;
            }
            else
            {
                smtp_name = "103.21.58.151";// "smtp.deltalabsangli.in"; //for softlayer server
                port_name = 587;
                smtpClient.Host = smtp_name;// "smtp.deltalabsangli.in"; //for softlayer server
                smtpClient.Port = port_name;
                smtpClient.Credentials = new NetworkCredential(txt_frm_email_add.Text, txt_pass.Text);
            }
            // Set server details
         
            smtpClient.Send(message);
            smtpClient.Timeout = 20000;
            //ClientScript.RegisterStartupScript(GetType(), "alert", "alert('Email sent.');", true);
            Response.Redirect("~/Master/Mail_send.aspx");
        }
        catch (Exception ex)
        {
            db.MessageBox(ex.Message, this);
        }

Answers (11)