2 Replies - 5874 Views - Last Post: 14 March 2012 - 05:11 AM Rate Topic: -----

#1 [email protected]   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 13-March 12

sending mail with attachment file to multiple user in asp.net with C#

Posted 14 March 2012 - 01:09 AM

I want send mail which i have multiple register users after that i want send mail to all register users.all user name fetch from database but not successfully send mail with attachment file.
when i am sending mail to multiple users with attachment file then occur a error.

Multiple user names are fetching from my sql database.
so Plz help me.

My code is above
//////////////////////////////  

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Net;
using System.Net.Mail;

public partial class CookeryAdmin_sendNews_RegUser : System.Web.UI.Page
{
    DataService ds = new DataService();
    string Uname;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Login"] == null)
        {
            Response.Redirect("../Default.aspx");
        }
        else
        {
            string strLogin = "select * from User_Details where email = '" + Session["Login"].ToString() + "' and isAdmin = 1";
            SqlDataReader sdrLogin = ds.data_Select(strLogin);
            while (sdrLogin.Read())
            {
                Uname = sdrLogin.GetString(sdrLogin.GetOrdinal("fName")) + " " + sdrLogin.GetString(sdrLogin.GetOrdinal("lName"));
            }

            lblMsg.Text = "Welcome, " + Uname;
            sdrLogin.Close();

        }
        if (!Page.IsPostBack)
        {

        }

    }
    protected void btnSendNews_Click(object sender, EventArgs e)
    {
        try
        {
            fetchEmailId();
        }
        catch (Exception ex)
        {
            TD1.InnerHtml = "";
            TD1.InnerHtml = "ERROR: " + ex.Message.ToString();

        }   
    }
    private void fetchEmailId()
    {
        string strUser = "";
        string sqlEmail = "select * from user_Details where isAdmin = 0";
        DataTable dtEmailId = ds.data_SelectByAdap(sqlEmail);
        int count = dtEmailId.Rows.Count;
        if (count > 0)
        {
            for (int i = 0; i < count; i++)
            {
                strUser += dtEmailId.Rows[i]["email"].ToString() + ",";
                 
            }
            SendNewsletter(strUser);
            TD1.InnerHtml = "";
            TD1.InnerHtml = "Newsletter successfully sent to Registered Users";
            text_clear();
        }
        else
        {
            TD1.InnerHtml = "";
            TD1.InnerHtml = "No Registered user are available";
                        
        }
    }
    private void text_clear()
    {
        txtSubject.Text = "";
        txtBody.Text = "";
    }
    private void SendNewsletter(string emailId)
    {
        System.Web.Mail.MailMessage message = new System.Web.Mail.MailMessage();
        message.To = emailId;
        message.From = "[email protected]";
        message.Subject = txtSubject.Text.ToString();
        message.BodyFormat = System.Web.Mail.MailFormat.Text;
        message.Body = txtBody.Text.ToString();
        if (msgUpload.HasFile)
        {
            string strFileName = msgUpload.FileName;
            msgUpload.PostedFile.SaveAs(Server.MapPath(strFileName));
            System.Web.Mail.MailAttachment attach = new System.Web.Mail.MailAttachment(Server.MapPath(strFileName));
            message.Attachments.Add(attach);
        }
        System.Web.Mail.SmtpMail.Send(message);
        Response.Flush();
    }
}

//////////////////////////////////


Regards,
Kuldeep Pathak

MOD EDIT: Added code tags. When posting code...USE CODE TAGS!!!

:code:

This post has been edited by JackOfAllTrades: 14 March 2012 - 02:48 AM


Is This A Good Question/Topic? 0
  • +

Replies To: sending mail with attachment file to multiple user in asp.net with C#

#2 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: sending mail with attachment file to multiple user in asp.net with C#

Posted 14 March 2012 - 02:48 AM

Moved to ASP.NET
Was This Post Helpful? 0
  • +
  • -

#3 Nakor   User is offline

  • Professional Lurker
  • member icon

Reputation: 448
  • View blog
  • Posts: 1,504
  • Joined: 28-April 09

Re: sending mail with attachment file to multiple user in asp.net with C#

Posted 14 March 2012 - 05:11 AM

First, you should know that System.Web.Mail has been deprecated in favor of System.Net.Mail. That being said, here's a link to an example of how to use System.Net.Mail. It includes an example of attaching attachments and having multiple recipients.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1