PW2
PW2

Reputation: 791

correct code for sending an email, asp.net

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);

                }
            }
}

Upvotes: 2

Views: 2689

Answers (4)

sshow
sshow

Reputation: 9084

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>

Upvotes: 1

Clicktricity
Clicktricity

Reputation: 4209

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>

Upvotes: 4

Klaus Byskov Pedersen
Klaus Byskov Pedersen

Reputation: 120917

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.

Upvotes: 0

Darren Lewis
Darren Lewis

Reputation: 8488

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

Upvotes: 0

Related Questions