2

I am trying to send an email through and C# application (Console). Actually I would like it to send an email to any type of email but for the time being if I can get it to send to a gmail account I would be happy enough.

I just want to be able to get this to send to a gmail account for the time being?

Whole Program:

namespace EmailAddress
{
    class Program
    {
        static void Main(string[] args)
        {
            Program test = new Program();
            test.email_send();
        }

        public void email_send()
        {
            MailMessage mail = new MailMessage();
            SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
            mail.From = new MailAddress("hg@gmail.com");
            mail.To.Add("hg@gmail.com");
            mail.Subject = "Test Mail - 1";
            mail.Body = "Your attachment is accessible on your computer!";

            System.Net.Mail.Attachment attachment;
            attachment = new System.Net.Mail.Attachment("g:/example1.txt");
            mail.Attachments.Add(attachment);

            SmtpServer.Port = 587;
            SmtpServer.Credentials = new System.Net.NetworkCredential("your mail@gmail.com", "your password");
            SmtpServer.EnableSsl = true;

            SmtpServer.Send(mail);
        }
    }
}

New code: Doesn't hang but will not send the message to the inbox

static void Main(string[] args)
        {
            Program test = new Program();
            //test.email_send();
            test.CreateTestMessage4();
        }

        public void email_send()
        {
            MailMessage mail = new MailMessage();
            SmtpClient smtp = new SmtpClient("smtp.gmail.com");
            mail.From = new MailAddress("g@gmail.com");
            mail.To.Add("g@gmail.com");
            mail.Subject = "Test Mail - 1";
            mail.Body = "Your attachment is accessible on your computer!";

            System.Net.Mail.Attachment attachment;

            attachment = new System.Net.Mail.Attachment("g:\\example1.txt");
            mail.Attachments.Add(attachment);//list of attachements

            smtp.Port = 587;//google standard -- most of the time wouldn't not set
            smtp.EnableSsl = true;
            smtp.UseDefaultCredentials = true;
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtp.Credentials = new System.Net.NetworkCredential("*","*");

            smtp.Send(mail);

            Console.WriteLine("-- Sending Email --");
            Console.ReadLine();
        }

Can someone try this code and see if it works. For some reason this isn't working so I would like to have someone's fresh perspective of this. I just call this in the main method for an instance of the class.

 public void email_send()
        {
            MailMessage mail = new MailMessage();
            SmtpClient smtp = new SmtpClient("smtp.gmail.com");
            mail.From = new MailAddress("your email address");
            mail.To.Add("your email address");
            mail.Subject = "Test Mail - 1";
            mail.Body = "Your attachment is accessible on your computer!";

            System.Net.Mail.Attachment attachment;

            attachment = new System.Net.Mail.Attachment("g:\\example1.txt");
            mail.Attachments.Add(attachment);//list of attachements

            //smtp.Port = 587;//google standard -- most of the time wouldn't not set
            //smtp.EnableSsl = true;
            //smtp.UseDefaultCredentials = true;
            //smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            //smtp.Credentials = new System.Net.NetworkCredential("your address", 
                                                                 "your password");

            smtp.Port = 587;
            smtp.Host = "smtp.gmail.com";
            smtp.UseDefaultCredentials = false;
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtp.EnableSsl = true;
            smtp.Credentials = new System.Net.NetworkCredential("*", "*"); smtp.Send(mail);

            Console.WriteLine("-- Sending Email --");
            Console.ReadLine();
        }
Ondrej Janacek
  • 12,486
  • 14
  • 59
  • 93
