Question

I am experiencing some problems sending emails with MailMessage. I have two email accounts, (account1@gmail.com, and account2@gmail.com) 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("account2@gmail.com", "Account2");
                emailMessage.To.Add(new MailAddress("account1@gmail.com", "Account1"));
                emailMessage.Subject = "SUBJECT";
                emailMessage.Body = "BODY";
                emailMessage.Priority = MailPriority.Normal;
                SmtpClient MailClient = new SmtpClient("smtp.gmail.com");
                MailClient.Credentials = new System.Net.NetworkCredential("account2@gmail.com", "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.

Was it helpful?

Solution

Try this:

using (MailMessage emailMessage = new MailMessage())
{
    emailMessage.From = new MailAddress("account2@gmail.com", "Account2");
    emailMessage.To.Add(new MailAddress("account1@gmail.com", "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("account2@gmail.com", "password");
        MailClient.Send(emailMessage);
    }                               
} 

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

OTHER TIPS

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("account2@gmail.com", "Account2");
        emailMessage.To.Add(new MailAddress("account1@gmail.com", "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("account2@gmail.com", "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();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top