tags:

views:

54

answers:

4

Hi, I have a form that allows a user to send an email to everyone on a mailing list (linq table). I'm having trouble with the correct code and syntax for linking to the smtp server.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Profile;
using System.Web.Security;
using System.Web.Mail;
using System.Configuration;
using System.Web.Configuration;
using System.Net.Configuration;
using System.Net.Mail;



public partial class MassEmail : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {

        mailingListClassDataContext Class = new mailingListClassDataContext();
        var emaillist = from emails in Class.mailinglistMembers select emails.email;

        foreach (var subcriber in emaillist)
        {


                MailMessage objMail = new MailMessage();
                objMail.From = "[email protected]";

                objMail.To = subcriber;

                objMail.BodyFormat = MailFormat.Html ;


                //The subject of the message 
                objMail.Subject = "test email that i hope works" ;

                //he message text 
                objMail.Body = Editor1.Content;

                //need help in this area
                SmtpClient client = new SmtpClient();

                SmtpClient.Send(objMail);

                }
            }
}
A: 

You can pass the SMTP server IP or name in the constructor to SmtpClient or set it explicity through the Host property.

Daz Lewis
A: 

You probably want to set the Host (and possibly the Credentials) property on the SmptClient. The server (host) defaults to localhost. Also consider creating the client instance outside of the loop.

klausbyskov
+3  A: 

The best solution is to put the smtp server details in your web.config

    <system.net>
        <mailSettings>
            <smtp>
                <network
                   host="smtp.emailhost.com"
                   port="25"
                   userName="username"
                   password="password" />
            </smtp>
        </mailSettings>
    </system.net>
  <system.web>
Clicktricity
Or better; in `machine.config`
sshow
A: 
using (var db = new mailingListClassDataContext())
{
    var client = new System.Net.Mail.SmtpClient();

    var recipients = from e in db.mailinglistMembers
                     select e.email;

    foreach (string recipient in recipients)
    {
        var message = new System.Net.Mail.MailMessage("[email protected]", recipient);
        message.Subject = "Hello World!";
        message.Body = "<h1>Foo bar</h1>";
        message.IsBodyHtml = true;
        client.Send(message);
    }
}

Try setting up configuration in your web.config or machine.config. Make sure you've specified the correct address and port of the SMTP server.

<configuration>
  <system.net>
    <mailSettings>
      <smtp deliveryMethod="network" from="[email protected]">
        <network
          host="localhost"
          port="25"
          defaultCredentials="true"
        />
      </smtp>
    </mailSettings>
  </system.net>
</configuration>
sshow
thanks. worked great.
PW2
@PW2 then you should mark my answer as the accepted answer.
sshow
thanks! maybe you can help me out with my other pending question, titled: "delete record onClick, asp.net"
PW2