Click here to Skip to main content
15,904,638 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi this is my code

For sending an email

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Net.Mail;
//using System.Net.Mail.SmtpClient;
//using System.Net.Mail.MailMessage;


namespace mailapp
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        public void sendMail(string toList, string fromlist, string ccList, string subject, string body)
        {
            try
            {
                MailMessage message = new MailMessage();
                SmtpClient smtpClient = new SmtpClient();
                MailAddress fromAddress = new MailAddress(fromlist);
                message.From = fromAddress;
                message.To.Add(toList);
                if (ccList != null && ccList != string.Empty)
                {
                    message.CC.Add(ccList);
                }
                message.Subject = subject;
                message.IsBodyHtml = true;
                message.Body = body;
                smtpClient.Host = "127.0.0.1";
                smtpClient.Port = 587;
                smtpClient.EnableSsl = true;
                smtpClient.UseDefaultCredentials = true;
                smtpClient.Credentials = new System.Net.NetworkCredential(txtfromadd.Text, txtpwd.Text);
                smtpClient.Send(message);
            }
            catch (Exception ex)
            {
                lblmail.Visible = true;
                lblmail.Text = ex.Message.ToString();
            }

        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            //string fromadd = txtfromadd.Text;
            string tocc = "suguna@cavintek.com";
            sendMail(txtToAddress.Text, txtfromadd.Text, tocc, txtSubject.Text, txtContent.Text);
        }
    }
}


I tried more possibilities to send email but am getting error failure sending email..Inner exception is unable to connect to the remote server

why what i want to do?i made any mistake in this?
Posted
Updated 25-Aug-11 20:45pm
v2
Comments
Prerak Patel 26-Aug-11 2:45am    
Use code block for code segments.

1. Check whether SMTP settings are correct and server is configured correctly (Host & Port Settings)
2. Check whether credentials(user name and password) are correct
3. Check whether firewall is blocking the request
 
Share this answer
 
The error message itself explain you what the error is.
there is server connection problem.

possible checks are

1. Is a valid server "127.0.0.1 (smtp server)" as you specified ?
2. The credentials "id" and "pwd" are valid for sending email ?
3. Check port 587 is blocked in firewall

make one change in code, set UseDefaultCredentials to false
smtpClient.UseDefaultCredentials = false;
 
Share this answer
 
Comments
suryaswathi 26-Aug-11 3:28am    
i added the code smtpClient.UseDefaultCredentials = false;
and checked server and id and pwd..but how to check port 587 is blovked in firewall
shreya1987 26-Aug-11 3:34am    
which OS do u have ?
suryaswathi 26-Aug-11 3:35am    
windows 7
koolprasad2003 26-Aug-11 3:44am    
check this link to open port in firewall

http://windows.microsoft.com/en-IN/windows-vista/Open-a-port-in-Windows-Firewall
koolprasad2003 26-Aug-11 3:44am    
check following setting also https://support.exabytes.com/KB/a1886/switching-from-smtp-port-25-to-port-587.aspx
Check SMTP is been enabled in server OR specify other host details for example Gmail server and check.
 
Share this answer
 
Comments
suryaswathi 26-Aug-11 3:08am    
i checked everything but am getting same error
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Net;
using System.Net.Mail;
using System.IO;
public partial class send_mail : System.Web.UI.Page
{

MailMessage obj = new MailMessage();
protected void Page_Load(object sender, EventArgs e)
{



}

in button send code
{

SmtpClient serverobj = new SmtpClient();
serverobj.Credentials = new NetworkCredential("anil@gmail.com", "anil");
serverobj.Port = 25;
serverobj.Host = "smtp.gmail.com";
serverobj.EnableSsl = true;
obj = new MailMessage();
obj.From = new MailAddress("anil@gmail.com", "Anilkumar", System.Text.Encoding.UTF8);
obj.To.Add(ds.Tables[0].Rows[i]["struseremail"].ToString());
obj.Priority = MailPriority.High;
obj.Subject = txtsub.Text.Trim();
string date = DateTime.Now.ToString();
obj.Body = txtbody.Text;
serverobj.Send(obj);
lblmessage.Text = "Your message has been sent.";


}

}


}

Just Try this.

Regards,

Anilkumar.D
 
Share this answer
 
v2
Comments
suryaswathi 26-Aug-11 4:39am    
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at


smtp exception was unhandled by user code

i got this error..while trying this code ..how to overcome this error?
Arun Jacob 26-Aug-11 5:06am    
Please use code formatting
Member 12641944 22-Nov-16 5:39am    
please explain properly. i am also face this error.
suryaswathi 26-Aug-11 5:54am    
Hi anil kumar sir code working correctly..thanks....able to send mail..

now if i have to address more mean i have to use it array ...
for cc and bcc to address more mean want to use array

how to use in ur coding in this..
suryaswathi 26-Aug-11 6:14am    
sir again one pblm the email send from gmail but not send from yahoo why

i use port 587

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