Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I tried to send Email from asp.net I am using this code


C#
MailMessage msg = new MailMessage();
            msg.From = new MailAddress("username@domain.com");
            msg.To.Add(TextBox1.Text);
            msg.Subject = TextBox2.Text;
            msg.Body = TextBox3.Text;

            SmtpClient sc = new SmtpClient("wmail.link.net", 25);
            sc.Credentials = new NetworkCredential("username", "xxxxxx", "wmail.link.net");
            sc.Send(msg);
            LabelResult.Text = "Mail sent";
            LabelResult.Font.Size = 30;

and I get this error

System.Net.Mail.SmtpException: Failure sending mail. ---> System.Net.WebException: The remote name could not be resolved: 'wmail.link.net' at System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket6, Int32 timeout) at System.Net.PooledStream.Activate(Object owningObject, Boolean async, Int32 timeout, GeneralAsyncDelegate asyncCallback) at System.Net.PooledStream.Activate(Object owningObject, GeneralAsyncDelegate asyncCallback) at System.Net.ConnectionPool.GetConnection(Object owningObject, GeneralAsyncDelegate asyncCallback, Int32 creationTimeout) at System.Net.Mail.SmtpConnection.GetConnection(String host, Int32 port) at System.Net.Mail.SmtpClient.Send(MailMessage message) --- End of inner exception stack trace --- at System.Net.Mail.SmtpClient.Send(MailMessage message) at _Default.Button1_Click(Object sender, EventArgs e)


any help please!
Posted

Please use a local Email-Server for testing: http://www.hmailserver.com/[^]

It's free.

your provider will limit the number of accesses to the pop/smtp and might also ban you completely if you try to shoot the SMTP/POP with strange commands.

Your Exception
The remote name could not be resolved: 'wmail.link.net' 


says that the address is not existing or at least can not be resolved to an IP address.
 
Share this answer
 
This error message is quite self-explanatory: The remote name could not be resolved: 'wmail.link.net'
You cannot create a connection to the SMTP server you have specified.

There is nothing wrong with your code. You just need to check why you can't access the SMTP ('wmail.link.net') server

For starters, try pinging wmail.link.net from the machine your application is running on.
 
Share this answer
 
If you cannot connect to any smtp you should consult your network department because it seems to be a general problem with a firewall between you and your provider. You can also try to disable your local windows firewall.

Also you can refer the same question in System.Net.Mail.SmtpException:Failure sending mail[^]
 
Share this answer
 
string RoMaMailID = System.Configuration.ConfigurationManager.AppSettings.Get("FromMailID");
string RoMaMailServer = System.Configuration.ConfigurationManager.AppSettings.Get("MailServerName");
C#
string RoMaUserName = System.Configuration.ConfigurationManager.AppSettings.Get("USername");
      string RoMaPassword = System.Configuration.ConfigurationManager.AppSettings.Get("Password");

C#
string Subject = "Test Mail";
          System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(RoMaMailServer);
          if (!RoMaUserName.ToString().Equals("")) {
              client.UseDefaultCredentials = false;
              client.Credentials = new System.Net.NetworkCredential(RoMaUserName, RoMaPassword);
          }
          client.Port = 465;


          client.EnableSsl = true;
          client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;

C#
System.Net.Mail.MailMessage Mail = new System.Net.Mail.MailMessage("Roadside Systems<" + FromMailID+ ">",To, Subject, Message);
           Mail.IsBodyHtml = true;
           try
           {
               //client.Send(Mail);
               SendEmail(To, Subject, Message);
               Response.Write("Mail send Successfully");
           }
           catch (Exception Ex)
           {
               Response.Write(Ex.Message);
           }
 
Share this answer
 
Comments
TorstenH. 27-Aug-12 7:05am    
?? and your solution is to copy some code from somewhere else or what?
Try This, By use of below sample code you can send email with attachment.


http://hemantrautela.blogspot.in/2012/09/advance-email-sending-codecnet-with.html[^]
 
Share this answer
 
C#
protected void SendMail()
{
    // Gmail Address from where you send the mail
    var fromAddress = "Gmail@gmail.com";
    // any address where the email will be sending
    var toAddress = YourEmail.Text.ToString();
    //Password of your gmail address
    const string fromPassword = "Password";
     // Passing the values and make a email formate to display
    string subject = YourSubject.Text.ToString();
    string body = "From: " + YourName.Text + "\n";
    body += "Email: " + YourEmail.Text + "\n";
    body += "Subject: " + YourSubject.Text + "\n";
    body += "Question: \n" + Comments.Text + "\n";
    // smtp settings
    var smtp = new System.Net.Mail.SmtpClient();
    {
        smtp.Host = "smtp.gmail.com";
        smtp.Port = 587;
        smtp.EnableSsl = true;
        smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
        smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
        smtp.Timeout = 20000;
    }
    // Passing values to smtp object
    smtp.Send(fromAddress, toAddress, subject, body);
}

protected void Button1_Click(object sender, EventArgs e)
{
    try
    {
        //here on button click what will done
        SendMail();
        DisplayMessage.Text = "Your Comments after sending the mail";
        DisplayMessage.Visible = true;
        YourSubject.Text = "";
        YourEmail.Text = "";
        YourName.Text = "";
        Comments.Text = "";
    }
    catch (Exception) { }
}


I have used above Code and it's working fine.
 
Share this answer
 
Comments
Member 10695198 22-May-14 5:33am    
how we can attach a file using this code

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