Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi, i am trying to send an email through my application, but it gives me an exception. Email sending failure. Inner exception "Unable to connect to the remote server".

this problem comes for my domain as well as when i try to use gmail smtp settings.

C#
string ReplyTo = "From@mydomain.com";
                SmtpClient client = new SmtpClient("localhost"); 
                MailAddress from = new MailAddress("From@mydomain.com", "Hello"); 
                MailAddress to = new MailAddress("To@mydomain.com");
                MailMessage message = new MailMessage(from, to);
                client.Credentials = new System.Net.NetworkCredential("MYID", "PASSWORD");
                
                message.Body = Body;
                message.Subject = Subject;

                StringBuilder str = new StringBuilder();
                str.AppendLine("Hello this is a test email");StringBuilder str = new StringBuilder();
                str.AppendLine("BEGIN:VCALENDAR");
                str.AppendLine("PRODID:-//HR21");
                str.AppendLine("VERSION:2.0");
                str.AppendLine("METHOD:PUBLISH");
                str.AppendLine("BEGIN:VEVENT");
                str.AppendLine(string.Format("DTSTART:{0:yyyyMMddTHHmmssZ}", FromDate.ToUniversalTime()));
                str.AppendLine(string.Format("DTSTAMP:{0:yyyyMMddTHHmmssZ}", DateTime.Now.ToUniversalTime()));
                str.AppendLine(string.Format("DTEND:{0:yyyyMMddTHHmmssZ}", ToDate.ToUniversalTime()));
                str.AppendLine("LOCATION: None");
                str.AppendLine(string.Format("UID:{0}", Guid.NewGuid()));
                str.AppendLine(string.Format("DESCRIPTION:{0}", message.Body));
                str.AppendLine(string.Format("X-ALT-DESC;FMTTYPE=text/html:{0}", message.Body));
                str.AppendLine(string.Format("SUMMARY:{0}", message.Subject));
                str.AppendLine(string.Format("ORGANIZER:MAILTO:{0}", message.From.Address));

                str.AppendLine(string.Format("ATTENDEE;CN=\"{0}\";PARTSTAT=ACCEPTED:mailto:{1}", message.To[0].DisplayName, message.To[0].Address));

                str.AppendLine("BEGIN:VALARM");
                str.AppendLine("TRIGGER:-PT15M");
                str.AppendLine("ACTION:DISPLAY");
                str.AppendLine("DESCRIPTION:Reminder");
                str.AppendLine("END:VALARM");
                str.AppendLine("END:VEVENT");
                str.AppendLine("END:VCALENDAR");

                System.Net.Mime.ContentType ct = new System.Net.Mime.ContentType("text/calendar");
                ct.Parameters.Add("method", "PUBLISH");
                AlternateView avCal = AlternateView.CreateAlternateViewFromString("abc", ct);

                message.AlternateViews.Add(avCal);
             
                client.Send(message);
Posted
Updated 3-Dec-13 21:16pm
v2

You cannot send an email as "localhost" as your SMTP client. That has to be legitimate in order for an email to be sent.
 
Share this answer
 
Comments
Adeel Ijaz 3-Dec-13 17:36pm    
i am using ip actually 10.10.0.45
Adeel Ijaz 3-Dec-13 17:37pm    
in the smtpclient.credentials i am not passing my actual userid and password, instead i am passing hard coded string "UserId" , "Password". Is it because of this ?
It has nothing to do with anything in your code except SmtpClient client = new SmtpClient("localhost");. It assumes that you are running SMTP server on your local host. I doubt you do. This parameter should be the host computer name or IP address of the host running some SMTP service.

—SA
 
Share this answer
 
Comments
Adeel Ijaz 3-Dec-13 17:28pm    
I am using Ip address = "10.10.0.45"
Sergey Alexandrovich Kryukov 3-Dec-13 17:43pm    
And..?!
—SA
Adeel Ijaz 3-Dec-13 17:29pm    
in the smtpclient.credentials i am not passing my actual userid and password, instead i am passing hard coded string "UserId" , "Password". Is it because of this ?
Sergey Alexandrovich Kryukov 3-Dec-13 17:43pm    
It depends on the SMPT server. Do you really use the server on local host?
—SA
Adeel Ijaz 3-Dec-13 17:44pm    
No, but i am testing the application. Actual would be the customer's server.
C#
System.Net.Mail.MailMessage meg = new System.Net.Mail.MailMessage();
System.Net.Mail.SmtpClient mailsender = new System.Net.Mail.SmtpClient();


try {
    string su ="Your Message" 

    string attachfilepath = "";
    //apppath get path for file
    if (File.Exists(apppath + "\\ErrorLogs.txt") == true) {
        attachfilepath = apppath + "\\ErrorLogs.txt";
    } else if (File.Exists(apppath + "\\ErrorLogs.txt") == true) {
        attachfilepath = apppath + "\\ErrorLogs.txt";
    }

    Attachment atm = new Attachment(attachfilepath);

    meg.From = new MailAddress("From Address", "SendeName");
    // Email address of the sender and Display name(optional)

    meg.To.Add("sample@gmail.com");
    //To address

    meg.CC.Add("sample@gmail.com");
    //Cc
    meg.Bcc.Add("sample@gmail.com");
    //Bcc
    meg.Subject = "This is text mail";
    // Email subject
    meg.Body = su;
    //Email body i.e. the content of the email
    meg.IsBodyHtml = true;
    meg.Attachments.Add(atm);
    //Attachment

    Configuration configurationFile = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath + "/web.config");
    MailSettingsSectionGroup mailSettings = configurationFile.GetSectionGroup("system.net/mailSettings");

    if (mailSettings != null) {
        mailsender.Host = mailSettings.Smtp.Network.Host;
        mailsender.Port = mailSettings.Smtp.Network.Port;
        if (Convert.ToBoolean(mailSettings.Smtp.Network.DefaultCredentials)) {
            mailsender.UseDefaultCredentials = true;
        }
        if (Convert.ToBoolean(mailSettings.Smtp.Network.EnableSsl)) {
            mailsender.EnableSsl = true;
        }
        mailsender.Credentials = new System.Net.NetworkCredential(mailSettings.Smtp.Network.UserName, mailSettings.Smtp.Network.Password);
    }
    mailsender.Send(meg);
} catch (Exception ex) {
    ShowMessage(ex.Message);

} finally {
    ShowMessage("Mail Successfully send");

}


SQL
for best coding try to give email address and password in web.config file
here the code for web.config



XML
<pre lang="xml">    <system.net>
        <mailSettings>
            <smtp>

                <network defaultCredentials="true" host="smtp.gmail.com" password="password" port="587" userName="emailaddress" enableSsl="true"/>
            </smtp>
        </mailSettings>
        <settings>
            <httpWebRequest useUnsafeHeaderParsing="true"/>
        </settings>
    </system.net></pre>


try to change port 465 to 587
 
Share this answer
 
v5
Use code in web.config for this email configuration.
 
Share this answer
 
C#
SmtpClient client = new SmtpClient("localhost");

Replace localhost with correct IP.

C#
client.Credentials = new System.Net.NetworkCredential("MYID", "PASSWORD");

Provide correct UserID and Password instead of "MYID" and "PASSWORD".
 
Share this answer
 

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