Send a single mail with multiple Ids using c#

Posted by Gopal_nivas under ASP.NET on 8/11/2011 | Points: 10 | Views : 3512 | Status : [Member] | Replies : 5
hi..

i am having a list.

in that i have values like

email orderno
a@gmail.com 1
b@gmail.com 2
a@gmail.com 5
d@gmail.com 8
e@gmail.com 9
d@gmail.com 10


now i want to send the single mail to particular mailid.
i.e
a@gmail.com have two order numbers.

now i want to send a single mail to "a@gmail.com" with that 2 order numbers(1,5).

same as foe the remaining mail id's.

how can i filter that and how to send the multiple order no. with single mail.

need ur suggestions with examples
Regards
gopal.s




Responses

Posted by: Dhirendra on: 8/11/2011 [Member] Starter | Points: 25

Up
0
Down
following might be one way to achieve this..
select emailIds from table which has orders (only those records)
execute this SQL command(procedure) through C#)
loop through this records and execute another comman which will pull orders of that particular email Id
loop through the orders of that email Id and prapare email content
once it is done then send this email.
then goto next email Id to pull the record

Other way using OOPs is create customer Object and set orders array to that customer object and assinged all required properties to customer and order object
return this customer object list back to your SMPT layer whre you are sending emails..

hope this helps..



Gopal_nivas, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Dhirendra on: 8/11/2011 [Member] Starter | Points: 25

Up
0
Down
following are the way to achieve this..
select only emailIds from table which has order exists(only those records)
execute this SQL command(procedure) through C#)
loop through these records and execute another command which will pull orders of that particular email Id
loop through the orders of that email Id and prapare email content
once it is done then send this email.
then goto next email Id to pull the record

Other way using OOPs is create customer Object and set orders array to that customer object and assinged all required properties to customer and order object
return this customer object list back to your SMPT layer whre you are sending emails..

hope this helps..



Gopal_nivas, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Gopal_nivas on: 8/11/2011 [Member] Starter | Points: 25

Up
0
Down
hi dhirendra..

thanks for ur reply..

can you provide some examples for the above scenario..

need ur suggetsions with examples

regards
gopal.s



Gopal_nivas, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Dhirendra on: 8/11/2011 [Member] Starter | Points: 25

Up
0
Down
here is the complete example
I have used Northing Database tables as sample. you just need to write logic to send email using smtp client by praparing email body.( see string build hjs order Id)
C# code..
add this code at start up
ProductDataStore datastore = new ProductDataStore();
List<Customer> list = datastore.SendEmails(); //which will call GetCustomers inside sendemails Method.........

public class ProductDataStore
{

public void SendEmails()
{
List<Customer> list = GetCustomers();
StringBuilder sb = new StringBuilder();
foreach (var Customer in list)
{
sb.Append("Customr Id: = " + Customer.CustId);
foreach (var orderitem in Customer.Orders)
{
sb.Append("Order Id " + orderitem.orderId);
}

//call sent email login using smtp class
//System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
}


}

public List<Customer> GetCustomers()
{
SqlCommand cmd = new SqlCommand();

SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["NwConn"].ConnectionString);
cmd.Connection = conn;
cmd.CommandText = "GetCustomerWithOrder";
conn.Open();
SqlDataReader Custreader = cmd.ExecuteReader();
string strCustId = null;
List<Customer> custlist = new List<Customer>();
Customer cust = null;
while (Custreader.Read())
{
cust = new Customer();
strCustId = Convert.ToString(Custreader["CustomerId"]);
cust.CustId = strCustId;
SqlCommand cmd1 = new SqlCommand();
cmd1.Connection = conn;
cmd1.CommandText = "GetOrdersByCustId";
cmd1.CommandType = CommandType.StoredProcedure;

cmd1.Parameters.AddWithValue("@CustomerID", strCustId);

SqlDataReader Orderreader = cmd1.ExecuteReader();
if (Orderreader.HasRows)
{
List<Order> orderlist = new List<Order>();
while (Orderreader.Read())
{
Order orders = new Order();

orders.orderId = Convert.ToInt32(Orderreader["OrderID"]);
orderlist.Add(orders);
}
cust.Orders = orderlist;
}
custlist.Add(cust);
Orderreader.Close();
}
Custreader.Close();
conn.Close();
return custlist;

}
}

public class Order
{
public int orderId { get; set; }
}
public class Customer
{
public string CustId { get; set; }
public string CustName { get; set; }
public string EmailId { get; set; }
public List<Order> Orders { get; set; }
}


SQl procedures

create proc dbo.GetCustomerWithOrder
as

begin
select distinct(cust.CustomerId ) from customers cust
inner join orders ord
on ord.CustomerId =cust.CustomerId
end
--exec GetOrdersByCustId 'ALFKI'
Create proc dbo.GetOrdersByCustId
@CustomerID varchar(10)
as

begin
select * from orders
where CustomerId =@CustomerID
end
Go


also..make sure you use this type of connection string


<connectionStrings >

<add name="NwConn" connectionString="Data Source=DHIRENDRA-1 ;Initial Catalog=NorthWind; User Id=usder; Password=;MultipleActiveResultSets=True;" providerName="System.Data.SqlClient"/>
</connectionStrings>

Gopal_nivas, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Dhirendra on: 8/12/2011 [Member] Starter | Points: 25

Up
0
Down
Hi Gopal
Hope, the above example helps to you...

please mark as answer if the above code example helped to you..........

Gopal_nivas, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response