• (3 Pages)
  • +
  • 1
  • 2
  • 3

Sending e-mail messages using C# How to send e-mail messages using C# and WinForms.

#31 sumanthkumarch  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 7
  • Joined: 08-February 12

Posted 08 February 2012 - 08:12 PM

i kindly request you to tell all the sample parameters to be entered in the application for gmail or ymail or any mail server to send a email

This post has been edited by modi123_1: 08 February 2012 - 10:38 PM
Reason for edit:: removed excessive quote

Was This Post Helpful? 0
  • +
  • -

#32 sumanthkumarch  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 7
  • Joined: 08-February 12

Posted 08 February 2012 - 08:18 PM

i have used sendserver:smtp.gmail.com serverport:2525 from:mymail id to:frenmail id uname:my mail id pasword: mailid password


I am getting an error mail sending failed.Please help me this code is very useful in my project.
Was This Post Helpful? 0
  • +
  • -

#33 shendism  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 3
  • Joined: 09-February 12

Posted 09 February 2012 - 05:57 PM

View Postsumanthkumarch, on 08 February 2012 - 08:18 PM, said:

i have used sendserver:smtp.gmail.com serverport:2525 from:mymail id to:frenmail id uname:my mail id pasword: mailid password


I am getting an error mail sending failed.Please help me this code is very useful in my project.


Try smtpserverport:587 i write some code maybe will help you... i'm not understanding what exactly are you looking for
Hope i help you

MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com"); 
mail.From = new MailAddress("email@gmail.com");
mail.To.Add("email@smth.smth");       
mail.Subject = "Name of subject"
mail.Body = "mail with attachment";
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("email@gmail.com", "paswword");


Was This Post Helpful? 0
  • +
  • -

#34 Joerabbit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 19-March 12

Posted 19 March 2012 - 05:06 AM

View Postsumanthkumarch, on 08 February 2012 - 08:18 PM, said:

i have used sendserver:smtp.gmail.com serverport:2525 from:mymail id to:frenmail id uname:my mail id pasword: mailid password


I am getting an error mail sending failed.Please help me this code is very useful in my project.


Like Guest_Ed mentioned

sendserver = smtp.gmail.com // sets the server you're sending from to gmail
serverPort = 587 // the port used for gmail

and then you must set Enablessl to true, to activate SecureSocketlayer(SSL)

i.e. if you just want to send to from gmail server you can use:

client.Host = "smtp.gmail.com";
client.Port = 587;
client.EnableSsl = true;

or just type in:
client.EnableSsl = true; // in your button1, before you send the msg via client.Send(message);
Was This Post Helpful? 0
  • +
  • -

#35 dygr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 11-June 12

Posted 11 June 2012 - 01:09 PM

Hello,

I have successfully sent an email using our exchange server using this and for that I would like to thank you.
Additionally I would like to embed an image in the email, such as a signature or banner. An advice on how to go about this?
Was This Post Helpful? 0
  • +
  • -

#36 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6037
  • View blog
  • Posts: 23,435
  • Joined: 23-August 08

Posted 11 June 2012 - 01:24 PM

View Postdygr, on 11 June 2012 - 04:09 PM, said:

Hello,

I have successfully sent an email using our exchange server using this and for that I would like to thank you.
Additionally I would like to embed an image in the email, such as a signature or banner. An advice on how to go about this?


http://msdn.microsof...ontenttype.aspx
Was This Post Helpful? 0
  • +
  • -

#37 RPG_Lover  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 28-June 12

Posted 28 June 2012 - 02:42 PM

I was wondering if there is a way to make it a one way Email. I know it is possible but I would like to know how to program that. Where the Message when input get sent to the Email provided in the code (Kinda like a Contact Us on the websites. But instead for the Program app.) I would be glad and I only need the following for my Contact.

The name
The email
The subject
The comment

I do not need all that Server stuff. So I would be happy if you reply the answer.
Was This Post Helpful? 0
  • +
  • -

#38 supperthin02  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 30-September 12

Posted 30 September 2012 - 11:08 PM

---------------------------

---------------------------
Cannot send message: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Must issue a STARTTLS command first. mz10sm488277pbc.37
---------------------------
OK
---------------------------
HELP ME
Was This Post Helpful? 0
  • +
  • -

