Using Mailmessage to Send Emails in C#

I am experiencing some problems sending emails with MailMessage. I have two email accounts, ([email protected], and [email protected]) and I would like account2 to send an email to account one at a button click event.

This is what I have but it's not working. I'm getting and exception saying it's forbidden.

            try
            {
                //do submit
                MailMessage emailMessage = new MailMessage();
                emailMessage.From = new MailAddress("[email protected]", "Account2");
                emailMessage.To.Add(new MailAddress("[email protected]", "Account1"));
                emailMessage.Subject = "SUBJECT";
                emailMessage.Body = "BODY";
                emailMessage.Priority = MailPriority.Normal;
                SmtpClient MailClient = new SmtpClient("smtp.gmail.com");
                MailClient.Credentials = new System.Net.NetworkCredential("[email protected]", "password");
                MailClient.Send(emailMessage);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

I have a feeling it's a problem with the Smtp but I have no clue.

4

5 Answers

Try this:

using (MailMessage emailMessage = new MailMessage())
{
    emailMessage.From = new MailAddress("[email protected]", "Account2");
    emailMessage.To.Add(new MailAddress("[email protected]", "Account1"));
    emailMessage.Subject = "SUBJECT";
    emailMessage.Body = "BODY";
    emailMessage.Priority = MailPriority.Normal;
    using (SmtpClient MailClient = new SmtpClient("smtp.gmail.com", 587))
    {
        MailClient.EnableSsl = true;
        MailClient.Credentials = new System.Net.NetworkCredential("[email protected]", "password");
        MailClient.Send(emailMessage);
    }                               
} 

I added the port to the smtp client and enabled SSL. If port 587 doesn't work, try port 465.

Try:

MailMessage Msg = new MailMessage();

Msg.From = new MailAddress(txtUsername.Text);

Msg.To.Add(txtTo.Text);

Msg.Subject = txtSubject.Text;

Msg.Body = txtBody.Text;

SmtpClient smtp = new SmtpClient();

smtp.Host = "smtp.gmail.com";

smtp.Port = 587;

smtp.Credentials=new System.Net.NetworkCredential(txtUsername.Text,txtpwd.Text);

smtp.EnableSsl = true;

smtp.Send(Msg);

You have not define port number. it should be 587. and use enableSsl=true as below:

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

Try adding a Port number 587 and enabling SSL on as Gmail uses “strict” SSL security. This means that we’ll always enforce that your other provider’s remote server has a valid SSL certificate.:

    try
    {
        //do submit
        MailMessage emailMessage = new MailMessage();
        emailMessage.From = new MailAddress("[email protected]", "Account2");
        emailMessage.To.Add(new MailAddress("[email protected]", "Account1"));
        emailMessage.Subject = "SUBJECT";
        emailMessage.Body = "BODY";
        emailMessage.Priority = MailPriority.Normal;
        SmtpClient MailClient = new SmtpClient("smtp.gmail.com", 587);
        MailClient.Credentials = new System.Net.NetworkCredential("[email protected]", "password");
        MailClient.EnableSsl = true;
        MailClient.Send(emailMessage);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
MailMessage objMail = new MailMessage();
SmtpClient objSMTP = new SmtpClient("from.google.uk");
MailAddress objTo = new MailAddress("e-mail", "name");

string sql = "Select Naam, Mail from tblMail";

OleDbCommand cmdMail = new OleDbCommand(sql, cnnConnectie);

OleDbDataReader dtrMail = default(OleDbDataReader);

cnnConnectie.Open();
dtrMail = cmdMail.ExecuteReader();

while (dtrMail.Read())
{
    objMail.To.Add(dtrMail["Mail"].ToString());

    objMail.From = objTo;
    objMail.Body = "test";
    objMail.Subject = dtrMail["name"].ToString();
    objSMTP.Send(objMail);
}
cnnConnectie.Close();
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

James H. Sterling

James H. Sterling

Environmental Science & Climate Journalist

James Sterling reports on renewable energy developments, climate policy, ecological conservation, and green tech innovations around the globe.

Share this article
Twitter Facebook Pinterest