Click here to Skip to main content
15,891,847 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Having a little trouble with my Newsletter...

Let me explain: I have a sql table with my emails and nothing else and a sql table where i store the mails after they have been sendt, somehow the mails wont sent, but it will save the mails.

it is all set up in a formview.

C#
protected void btnSendNewsLetter_Click(object sender, EventArgs e)
    {
        TextBox subject = (TextBox)fvNewsletter.Row.FindControl("txtSubject");
        TextBox body = (TextBox)fvNewsletter.Row.FindControl("txtBody");

        string connection = WebConfigurationManager.ConnectionStrings["GreenCollaborationConnectionString"].ConnectionString;

        SqlConnection con = new SqlConnection(connection);

        con.Open();

        SqlCommand comm = new SqlCommand("Select Email from MailingList", con);
        SqlDataAdapter da1 = new SqlDataAdapter(comm);
        GreenDataSet ds1 = new GreenDataSet();
        da1.Fill(ds1);
        if(ds1.Tables[0].Rows.Count > 0)
        {
            for (int i = 0; i <= ds1.Tables[0].Rows.Count - 1; i++)
            {
                string email = ds1.Tables[0].Rows[i].ItemArray[0].ToString();
                System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
                mail.From = new MailAddress("noreply@GreenCollaboration.com", "GreenCollaboration");
                mail.To.Add(email);
                mail.Subject = subject.Text;
                mail.SubjectEncoding = System.Text.Encoding.UTF8;
                mail.BodyEncoding = System.Text.Encoding.UTF8;
                mail.IsBodyHtml = true;
                mail.Body = body.Text;
            }
        }
    }


i hope some1 can help me :)
Posted
Comments
Richard C Bishop 8-Nov-12 11:29am    
You never send the message.

Check this: http://social.msdn.microsoft.com/forums/en/netfxnetcom/thread/a75533eb-131b-4ff3-a3b2-b6df87c25cc8/
terostar 8-Nov-12 11:31am    
Okay, then how do i do that??
Richard C Bishop 8-Nov-12 11:38am    
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("yoursmtphost"); smtp.Send(mail);

You're missing the bit that does the actual sending. Something like:

C#
using (SmtpClient smtp = new SmtpClient())
{
    smtp.Host = "SMTPServer";
    smtp.Send(mail);
}


You'll need to later that to suit but it'll get you started.

By the way, you're not really creating th emails very efficiently. It is better to have a method that encapsulates the mailing process and takes, for instance, a collection of To addresses rather than one at a time.
 
Share this answer
 
You need to use the SmtpClient class[^] to send the message(s).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900