#39 minhkhanh62  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 02-June 11

Posted 02 October 2012 - 07:41 AM

View Postsupperthin02, on 30 September 2012 - 11:08 PM, said:

---------------------------

---------------------------
Cannot send message: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Must issue a STARTTLS command first. mz10sm488277pbc.37
---------------------------
OK
---------------------------
HELP ME



pm me. i'll help you.
facebook : http://www.facebook....inhkhanh.dinh.5

i'll send you all source code. u just press f5 to run ^^
Was This Post Helpful? 0
  • +
  • -

#40 amit90510  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 05-January 13

Posted 05 January 2013 - 02:54 AM

View PostPixelCard, on 12 July 2008 - 07:00 AM, said:

In this tutorial I will show how to send e-mail messages from a C# application.

Tutorial Requirements:
  • C# IDE (Visual Studio 2008 used in this tutorial)
  • .NET Framework 2.0Attachment Please Help.txt

For this application it is needed to declare one additional namespace:

// Accessing an additional namespace for mail sending.
using System.Net.Mail;



So, here are the steps needed to be performed:

1. Create a standard C# Windows Forms application:

Posted Image

2. Add some labels and text boxes and two buttons to the form, so the form looks like this:

Posted Image

As you see, the current text in the text boxes is the name of each text box. You have to change the names to the corresponding boxes to make the code work. However, you can leave the default names, but don't forget to change the code.

3. Change the passBox PasswordChar property to '*', so when you will enter the password for the mail box, no one could see it:

Posted Image

4. For the contentBox change the AcceptsReturn and AcceptsTab properties to true, so when you will enter the message, you can use the TAB and ENTER keys to format the text:

Posted Image

Now, when the form is ready, let's pass to the code.


1. As the System.Net.Mail namespace was decalred at the beginning of the tutorial, we can pass to the sending code (for the Send button):


private void button1_Click(object sender, EventArgs e)
        {
            // To avoid situations, when the program crashes because server rejection or
            // invalid data, an exception handling mechanism is created.
            try
            {
                // Creating a new SMTP Client. The server URL/IP is indicated as
                // sendServer.Text (that is the text box with the data).
                SmtpClient client = new SmtpClient(sendServer.Text);

                // Creating a new mail message. The sender and receiver are
                // indicated as sendFrom.Text and SendTo.Text
                // (these are the text boxes with the data).
                MailMessage message = new MailMessage(sendFrom.Text, sendTo.Text);

                // The message body is the message content provided in the
                // contentBox.
                message.Body = contentBox.Text;

                // The message subject is located in the subjectBox.
                message.Subject = subjectBox.Text;

                // To be able to send the message, it is necessary to provide the
                // credentials on the server. The username is located in the userBox
                // and the password is located in the passBox.
                client.Credentials = new System.Net.NetworkCredential(userBox.Text, passBox.Text);

                // Some servers require a specific port to connect,
                // so it is specified in the serverPort text box.
                if (serverPort.Text != null)
                client.Port = System.Convert.ToInt32(serverPort.Text);

                // Send the message.
                client.Send(message);
            }

            // This catches the exceptions, if any.
            catch (Exception ex)
            {
                // Show a message, explaining the problem.
                MessageBox.Show("Cannot send message: " + ex.Message);
            }
        }



2. There is also code for the 'Clear Fields' button:


// Clear every field.
            sendServer.Clear();
            serverPort.Clear();
            sendTo.Clear();
            sendFrom.Clear();
            userBox.Clear();
            passBox.Clear();
            subjectBox.Clear();
            contentBox.Clear();



The application is ready.

This application is using SMTP (Simple Mail Transfer Protocol) to send messages, so you must have access toy uor mail provider's SMTP server (which is available for most mail services). The SMTP server address usually looks like this:

smtp.yourmailprovider.domain

The most commonly used SMTP port is:

2525


