0

I have the following code which if I send an mail its showing email ending fail how to solve this one?

public void Sendemail(string toaddr)
    {
        if (Editor1.Content == null)
        {
            ScriptManager.RegisterStartupScript(this, GetType(), "showalert", "alert('Subject or Body of Email Should not be Empty...!');", true);
        }
        else
        {
            string[] arryaddr = toaddr.Split(',');
            MailMessage ms = new MailMessage();
            ms.IsBodyHtml = true;
            //ms.To.Add(new MailAddress("[email protected]"));
            ms.To.Add(new MailAddress(toaddr));
            string fromaddr = "[email protected]";
            for (int i = 0; i < arryaddr.Length; i++)
            {
                ms.Bcc.Add(new MailAddress(arryaddr[i].ToString()));
            }
            ms.From = new MailAddress(fromaddr);
            string subject = txtSubject.Text;
            ms.Subject = subject;
            string body = Editor1.Content;
            ms.Body = body;
            ms.Priority = MailPriority.Normal;
            SmtpClient smtp = new SmtpClient();

            smtp.Credentials = new System.Net.NetworkCredential("[email protected]", "dpo123");
            try
            {
                smtp.Send(ms);
                ScriptManager.RegisterStartupScript(this, GetType(), "showalert", "alert('Email Sent Successfully...!');", true);
            }
            catch (Exception ex)
            {
            }
        }
    }

I have the following email settings

smtp from="[email protected]"
network host="217.199.175.121"
port="25"
userName="[email protected]"
password="*****"
enableSsl="false" 
3
  • You can't send an email without an email server to send it to, so you need to know the host name of that email server. Most servers use port 25, and do not use ssl. Apr 28, 2014 at 7:54
  • if i put portno it ll go Apr 28, 2014 at 7:58
  • Off topic: It would be nicer to use: using(var ms = new MailMessage()) and using(var smtp = new SmtpClient()) the used resources will be released automatically when done.
    – Max
    Apr 28, 2014 at 8:32

2 Answers 2

2

SMTPClient uses SMTP (protocol) to send emails. Thus it requires the details about the SMTP Server. Consider the code below

        SmtpClient client = new SmtpClient();
        client.UseDefaultCredentials = false;
        client.Credentials = new NetworkCredential("[email protected]", "password");
        client.Port = 587;
        client.Host = "smtp.gmail.com";
        client.EnableSsl = true;

In the above code, we have provided the following information to the SMTP client to work properly

  • Host: The smtp server details. As you can see, by default you use @gmail.com but here it is specified smtp.gmail.com
  • Port: The port to connect to the smtp server
  • EnableSsl: If ssl connection is required
  • Credentials: The account using whose credentials the mail should be sent.

Your code is missing the Host and Port (which are also required along with Credentials, EnableSsl is false, by default)

To know about details (host, port, Ssl requirement) of SMTP server at digitalprintonline.co.uk, you will have to contact the administrator there. Alternatively, you can use GMail SMTP server details along with your GMail account credentials.

7
  • the SMTP settings that you've specified are place in web.config? Apr 28, 2014 at 8:38
  • Then, please post this part of your web.config. As MSDN says (msdn.microsoft.com/en-us/library/k1c4h6e2%28v=vs.110%29.aspx) This constructor [new SmtpClient()] initializes the Host, Credentials, and Port properties for the new SmtpClient by using the settings in the application or machine configuration files. Apr 28, 2014 at 9:56
  • is any exception getting raised. if yes, what is the error message (ex.Message)? Apr 28, 2014 at 10:28
  • Are you behind any firewall? may be port 25 is blocked... as your server is accessible and working fine Apr 28, 2014 at 10:36
  • also, try adding smtp.UseDefaultCredentials = false (before smtp.Credentials = .... line) Apr 28, 2014 at 10:36
1

You don't need to have a server on your local machine in order to send a message to a SmtpClient. Your email service provider such as smtp.gmail.com has the server and your SmtpClient communicates with that server.

8
  • if i put gmail it will send mail but if i put diff from mail id it will not send Apr 28, 2014 at 8:04
  • without tat gmail.comserver we cant send mail ? Apr 28, 2014 at 8:12
  • 1
    You can use smtp.live.com or smtp.mail.yahoo.com as well. Port numbers are 587 and 465 respectively. Apr 28, 2014 at 8:23
  • 1
    Write smtp.Host = "smtp.gmail.com"; in your code after initializing a new SmtpClient. Apr 28, 2014 at 8:44
  • 1
    You need to add smtp.Host = "smtp.gmail.com"; in your code. before the try-catch part Apr 28, 2014 at 10:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.