Using MailMessage to send emails in C#
Asked Answered
A

6

6

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.

Arbutus answered 31/7, 2013 at 6:5 Comment(4)
See this : aspdotnet-suresh.com/2010/12/how-to-send-mail-using-gmail.htmlHopehopeful
Try adding Port number: SmtpClient MailClient = new SmtpClient("smtp.gmail.com",587);Wretched
Have you configured the Smtp setting section inside mail setting section of your app config file.Cyrstalcyrus
as a side note: wrap your MailMessage and SmtpClient in an using statement to dispose unmanaged resources correctlyWindowshop
I
11

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.

Impost answered 31/7, 2013 at 6:10 Comment(0)
P
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);
Prepared answered 31/7, 2013 at 6:13 Comment(0)
S
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);
Scrambler answered 31/7, 2013 at 6:12 Comment(0)
W
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("[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());
    }
Wretched answered 31/7, 2013 at 6:13 Comment(0)
E
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();
Eponymous answered 21/6, 2017 at 13:51 Comment(1)
Thanks for your reply. Can you please add clarification for your answer?Maidenly
T
0
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);
    }                               
} 

imap from google must be activated

https://help.accredible.com/s/article/smtp-setup-in-gmail-inbox?language=en_US

Tabina answered 27/8 at 7:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.