0
votes

This is my code:

string from = "[email protected]";
string to = "[email protected]";
string mailSubject = "mail demo";
string mailBody = "mail sent successfully";

MailMessage mess = new MailMessage(from, to, mailSubject, mailBody);
mess.IsBodyHtml = true;

SmtpClient emailClient = new SmtpClient("192.168.43.1", 25); //Server ip & port
emailClient.UseDefaultCredentials = false;
emailClient.Credentials = new System.Net.NetworkCredential("[email protected]", "****password****");

try
{
   emailClient.Send(mess);
   Response.Write("message sent");
}
catch (Exception ex)
{
    Response.Write("Exception caught in CreateTestMessage1(): {0}"+ ex.ToString());
}

but I get this error:

Exception caught in CreateTestMessage1(): {0}System.Net.Mail.SmtpException: Service not available, closing transmission channel. The server response was: Cannot connect to SMTP server 192.168.43.1 (192.168.43.1:25), connect error 10061

at System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint)
at System.Net.Mail.SmtpTransport.GetConnection(ServicePoint servicePoint)
at System.Net.Mail.SmtpClient.GetConnection()
at System.Net.Mail.SmtpClient.Send(MailMessage message) at mail_demo_project.register_mail_demo.submit_Click(Object sender, EventArgs e) in e:\dot net\projects\mail demo project\mail demo project\register_mail_demo.aspx.cs:line 53 error occured...

1
Well, the error seems to indicate there is NO SMTP server running on that machine at 192.168.43.1 ... have you checked??marc_s

1 Answers

0
votes

Use this code to send email:

string From = "[email protected]";
        MailAddress mailAddress = new MailAddress(From, "Contact Us");

        MailMessage mailMessage = new MailMessage();
        mailMessage.To.Add("[email protected]");
        mailMessage.From = mailAddress;
        mailMessage.Subject = Txt_Subject.Text;

        string mailBody = "User Email: " + Txt_msg.Text + "<br/> User Name: " + Txt_Name.Text + "<br/> Message: " + Txt_msg.Text  ;

        mailMessage.Body = mailBody;
        mailMessage.Priority = MailPriority.Normal;
        mailMessage.IsBodyHtml = true;

        System.Net.NetworkCredential credential = new System.Net.NetworkCredential(From, "password");

        SmtpClient smtpClient = new SmtpClient();
        smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
        smtpClient.EnableSsl = false;
        smtpClient.Host = "smtp.gmail.com";
        smtpClient.Port = 587;
        smtpClient.UseDefaultCredentials = true;
        smtpClient.Credentials = credential;

        smtpClient.Send(mailMessage);

and you have to make some security modification in your gmail account in security section :Allow less secure apps: YES