locked
Send Email from C# Executable

    Question

  • I know how to send an email message from a C# web program but how do you send an email message from a C# executable?

    Thanks for any help you can provide.

    Mike

    Monday, September 25, 2006 4:23 PM

Answers

  • pretty much the same way, depending on how you did it in ASP.NET

    there are a few threads about this. Hope this helps, import the System.Net.Mail namespace:



    MailMessage theMailMessage = new MailMessage("from@email.com", "to@email.com");
    theMailMessage.Body = "body email message here";
    theMailMessage.Attachments.Add(new Attachment("pathToEmailAttachment"));
    theMailMessage.Subject = "Subject here";
     
    SmtpClient theClient = new SmtpClient("IP.Address.Of.Smtp");
    theClient.UseDefaultCredentials = false;
    System.Net.NetworkCredential theCredential = new System.Net.NetworkCredential("user@name.com", "password");
    theClient.Credentials = theCredential;
    theClient.Send(theMailMessage);

     

     

    does this help? Be sure to use a valid SMTP server address and pass it your credential details as shown

    Monday, September 25, 2006 6:33 PM
    Moderator

All replies

  • pretty much the same way, depending on how you did it in ASP.NET

    there are a few threads about this. Hope this helps, import the System.Net.Mail namespace:



    MailMessage theMailMessage = new MailMessage("from@email.com", "to@email.com");
    theMailMessage.Body = "body email message here";
    theMailMessage.Attachments.Add(new Attachment("pathToEmailAttachment"));
    theMailMessage.Subject = "Subject here";
     
    SmtpClient theClient = new SmtpClient("IP.Address.Of.Smtp");
    theClient.UseDefaultCredentials = false;
    System.Net.NetworkCredential theCredential = new System.Net.NetworkCredential("user@name.com", "password");
    theClient.Credentials = theCredential;
    theClient.Send(theMailMessage);

     

     

    does this help? Be sure to use a valid SMTP server address and pass it your credential details as shown

    Monday, September 25, 2006 6:33 PM
    Moderator
  • And can u tell me how can i send email message from a C# web program?
    Monday, May 14, 2007 11:22 PM
  • any idea about this with smtp servers that require authentication?
    Tuesday, May 15, 2007 8:51 PM
  •  

    hii

     

    what's the stmp and pass ???????????

     

    my email in hotmail but i donno what is the stmp and pass

     

     

    g0000d bye

    Monday, August 13, 2007 5:58 PM
  • hotmail does not allow programmatically sending of emails I believe.

    the username and password is the email address and the password associated with that email address.

     

    Monday, August 13, 2007 9:22 PM
    Moderator
  •  

    Assuming that the application is running on a client from which email can be sent, is there a way to programmatically discover using C# the name of the email server without having the user of the application enter it?  The goal is for an application to to automatically send an email, containing for example a registration form,  to a recipient who is the manufacturer of the application.
    Thursday, October 18, 2007 6:54 PM
  • i was searching for it thank you alot it is working good.
    Thursday, November 13, 2008 2:53 AM
  • tnx a lot ahmedilyas, i was working all day long on this, and you saved me : )
    Wednesday, May 20, 2009 5:06 PM
  • im doing a contact form and trying to send emails using c#.

    i have this code

    string message = "Phone number:" + PhoneNumber.Text + "<br/>"
                                            + "Organization:" + Organization.Text + "<br/>"
                                            + "Comments:" + Comments.Text + "<br/>";
                    SmtpClient client = new SmtpClient("relay-hosting.secureserver.net",25);
                    MailMessage msg = new MailMessage();
                    msg.From = new MailAddress(EmailAddress.Text);
                    msg.To.Add(new MailAddress("xxxxx@hotmail.com"));
                    msg.Subject = ("new mail");
                    msg.Body = message;
                    msg.Priority = MailPriority.High;
                    client.Send(msg);
                    MessageLabel.Text = "Message Sent";
                }


    but on the website is showing this error
    Message not Sent at System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response) at System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] command, String from) at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, SmtpFailedRecipientException& exception) at System.Net.Mail.SmtpClient.Send(MailMessage message) at Contact.Button1_Click(Object sender, EventArgs e) in d:\hosting\5434839\html\Contact.aspx.cs:line 39

    any help
    Thursday, January 14, 2010 10:35 PM
  • im doing a contact form and trying to send emails using c#.

    i have this code

    string message = "Phone number:" + PhoneNumber.Text + "<br/>"
                                            + "Organization:" + Organization.Text + "<br/>"
                                            + "Comments:" + Comments.Text + "<br/>";
                    SmtpClient client = new SmtpClient("relay-hosting.secureserver.net",25);
                    MailMessage msg = new MailMessage();
                    msg.From = new MailAddress(EmailAddress.Text);
                    msg.To.Add(new MailAddress("xxxxx@hotmail.com"));
                    msg.Subject = ("new mail");
                    msg.Body = message;
                    msg.Priority = MailPriority.High;
                    client.Send(msg);
                    MessageLabel.Text = "Message Sent";
                }


    but on the website is showing this error
    Message not Sent at System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response) at System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] command, String from) at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, SmtpFailedRecipientException& exception) at System.Net.Mail.SmtpClient.Send(MailMessage message) at Contact.Button1_Click(Object sender, EventArgs e) in d:\hosting\5434839\html\Contact.aspx.cs:line 39

    any help
    Thursday, January 14, 2010 10:35 PM
  • SmtpClient theClient = new SmtpClient("IP.Address.Of.Smtp");

    what wll this line do.what should i write in place of "IP.Address.Of.Smtp" which ip? ,and which address ?  with eg plz.

    Thursday, September 02, 2010 1:23 PM
  • SmtpClient theClient = new SmtpClient("IP.Address.Of.Smtp");

    what wll this line do.what should i write in place of "IP.Address.Of.Smtp" which ip? ,and which address ?  with eg plz.

     The 'Ip.AddressOf.Smtp' is the publically visible Ip Address of your SMTP Server. If it's on the same machine as your c# .exe just use 127.0.0.1. This ctor also assumes the default port of 25 is being used. If not then use the overloaded ctor that takes a string (ip address) and an int (port number). 

     

    Friday, September 24, 2010 2:48 PM
  • Whats wrong with my code? It is show - Failure sending email.

                MailMessage theMailMessage = new MailMessage("from@inbox.lv", "to@hotmail.com");
                Uri dd = new Uri("http://www.hotmail.com");
                string host = dd.Host;
                int port = dd.Port;
                theMailMessage.Body = "Pashol nahuj";
                theMailMessage.Subject = "Ei dirst";
                SmtpClient theClient = new SmtpClient(host,port);
                theClient.UseDefaultCredentials = false;
                System.Net.NetworkCredential theCredential = new System.Net.NetworkCredential("from@inbox.lv", "password");
                theClient.Credentials = theCredential;
                theClient.Send(theMailMessage);


    Thursday, May 03, 2012 10:00 AM