Sending email from ASP.NET

Web hosting Forums and discussions

Forums for Web hosting discussions: If you have any queries or questions related to web hosting services, place them here and get answered by our experts and customers. We invite you to even participate and provide solutions or answers that are known to any queries placed in this forums. We will get answer for all the queries and questions raised here.

Sending email from ASP.NET

I would like to send mail via code with ASP.NET4. What is the SMTP server info and do I need username and password on SMTPClient to authenticate?

The discussion has been resolved.

Accepted Answer

  • Replied by Sandy on Friday, July 27 2012, 06:41 AM
    using System.Net;
    using System.Net.Mail;

    Sample code using SMTPClient

    MailMessage mail = new MailMessage();
    mail.From = new MailAddress("info@my");
    mail.Subject = "Test Subject";
    mail.Body = "Test Body";
    SmtpClient smtp = new SmtpClient("", 25);
    smtp.Credentials = new NetworkCredential("info@my","my email password");
    smtp.UseDefaultCredentials = false;

    • SPB - more than a month ago
      comment the line as below
      //smtp.UseDefaultCredentials = false;
    CEO, Owner of HostASP
  • Replied by Brian Jackson on Saturday, July 21 2012, 01:17 PM · Hide · #1
    I got it.
  • Replied by Sandy on Friday, July 27 2012, 06:34 AM · Hide · #2
    Your SMTP server name is "mail. your domain name" Yes, you will need to use the email account for authentication. Open relay is not enabled on our servers.

    Your email account with password (complete address like you @ yourdomain .com)
    CEO, Owner of HostASP
  • Replied by SPB on Friday, September 07 2012, 01:31 PM · Hide · #3
    The above code gave me a "graylist" error but the following worked for me in VB.

    Dim SmtpServer As New SmtpClient()
    Dim mail As New MailMessage()
    SmtpServer.Credentials = New _
    Net.NetworkCredential("EmailAddress", "Password")
    SmtpServer.Port = 25
    SmtpServer.Host = ""
    mail = New MailMessage()
    mail.From = New MailAddress("FromEmailAddress")
    mail.Subject = "Subject"
    Dim body As String = "Body"
    mail.Body = body
    1 vote by John Baird
    • Sandy - more than a month ago
      The only difference I could see here is this one in VB language (syntax changes) and the other earlier sample is in C#. Gray list error will typically appear from SMTP server if SMTP authentication is not been performed. We would also need to ensure that the sender (from) email id and authenticated SMTP email (NetworkCredential) are of same domain so that emails sent to recipients are not parked directly into their SPAM folder.
    • John Baird - more than a month ago
      This is certainly NOT the same as the earlier sample. This code works, the above does not. You can convert this code to C# using an online converter and see for yourself. + 1
    Team lead @HA
    Replied by Marcel Kucera on Monday, September 10 2012, 09:46 PM · Hide · #4
    Tried the code above, got en exception "Unable to connect to the remote server "!
    Same code is working fine with my google account and their server "" on port 587.
    I checked with "telnet 25" and got no response
    Seems to me that port 25 is not open, neither 587 or 465 which I also tried with telnet and got no response
    Which port shall I use then?
    • Sandy - more than a month ago
      Hi, you must be trying to send email from your local machine. It looks like your ISP had blocked the port. Try this alternate port 8889. There is also a Ticket reply in your account #865641.
Your Response
Please login first in order for you to submit comments