Roboneter
Roboneter

Reputation: 897

C# SMTP mail not working?

   private void SendMail()
    {
        clGast.SelectById(clReservering.Gastid);
        System.Net.Mail.MailMessage mailtje = new System.Net.Mail.MailMessage("[email protected]", 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?

Upvotes: 2

Views: 5188

Answers (2)

field_b
field_b

Reputation: 698

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("[email protected]"), new MailAddress("[email protected]"));
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();

Upvotes: 0

Nick
Nick

Reputation: 4212

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;
        }
    }
}

Upvotes: 1

Related Questions