Doug Hauf
  • 3,025
  • 8
  • 46
  • 70
  • 2
    Sending _to an account_ and sending _via an account_ are completely different. Your question appears to be mixing these two things. Can you clarify which one you're having trouble with? – Simon Whitehead Dec 19 '13 at 01:48
  • 1
    @Doug Hauf Please keep one question per post. – kaptan Dec 19 '13 at 01:50
  • Regarding your new code. I never see you call the `email_send()` method. Try calling `test.email_send();` right after `Program test = new Program();` – mason Dec 19 '13 at 02:35
  • 1
    Set `UseDefaultCredentials` to false and specify a username and password on NetworkCredential – BrunoLM Dec 19 '13 at 02:37
  • possible duplicate of [Sending email in .NET through Gmail](http://stackoverflow.com/questions/32260/sending-email-in-net-through-gmail) – avs099 Dec 19 '13 at 02:42
  • After you do the `smtp.Credentials = ..` line you still need to call `smtp.Send(mail)` put it immediately above your `Console.WriteLine("...` call – NotMe Dec 19 '13 at 04:05
  • Can you try my above code and see if you can send an email? The program compiles and does not throw and error so I am not really certain what is causing all of the errors. Doug Hauf – Doug Hauf Dec 19 '13 at 22:17

2 Answers2

2

Your code is close:

MailMessage mail = new MailMessage();
using (SmtpClient smtp = new SmtpClient("smtp.gmail.com")) {
    mail.From = new MailAddress("haufdoug@gmail.com");
    mail.To.Add("haufdoug@gmail.com");
    mail.Subject = "Test Mail - 1";
    mail.Body = "Your attachment is accessible on your computer!";

    System.Net.Mail.Attachment attachment;

    attachment = new System.Net.Mail.Attachment("g:\\example1.txt");
    mail.Attachments.Add(attachment);

    smtp.Port = 587;
    smtp.EnableSsl = true;
    smtp.UseDefaultCredentials = false;
    smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
    smtp.Credentials = new System.Net.NetworkCredential("your mail@gmail.com", "your password");

    smtp.Send(mail);
}

Now, regarding some of your other questions.

Port 587 is used by Google only. Typically the mail server you connect to will tell you what port to use for SMTP mail; Google said there's is 587.

For sending through other servers you typically won't set the port number.

side notes:

  1. SmtpClient implements the IDisposable interface. So it should be wrapped in a using clause to ensure it is properly disposed of when complete. Unless you are using the .SendAsync() method.
  2. I changed the variable name from SmtpServer to smtp for two reasons. First, naming convention. Variables inside a method are recommended to be camel case (forExampleThis). Second, SmtpServer is a misnomer as the object is an SmtpClient. Those are very different things and it's just bad practice to misname things.
NotMe
  • 87,343
  • 27
  • 171
  • 245
  • This is the error I just received. Doesn't there have to be a password that is set with GMail. The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at – Doug Hauf Dec 19 '13 at 01:59
  • @DougHauf: Well.. yes. That's what the `smtp.Credentials = ...` line is for. Replace `"your mail@gmail.com"` with `"haufdoug@gmail.com"` and `"your password"` with your actual gmail password. It will then use your credentials. – NotMe Dec 19 '13 at 02:01
  • @DougHauf NEVER put your password into a public website like that. Now I could go log into your gmail account if I wish. And I could log into your other sites because you likely used the same password. You need to IMMEDIATELY change your gmail password and your password for any sites where you used the same password. – mason Dec 19 '13 at 02:21
  • Thanks just changed it, it doesn't make it to my in box. I am going to post the code in my original question with out the password. – Doug Hauf Dec 19 '13 at 02:29
  • Odd. System.Net.Mail.SmtpClient does not implement Disposable in the 3.5 framework. – Nyerguds Aug 11 '14 at 09:19
2

Can you try this, it's working for me:

SmtpClient SmtpServer = new SmtpClient();
SmtpServer.Port = 587;
SmtpServer.Host = smtp.gmail.com;
SmtpServer.UseDefaultCredentials = false;
SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;
SmtpServer.EnableSsl = true; 
SmtpServer.Credentials = new System.Net.NetworkCredential("your mail@gmail.com", "your password");
vahnevileyes
  • 415
  • 3
  • 6