C# .NET - The remote name could not be resolved: 'smtp.gmail.com'

Asked By Daniel on 22-Jan-12 01:19 PM
Hi!
I getting the following error when trying to send a file uploaded to my email address
<<The remote name could not be resolved: 'smtp.gmail.com'>>
I am using the following code:


            string toEmailAddress = "xx@gmail.com";
            string gmailId = "abc@gmail.com";
            string password = "";
            string bodyMsg = "Hello its testing mail";
            MailMessage mail = new MailMessage();
            mail.To.Add(toEmailAddress);
            mail.From = new MailAddress(gmailId);
            mail.Subject = TextBoxEmailAdress.Text;
            mail.Body = bodyMsg;
            mail.IsBodyHtml = true;
            if (FileUpload1.HasFile)
            {
                mail.Attachments.Add(new Attachment(FileUpload1.PostedFile.InputStream, FileUpload1.FileName));


            }
            SmtpClient smtp = new SmtpClient("smtp.gmail.com");
            smtp.EnableSsl = true;
            smtp.UseDefaultCredentials = false;
            smtp.Credentials = new System.Net.NetworkCredential(gmailId, password);
            smtp.Send(mail);
 and this image represent the form that I have on how I want to perform that:





Sreekumar P replied to Daniel on 22-Jan-12 02:24 PM
Hi,

In ur code I seen that the Port no is missing try after adding that .

SmtpClient.Port=587

Try the below code too.

Be sure to use System.Net.Mail, not the deprecated System.Web.Mail. Doing SSL with System.Web.Mail is a gross mess of hacky extensions.

using System.Net;
using System.Net.Mail;
 
var fromAddress = new MailAddress("from@gmail.com", "From Name");
var toAddress = new MailAddress("to@example.com", "To Name");
const string fromPassword = "fromPassword";
const string subject = "Subject";
const string body = "Body";
 
var smtp = new SmtpClient
       {
         Host = "smtp.gmail.com",
         Port = 587,
         EnableSsl = true,
         DeliveryMethod = SmtpDeliveryMethod.Network,
         UseDefaultCredentials = false,
         Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
       };
using (var message = new MailMessage(fromAddress, toAddress)
           {
             Subject = subject,
             Body = body
           })
{
  smtp.Send(message);
}
Devil Scorpio replied to Daniel on 22-Jan-12 03:33 PM
Hi,

Check this code

string from = me@gmail.com; //Replace this with your own correct Gmail Address

string to = you@gmail.com //Replace this with the Email Address to whom you want to send the mail

System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();<o:p />
<o:p> </o:p>mail.To.Add(to);
 mail.From = new MailAddress(from, "One Ghost" , System.Text.Encoding.UTF8);<o:p />
mail.Subject = "This is a test mail" ;<o:p />
mail.SubjectEncoding = System.Text.Encoding.UTF8;<o:p />
mail.Body = "This is Email Body Text";<o:p />
mail.BodyEncoding = System.Text.Encoding.UTF8;<o:p />
mail.IsBodyHtml = true ;<o:p />
mail.Priority = MailPriority.High;<o:p />
<o:p />
SmtpClient client = new SmtpClient();<o:p />
//Add the Creddentials- use your own email id and password

 client.Credentials = new System.Net.NetworkCredential(from, "Password");
<o:p />
client.Port = 587; // Gmail works on this port<o:p />
<o:p />client.Host = "smtp.gmail.com";<o:p />
client.EnableSsl = true; //Gmail works on Server Secured Layer
       try<o:p />
        {<o:p />
            client.Send(mail);<o:p />
        }<o:p />
        catch (Exception ex)<o:p /> 
        {<o:p />
            Exception ex2 = ex;<o:p />
            string errorMessage = string.Empty;<o:p /> 
            while (ex2 != null)<o:p />
            {<o:p />
                errorMessage += ex2.ToString();<o:p />
                ex2 = ex2.InnerException;<o:p />
            }<o:p />
<o:p> </o:p>  HttpContext.Current.Response.Write(errorMessage );<o:p />
        } // end try 
<pre />

Reference :- http://www.codeproject.com/Articles/26971/Sending-E-mail-using-ASP-net-through-Gmail-account
dipa ahuja replied to Daniel on 22-Jan-12 03:49 PM
This may be because coz of your username or password is not correct, try to set by web.config

Step 1 : Add this in Web.config
    
<system.net>
<mailSettings>
<smtp>
<network host="smtp.gmail.com"
  port="587"
  userName=""
  password=""
</smtp>
</mailSettings>
</system.net>
  
Step 2 : Mail Sending Code
 
using System.Net.Mail;
using System.Net;
 
protected void btnSent_Click(object sender, EventArgs e)
{
  string toEmailAddress = "xx@gmail.com";
  string GmailId = "abc@gmail.com";
  string bodyMsg = "helo its testing mail";
  string subject = "testing mail";
  MailMessage mail = new MailMessage();
  mail.To.Add(toEmailAddress);
 
  mail.From = new MailAddress(GmailId);
  mail.Subject = subject;
  mail.Body = bodyMsg;
  mail.IsBodyHtml = true;
 
    SmtpClient smtp = new SmtpClient();
  smtp.EnableSsl = true;
  smtp.Send(mail);
}
 
 

[)ia6l0 iii replied to Daniel on 22-Jan-12 09:38 PM
Please check the inner Exception. This general WebException that you see, would always have an inner exception that is more specific. And I don't think it is the missing port issue as described above, because the domain is not accessible at all. 

And also note that this means that the client can not connect to the DNS server to resolve the name. This also means that is a network connectivity issue. You need to first have this in place, to place a hostname resolve query on the DNS server.

If you have a proxy, ensure that you have bypassed this url. If you have one, make sure you add it to your defaultProxy node in your web.config
<system.net>
    <defaultProxy>
        <proxy usessystemdefault="False" proxyaddress="proxyaddress" bypassonlocal="True" autoDetect="False" />
    </defaultProxy> 
</system.net>


Hope this helps.
Jitendra Faye replied to Daniel on 22-Jan-12 11:21 PM
This is because of SMTP settings .

try to set setting like this-


SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.UseDefaultCredentials = False;
smtp.Credentials = new System.Net.NetworkCredential ("YourUserName@gmail.com","YourGmailPassword"); smtp.EnableSsl = true; smtp.Send(mail);




Try this and let em know.