none
C# Send E-Mail RRS feed

  • Question

  • Hey,

    I have a Problem i made a Windows Application which sends Emails if u press the Button, but if i press it nothing happens. Here is the Code:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Net.Mail;

    namespace emailsender
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private void button1_Click(object sender, EventArgs e)
            {
                MailMessage mail = new MailMessage();
                mail.From = new MailAddress("robin.harz26@gmail.com");
                mail.To.Add("robin.harz26@gmail.com");
                mail.Subject = "Test";
                mail.Body = "Test";

                SmtpClient client = new SmtpClient("smtp.gmail.com", 465);
                try
                {
                    client.Credentials = new System.Net.NetworkCredential("robin.harz26@gmail.com", "password");

                    client.EnableSsl = true;

                    client.Send(mail);

                    Console.WriteLine("Email send");

                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error\n\n{0}", ex.Message);
                }

            }

            private void Form1_Load(object sender, EventArgs e)
            {

            }
        }
    }

    Friday, September 25, 2015 7:34 PM

Answers

  • Hi,

    Please use 587 instead.I've tested  587 with your code, it works fine on my side.

    SmtpClient client = new SmtpClient("smtp.gmail.com",587);

    Also note that with Gmail, you can send and receive messages up to 25 megabytes (MB) in size. Please try configuring your SMTP server on port 465 (with SSL) and port 587 (with TLS).

    Have a nice day!

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, September 28, 2015 5:25 AM

All replies