2
   private void SendMail()
    {
        clGast.SelectById(clReservering.Gastid);
        System.Net.Mail.MailMessage mailtje = new System.Net.Mail.MailMessage("norepy@robocamp.nl", clGast.pemail);
        mailtje.Subject = "Bevestiging reservering Robocamp";
        mailtje.Body = "Geachte meneer/mevrouw " + clGast.ptussenvoegsel + " " + clGast.pachternaam + ",\n\n";
        mailtje.Body += "Hierbij de bevestiging van u reservering bij robocamp. Hieronder staan de gegevens nogmaals vermeld : \n\n";
        mailtje.Body += "Van datum: " + clReservering.Datumstart + " \n";
        mailtje.Body += "Tot datum: " + clReservering.Datumeind + " \n";
        mailtje.Body += "Plaats nummer: " + clReservering.Plaatsid + " \n\n";

        SmtpClient client = new SmtpClient();
        client.Port = 25;
        client.DeliveryMethod = SmtpDeliveryMethod.Network;
        client.UseDefaultCredentials = false;
        client.Host = "smtp.google.com";

        client.Send(mailtje); 
    }

why is this not working?

unable to send mail. Smtp failure?

clGast.pemail is an email adress. active and working.

Error is :

SmtpException was unhandeled

Failure sending mail

anyone?

Roboneter
  • 867
  • 2
  • 13
  • 24
  • 4
    "not working" is not a good explanation of the problem. Please show error messages/behavior you see compared to what you expect. – Alexei Levenkov Jan 01 '14 at 21:36
  • Side note: I don't believe Google have an open relay SMTP server... – Alexei Levenkov Jan 01 '14 at 21:37
  • There are a lot of things which can prevent an email message from reaching your inbox, many of which have nothing to do with the code. What is the response from the SMTP server? – David Jan 01 '14 at 21:38
  • SmtpException was unhandeled Failure sending mail – Roboneter Jan 01 '14 at 21:47
  • possible duplicate of [Sending email in .NET through Gmail](http://stackoverflow.com/questions/32260/sending-email-in-net-through-gmail) – Daniel A. White Jan 01 '14 at 21:49
  • Also post the inner exception in its entirety. Type, message, stacktrace. I want to see ex.ToString(). – usr Jan 01 '14 at 22:39

2 Answers2

1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;

/// <summary>
/// Simple Emailer class for sending a simple email.
/// </summary>
public class Emailer
{
    /// <summary>
    /// Takes a users email and item name and generates an email
    /// </summary>
    /// <param name="recipient">Recipients e-mail address</param>
    public static void SendMail(string recipient)
    {
        try
        {
            // Setup mail message
            MailMessage msg = new MailMessage();
            msg.Subject = "Email Subject";
            msg.Body = "Email Body";
            msg.From = new MailAddress("FROM Email Address");
            msg.To.Add(recipient);
            msg.IsBodyHtml = false; // Can be true or false

            // Setup SMTP client and send message
            using (SmtpClient smtpClient = new SmtpClient())
            {
                smtpClient.Host = "smtp.gmail.com";
                smtpClient.EnableSsl = true;
                smtpClient.Port = 587; // Gmail uses port 587
                smtpClient.UseDefaultCredentials = false; // Must be set BEFORE Credentials below...
                smtpClient.Credentials = new NetworkCredential("Gmail username", "Gmail Password");
                smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
                smtpClient.Send(msg);
            }
        }
        catch (SmtpFailedRecipientsException sfrEx)
        {
            // TODO: Handle exception
            // When email could not be delivered to all receipients.
            throw sfrEx;
        }
        catch (SmtpException sEx)
        {
            // TODO: Handle exception
            // When SMTP Client cannot complete Send operation.
            throw sEx;
        }
        catch (Exception ex)
        {
            // TODO: Handle exception
            // Any exception that may occur during the send process.
            throw ex;
        }
    }
}
Nick
  • 4,192
  • 1
  • 19
  • 30
0

You need to set the port to 587 and enable SSL:

Client.EnableSsl = true;

I normally specify a user name and password as well.

Here is a complete listing without infrastructure code:

MailMessage msg = new MailMessage(new MailAddress("test@gmail.com"), new MailAddress("test@gmail.com"));
msg.Subject = "The Subject";
msg.Body = "The Body";
SmtpClient client = new SmtpClient();
client.Host = "smtp.gmail.com"
client.Port = 587;
client.EnableSsl = true;
client.Credentials = new NetworkCredential("userName", "pa55word");
client.Send(msg);
client.Dispose();
field_b
  • 688
  • 5
  • 21