5

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.

Davis Broda
  • 4,102
  • 5
  • 23
  • 37
Zac Voicheq
  • 93
  • 1
  • 1
  • 7
  • See this : http://www.aspdotnet-suresh.com/2010/12/how-to-send-mail-using-gmail.html – Janki Jul 31 '13 at 06:10
  • 2
    Try adding Port number: `SmtpClient MailClient = new SmtpClient("smtp.gmail.com",587);` – Vishal Suthar Jul 31 '13 at 06:11
  • Have you configured the Smtp setting section inside mail setting section of your app config file. – Vinay Pratap Singh Bhadauria Jul 31 '13 at 06:13
  • 1
    as a side note: wrap your `MailMessage` and `SmtpClient` in an [`using` statement](http://msdn.microsoft.com/en-us/library/yh598w02.aspx) to [dispose](http://msdn.microsoft.com/en-us/library/system.idisposable.aspx) unmanaged resources correctly – rexcfnghk Jul 31 '13 at 06:17

5 Answers5

10

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.

Loetn
  • 3,832
  • 25
  • 41
2

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);
A Coder
  • 3,039
  • 7
  • 58
  • 129
1

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

SmtpClient MailClient = new SmtpClient("smtp.gmail.com",587);
Amit
  • 15,217
  • 8
  • 46
  • 68
1

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());
    }
Vishal Suthar
  • 17,013
  • 3
  • 59
  • 105
0
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();
Slam
  • 8,112
  • 1
  • 36
  • 44