4 Replies - 815 Views - Last Post: 17 August 2010 - 07:13 AM Rate Topic: -----

#1 ramo1995  Icon User is offline

  • New D.I.C Head

Reputation: 8
  • View blog
  • Posts: 38
  • Joined: 24-July 10

Sending an Email

Posted 16 August 2010 - 06:03 AM

Hi guys. I made a samll program to send an eamil programatically using SMTP. I don't know why, but it just gives me an error (" Faliure sending email.")

My Code:

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


namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public SmtpClient client = new SmtpClient();
        public MailMessage msg = new MailMessage();

        public System.Net.NetworkCredential smtpCreds =
            new System.Net.NetworkCredential("myusername@gmail.com", "mypassword");

        public Form1()
        {
            InitializeComponent();
        }

        public void SendEmail(string sendTo, string sendFrom, string subject, string body)
        {
            try
            {
                //Setup SMTP Host Here
               
                client.Host = "smtp.gmail.com";
                client.Port = 587;
                client.UseDefaultCredentials = false;
                client.Credentials = smtpCreds;
                client.EnableSsl = true;

                //Convert strings to MailAddress
                MailAddress to = new MailAddress(sendTo);
                MailAddress from = new MailAddress(sendFrom);

                //Setup message settings
                msg.Subject = subject;
                msg.Body = body;
                msg.From = from;
                msg.To.Add(to);

                

                //Send email!
                client.Send(msg);
            }
            catch (Exception ex)
            {
                //This is just in case you type something wrong or if it cannot connect to the server
                //It will pop up with the reason why it will not work 
                MessageBox.Show(ex.Message, "ERROR");
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SendEmail("myusername@gmail.com", "myusername@gmail.com", "Hello ", "IT WORKS!");
            
        }
    }
}



Could anyone help?

Thanks!

Is This A Good Question/Topic? 1
  • +

Replies To: Sending an Email

#2 Imdsm  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 103
  • View blog
  • Posts: 362
  • Joined: 21-March 09

Re: Sending an Email

Posted 16 August 2010 - 06:39 AM

Are you sure gmail is 587?
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,436
  • Joined: 23-August 08

Re: Sending an Email

Posted 16 August 2010 - 06:44 AM

Maybe helpful. I was able to connect to the SMTP server via both 465 and 587.

You might want to copy and paste the exact exception you are receiving.
Was This Post Helpful? 1
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5438
  • View blog
  • Posts: 11,667
  • Joined: 02-June 10

Re: Sending an Email

Posted 16 August 2010 - 07:25 AM

"Failure sending email" is not an exception. Its a response after trying to send the email.
The only time I get this is when the server/port/username/password combo isn't right and the message can't be sent.
Was This Post Helpful? 1
  • +
  • -

#5 ramo1995  Icon User is offline

  • New D.I.C Head

Reputation: 8
  • View blog
  • Posts: 38
  • Joined: 24-July 10

Re: Sending an Email

Posted 17 August 2010 - 07:13 AM

View PostJackOfAllTrades, on 16 August 2010 - 05:44 AM, said:

Maybe helpful. I was able to connect to the SMTP server via both 465 and 587.

You might want to copy and paste the exact exception you are receiving.



When I use the 587 or 25 port the message says " Failure sending message").

But when I use the 465 port the message says (" operation timed out").

I don't know why it isn't working with my computer, maybe a network problem. Do you know any other way I can send an email?

Thanks
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1