System.Net.Mail.SmtpException: Failure sending mail. ---> System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it 173.194.79.108:587
at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception)
--- End of inner exception stack trace ---
at System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket6)
at System.Net.PooledStream.Activate(Object owningObject, Boolean async, GeneralAsyncDelegate asyncCallback)
at System.Net.PooledStream.Activate(Object owningObject, GeneralAsyncDelegate asyncCallback)
at System.Net.ConnectionPool.GetConnection(Object owningObject, GeneralAsyncDelegate asyncCallback, Int32 creationTimeout)
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)
--- End of inner exception stack trace ---
at System.Net.Mail.SmtpClient.Send(MailMessage message)
at WindowsFormsApplication2.Form1.button1_Click(Object sender, EventArgs e) in C:\Users\jagan\Desktop\WindowsFormsApplication2\WindowsFormsApplication2\Form1.cs:line 33





that is the error i got//



my code was---


MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");

mail.From = new MailAddress("ait90510@gmail.com");

mail.To.Add("mail.at100@gmail.com");
mail.Subject = "Name of subject";

mail.Body = "mail with attachment";

SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("myuserid", "password");
SmtpServer.Send(mail);




Anybody Please Help...................................................................


my id =mail.amit100@gmail.com

Attached File(s)


Was This Post Helpful? 0
  • +
  • -

#41 olibenu  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 45
  • View blog
  • Posts: 537
  • Joined: 31-December 08

Posted 05 January 2013 - 03:36 AM

this thread is really hot!

note that most mail providers like yahoo & google limit the number of mails you can send to prevent spam
Was This Post Helpful? 0
  • +
  • -

#42 emile2012  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 30-December 12

Posted 16 July 2013 - 07:01 AM

I would replace the code to clear the textboxes witht this:
private void btnClearFields(object sender, EventArgs e)
        {
            clearFields(new TextBox[] { sendServer, serverPort, sendTo, sendFrom, 
                userBox, passBox, subjectBox, contentBox });
        }

        private void clearFields(TextBox[] txt)
        {
            foreach (TextBox field in txt)
            {
                field.Clear();
            }
        }
    }


Was This Post Helpful? 0
  • +
  • -

#43 TechMeOutTV  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 11-October 13

Posted 11 October 2013 - 06:50 PM

thanks for the share. works great for me but it looks like when you use this with gmail, they just make the FROM field whatever credential you used to login with. i referenced this tutorial and have it working here:

http://blog.techmeou...on-with-smtp/#!

thanks again!
Was This Post Helpful? 0
  • +
  • -

#44 prozen111  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 18-July 14

Posted 18 July 2014 - 11:52 AM

I am getting an error when I press send.

System.Net.Mail.SmtpException: Failure sending mail. ---> System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it 65.55.176.126:587
at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception)
--- End of inner exception stack trace ---
at System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket6)
at System.Net.PooledStream.Activate(Object owningObject, Boolean async, GeneralAsyncDelegate asyncCallback)
at System.Net.PooledStream.Activate(Object owningObject, GeneralAsyncDelegate asyncCallback)
at System.Net.ConnectionPool.GetConnection(Object owningObject, GeneralAsyncDelegate asyncCallback, Int32 creationTimeout)
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)
--- End of inner exception stack trace ---
at System.Net.Mail.SmtpClient.Send(MailMessage message)
at WindowsFormsApplication2.Form1.button1_Click(Object sender, EventArgs e) in C:\Users\jagan\Desktop\WindowsFormsApplication2\WindowsFormsApplication2\Form1.cs:line 67


 client.Port = 587;
                client.Credentials = new System.Net.NetworkCredential(txtFrom.Text + "@" + txtSMTP.Text, txtPassword.Text);
                client.EnableSsl = true;
                client.Send(mail);
                MessageBox.Show("Mail was sent successfully!");


Was This Post Helpful? 0
  • +
  • -

#45 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3223
  • View blog
  • Posts: 10,812
  • Joined: 12-December 12

Posted 18 July 2014 - 12:04 PM

If you search "gmail the target machine actively refused it" you will find quite a number of links. Here's one which suggests trying a different port-number, such as 465, and that you can test the connection with:

telnet smtp.gmail.com 587

Gmail will also restrict the number of emails you can send. (They may even have sent you an email.)

It may even be your anti-virus or firewall blocking it.

This post has been edited by andrewsw: 18 July 2014 - 12:04 PM

Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3