Click here to Skip to main content
16,007,277 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;

public partial class feed_back : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            MailMessage mailmessage = new MailMessage();
            mailmessage.To.Add(TextBox1.Text);
            mailmessage.From = new MailAddress(TextBox2.Text);
            mailmessage.Subject = TextBox3.Text;
            mailmessage.Body = TextBox4.Text;
            SmtpClient smtpclient = new SmtpClient("");
           
            smtpclient.Credentials = new System.Net.NetworkCredential("", "smtpclient");
            smtpclient.Send(mailmessage);
            Label5.Text = "Email sent";
             
        }
        catch (Exception ex)
        {
            Label5.Text = "Could not send email-error:" + ex.Message;

        }
    }
}

I am using this code. Its displaying error.

"Could not send email-error:The SMTP host was not specified."

I had done all the smtp configure settings in IIS.

What is the solution for this error.
Posted
Updated 6-Sep-11 1:09am
v2

You need to provide the SMTP server name, I hope this piece of code might help you in understanding

C#
using System;
using System.Windows.Forms;
using System.Net.Mail;
namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                MailMessage mail = new MailMessage();
                SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
                mail.From = new MailAddress("your_email_address@gmail.com");
                mail.To.Add("to_address@mfc.ae");
                mail.Subject = "Test Mail";
                mail.Body = "This is for testing SMTP mail from GMAIL";
                SmtpServer.Port = 587;
                SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
                SmtpServer.EnableSsl = true;
                SmtpServer.Send(mail);
                MessageBox.Show("mail Send");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    }
}
 
Share this answer
 
Try this,

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.ComponentModel;
using System.Windows.Forms;

namespace DataSimulator
{
    class EmailFunctions
    {

        //ayarlar
        public SmtpClient client = new SmtpClient();
        public MailMessage msg = new MailMessage();
        public System.Net.NetworkCredential smtpCreds = new System.Net.NetworkCredential("frommailaddress@gmail.com", "frommailpassword");

        public void SendMail(String sendTo, String sendFrom, String subject, String body)
        {
            try
            {
                //smtp client configurations
                client.Host = "smtp.gmail.com";
                client.Port = 587; //gmail smtp port number "587"
                client.UseDefaultCredentials = false;
                client.Credentials = smtpCreds;
                client.EnableSsl = true;

                MailAddress to = new MailAddress(sendTo);
                MailAddress from = new MailAddress(sendFrom);

                //mail mesajı ayarları
                msg.Subject = subject;
                msg.Body = body;
                msg.From = from;
                msg.To.Add(to);

                //gönderme işlemi
                client.Send(msg);
                MessageBox.Show("successfull", "success");
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message,"unsuccessfull!");
            }
        }
        
    }
}
 
Share this answer
 
Use this code create html for your mail content and class to send mail

C#
namespace MailerService
{
    public class MailerController
    {
          public static void SendUserConfirmationMail(User _user)
        {
            Mailer mailers = new Mailer();
            mailers.RecipientsEmailId = "abc@abc.com";
            mailers.RecipientsName = _"Test";
            mailers.Subject = "Testing Mail";
            mailers.MessageSendingTime = DateTime.Now;
            mailers.UserName = "Abc";
            MailHelper mailHelper = new MailHelper();
            mailHelper.SendMail(mailers, "Test.htm");
        }
    }
 }


C#
namespace MailerService
{
    public class MailHelper
    {
        #region Public variable
        public string MailTo
        { get; set; }
        public string MailFrom
        { get; set; }
        public string MailSubject
        { get; set; }
        public string MailBody
        { get; set; }
        public string MailKey
        { get; set; }
        #endregion

        public MailHelper()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public void SendMail(Mailer mailers, string filePath)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(System.IO.File.ReadAllText(Settings.GetApplicationRootPath() + "HtmlMails//" + filePath));
            sb = sb.Replace("Identifier1", mailers.Identifier1);
            sb = sb.Replace("Identifier2", mailers.Identifier2);
            sb = sb.Replace("Identifier3", mailers.Identifier3);
            sb = sb.Replace("Identifier4", mailers.Identifier4);

            MailTo = mailers.RecipientsEmailId;
            MailFrom = WebConfigurationManager.AppSettings["SmtpServerUserName"];
            MailSubject = mailers.Subject;
            MailBody = sb.ToString();
            SendMail();
        }



        #region To send mail
        public void SendMail()
        {
            MailHelper objMail = new MailHelper();
            objMail.MailBody = this.MailBody;
            objMail.MailFrom = this.MailFrom;
            objMail.MailSubject = this.MailSubject;
            objMail.MailTo = this.MailTo;
            Thread th = new Thread(delegate() { ThreadSend(objMail); });
            th.Start();
        }
        public void ThreadSend(MailHelper obj)
        {
            SmtpClient smtp = new SmtpClient();
            MailMessage eMail = new MailMessage();
            eMail.IsBodyHtml = true;
            eMail.From = new MailAddress(obj.MailFrom);
            eMail.To.Add(obj.MailTo);
            smtp.Host = WebConfigurationManager.AppSettings["SmtpServer"];    //Settings.SmtpServer;
            smtp.EnableSsl = false;   // Settings.SmtpServerSSL;
            eMail.Body = obj.MailBody;
            eMail.Subject = obj.MailSubject;
            smtp.Port = WebConfigurationManager.AppSettings["SmtpServerPort"].ToInteger();   // Settings.SmtpServerPort;
            smtp.Credentials = new System.Net.NetworkCredential(WebConfigurationManager.AppSettings["SmtpServerUserName"], WebConfigurationManager.AppSettings["SmtpServerPassword"]);
            smtp.Send(eMail);
        }

        void smtp_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            throw new NotImplementedException();
        }
        #endregion
    }
}
 
Share this answer
 
Hi anusangeetha,
Go through the following video
click here
 
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