...

View Full Version : C#.NET Email



sean3838
10-03-2012, 01:52 AM
I am trying to send as an html page an email on Page_Load after all the dynamic controls are rendered. It works and I get sent the email... but its hundreds of them. It's somehow looping through the sendEmail() function and I don't know why. If I add a button to the page and send the email in its on_click event it works and only sends 1 email. I'm not sure what the problem is?

Here is the code I'm just going to copy and paste the whole thing in:



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.IO;

namespace mceTickets
{
public partial class design : System.Web.UI.Page
{

public String name;
public String email;
public Int32 price;
public Int32 numTickets;
public String usertocredit;

public string bannerURL;
public string ticketName;
public string eventName;
public string venueName;
public string venueAddress;
public string venueAddress2;
public string city;
public string startDay;
public string startMonth;
public string startYear;
public string startMinute;
public string startHour;

protected void Page_Load(object sender, EventArgs e)
{

name = Request.QueryString["Name"];
email = Request.QueryString["Email"];
price = Convert.ToInt32(Request.QueryString["p1"]);
numTickets = Convert.ToInt32(Request.QueryString["num1"]);
usertocredit = Request.QueryString["User"];

string promoterName = "";
int index = usertocredit.IndexOf("Event");
if (index > 0)
{ promoterName = usertocredit.Substring(0, index); }

bannerURL = "http://www.mcetickets.com/images/ticket_banner.jpg";

ReadXmlFile(Server.MapPath("~/Promoters/NewEvent/" + usertocredit));

readFirstTicket();

sendEmail()
}

private void readFirstTicket()
{
for (int i = 0; i < numTickets; i++)
{
Guid g = Guid.NewGuid();
string random = g.ToString();

Image image1 = new Image();
image1.ID = "Image1";
image1.Height = 200;
image1.Width = 37;
image1.ImageUrl = bannerURL;

Image image2 = new Image();
image2.ID = "Image2";
image2.ImageUrl = "http://www.mcetickets.com/images/ticket_logo.jpg";

Image barCode = new Image();
barCode.ID = "myBarCode";
barCode.ImageUrl = "http://barcode.tec-it.com/barcode.ashx?code=QRCode&modulewidth=fit&data=" + random.Substring(0, 10) + "&dpi=96&imagetype=gif&rotation=0&color=&bgcolor=&fontcolor=&quiet=0&qunit=mm&eclevel=";

Label label13 = new Label();
label13.ID = "Label13";
label13.Text = "Ticket Name:";

Label label10 = new Label();
label10.ID = "Label10";
label10.Text = ticketName;

Label label7 = new Label();
label7.ID = "Label7";
label7.Text = "Price:";

Label label9 = new Label();
label9.ID = "Label9";
label9.Text = "" + price.ToString();

Label label14 = new Label();
label14.ID = "Label14";
label14.Text = "Event Name:";

Label label15 = new Label();
label15.ID = "Label15";
label15.Text = "Event Date:";

Label label4 = new Label();
label4.ID = "Label4";
label4.Text = eventName;

Label label5 = new Label();
label5.ID = "Label5";
label5.Text = startMonth + " " + startDay + ", " + startYear + " at " + startHour + ":" + startMinute;

Label label1 = new Label();
label1.ID = "Label1";
label1.Text = venueName;

Label label2 = new Label();
label2.ID = "Label2";
label2.Text = venueAddress;

Label label12 = new Label();
label12.ID = "Label12";
label12.Text = venueAddress2;

Label label3 = new Label();
label3.ID = "Label3";
label3.Text = city;

Label label16 = new Label();
label16.ID = "Label16";
label16.Text = "For: ";

Label label11 = new Label();
label11.ID = "Label11";
label11.Text = name;

Label label6 = new Label();
label6.ID = "Label6";
label6.Text = "www.mcetickets.com";

PlaceHolder1.Controls.Add(new LiteralControl("<table class='style1'><tr>"));
PlaceHolder1.Controls.Add(new LiteralControl("<td class='style2' valign='top' rowspan='3'>"));
PlaceHolder1.Controls.Add(image1);
PlaceHolder1.Controls.Add(new LiteralControl("</td><td class='style3'>"));
PlaceHolder1.Controls.Add(label13);
PlaceHolder1.Controls.Add(new LiteralControl("&nbsp;"));
PlaceHolder1.Controls.Add(label10);
PlaceHolder1.Controls.Add(new LiteralControl("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"));
PlaceHolder1.Controls.Add(label7);
PlaceHolder1.Controls.Add(new LiteralControl("&nbsp;"));
PlaceHolder1.Controls.Add(label9);
PlaceHolder1.Controls.Add(new LiteralControl("<br />"));
PlaceHolder1.Controls.Add(label14);
PlaceHolder1.Controls.Add(new LiteralControl("&nbsp;"));
PlaceHolder1.Controls.Add(label4);
PlaceHolder1.Controls.Add(new LiteralControl("<br />"));
PlaceHolder1.Controls.Add(label15);
PlaceHolder1.Controls.Add(new LiteralControl("&nbsp;"));
PlaceHolder1.Controls.Add(label5);
PlaceHolder1.Controls.Add(new LiteralControl("<br />"));
PlaceHolder1.Controls.Add(new LiteralControl("</td><td rowspan='3' class='style4' >"));
PlaceHolder1.Controls.Add(image2);
PlaceHolder1.Controls.Add(new LiteralControl("</td></tr><tr><td class='style3'>"));
PlaceHolder1.Controls.Add(barCode);
PlaceHolder1.Controls.Add(new LiteralControl("</td></tr><tr><td class='style3'>"));
PlaceHolder1.Controls.Add(label1);
PlaceHolder1.Controls.Add(label2);
PlaceHolder1.Controls.Add(label12);
PlaceHolder1.Controls.Add(label3);
PlaceHolder1.Controls.Add(new LiteralControl("<br />"));
PlaceHolder1.Controls.Add(label16);
PlaceHolder1.Controls.Add(label11);
PlaceHolder1.Controls.Add(new LiteralControl("&nbsp;- "));
PlaceHolder1.Controls.Add(label6);
PlaceHolder1.Controls.Add(new LiteralControl("</td></tr></table>"));
PlaceHolder1.Controls.Add(new LiteralControl("<br /><br />"));
}
}

private void ReadXmlFile(string fileName)
{

System.Xml.XmlTextReader ab = new System.Xml.XmlTextReader(fileName);

while (ab.Read())
{

while (ab.ReadToFollowing("TicketName"))
{
ticketName = ab.ReadElementContentAsString();
}
}
ab.Close();

System.Xml.XmlTextReader tname2 = new System.Xml.XmlTextReader(fileName);

while (tname2.Read())
{

while (tname2.ReadToFollowing("TicketName2"))
{
ticketName2 = tname2.ReadElementContentAsString();
}
}
tname2.Close();

System.Xml.XmlTextReader tname3 = new System.Xml.XmlTextReader(fileName);

while (tname3.Read())
{

while (tname3.ReadToFollowing("TicketName3"))
{
ticketName3 = tname3.ReadElementContentAsString();
}
}
tname3.Close();

System.Xml.XmlTextReader xReader = new System.Xml.XmlTextReader(fileName);

while (xReader.Read())
{

while (xReader.ReadToFollowing("EventName"))
{
eventName = xReader.ReadElementContentAsString();
}
}
xReader.Close();

System.Xml.XmlTextReader a = new System.Xml.XmlTextReader(fileName);

while (a.Read())
{

while (a.ReadToFollowing("VenueName"))
{
venueName = a.ReadElementContentAsString() + ", ";
}
}
a.Close();

System.Xml.XmlTextReader b = new System.Xml.XmlTextReader(fileName);

while (b.Read())
{

while (b.ReadToFollowing("VenueAddress"))
{
venueAddress = b.ReadElementContentAsString() + ", ";
}
}
b.Close();

System.Xml.XmlTextReader c = new System.Xml.XmlTextReader(fileName);

while (c.Read())
{

while (c.ReadToFollowing("VenueAddress2"))
{
string content = c.ReadElementContentAsString();
if (content != "")
{
venueAddress2 = content;
}
}
}
c.Close();

System.Xml.XmlTextReader d = new System.Xml.XmlTextReader(fileName);

while (d.Read())
{

while (d.ReadToFollowing("VenueTownCity"))
{
city = d.ReadElementContentAsString();
}
}
d.Close();

System.Xml.XmlTextReader u = new System.Xml.XmlTextReader(fileName);

while (u.Read())
{

while (u.ReadToFollowing("StartDay"))
{
startDay = u.ReadElementContentAsString();
}
}
u.Close();

System.Xml.XmlTextReader ua = new System.Xml.XmlTextReader(fileName);

while (ua.Read())
{

while (ua.ReadToFollowing("StartMonth"))
{
startMonth = ua.ReadElementContentAsString();
}
}
ua.Close();

System.Xml.XmlTextReader us = new System.Xml.XmlTextReader(fileName);

while (us.Read())
{

while (us.ReadToFollowing("StartYear"))
{
startYear = us.ReadElementContentAsString();
}
}
us.Close();

System.Xml.XmlTextReader ug = new System.Xml.XmlTextReader(fileName);

while (ug.Read())
{

while (ug.ReadToFollowing("StartMinute"))
{
startMinute = ug.ReadElementContentAsString();
}
}
ug.Close();

System.Xml.XmlTextReader uz = new System.Xml.XmlTextReader(fileName);

while (uz.Read())
{

while (uz.ReadToFollowing("StartHour"))
{
startHour = uz.ReadElementContentAsString();
}
}
uz.Close();

}

private void sendEmail()
{
WebClient myClient = new WebClient();
string myPageHTML = null;
byte[] requestHTML;
// Gets the url of the page
string currentPageUrl = Request.Url.ToString();

UTF8Encoding utf8 = new UTF8Encoding();

requestHTML = myClient.DownloadData(currentPageUrl);

myPageHTML = utf8.GetString(requestHTML);

const string SERVER = "relay-hosting.secureserver.net";
MailMessage objMail = new MailMessage();
objMail.From = new MailAddress("from@fromAddress.com");
objMail.To.Add(new MailAddress("to@toAddress.com"));
objMail.IsBodyHtml = true;
objMail.Subject = "MCE Order Confirmation";
objMail.Priority = System.Net.Mail.MailPriority.High;
objMail.Body = myPageHTML;
SmtpClient Client = new SmtpClient(SERVER, 25);
Client.Credentials = CredentialCache.DefaultNetworkCredentials;

try
{
Client.Send(objMail);
}
catch
{
Response.Write("There was an error sending you your tickets!");
}
}

}
}

justin001
10-03-2012, 08:49 AM
Hello Sean,

Can you kindly share your webconfig code, so we have a better idea,
because sometime we see that there was a little mistake in webconfig file and we are debugging coding page whole day and we can't get solution.

Please share you webconfig code,
waiting for your reply,

Thanks and Regards,
Justin Ontong

alykins
10-03-2012, 03:35 PM
I doubt it has anything to do with the config file. Also don't share config files to the public (especially if you want to maintain security of site)

Put a break point on the page load and step through it. My guess is that it is being called in a loop or something

Anton Pavlov
10-05-2012, 04:10 PM
I experienced the same when I tried to send SMS from Outlook with a Gateway software. The software somehow did not update which e-mail was sent and which was not. I followed all instructions again and it solved now. I dont know which config step was missing but after I reconfigured it worked well. Maybe you can get some idea from this site (I followed this)http://www.sms-integration.com/what-to-do-if-sms-in-outlook-mobile-service-does-not-work-94.html

hope it helps

Anton



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum