I am having some trouble with the syntax. I’m trying to build a web form that users (at my work) can enter multiple email addresses into. For some reason it will only send to one email address.
Here’s my code… Please help. Hopefully all I need to do is change the syntax a bit.
using System;
using System.Data;
using System.Configuration;
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.IO;
using System.Net.Mail;
public partial class SendEmail1 : System.Web.UI.Page
{
string FileNameToAttache;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void sendButton_Click(object sender, EventArgs e)
{
SmtpClient smtpClient = new SmtpClient();
MailMessage message = new MailMessage();
try
{
MailAddress fromAddress = new MailAddress(fromTextBox.Text);
MailAddress toAddress = new MailAddress(toTextBox.Text, "");
message.From = fromAddress;
message.To.Add(toAddress);
message.Subject = subjectTextBox.Text;
message.IsBodyHtml = true;
/* Attaching Files Begin */
if (AttachFile1.PostedFile != null)
{
HttpPostedFile attFile = AttachFile1.PostedFile;
int attachFileLength = attFile.ContentLength;
if (attachFileLength > 0)
{
FileNameToAttache = Path.GetFileName(AttachFile1.PostedFile.FileName);
AttachFile1.PostedFile.SaveAs(Server.MapPath(FileNameToAttache));
message.Attachments.Add(new Attachment(Server.MapPath(FileNameToAttache)));
}
}
SmtpClient emailWithAttach = new SmtpClient();
emailWithAttach.Host = "DEVSMTP.corp.perfect-10.tv";
emailWithAttach.DeliveryMethod = SmtpDeliveryMethod.Network;
emailWithAttach.UseDefaultCredentials = true;
if (AttachFile2.PostedFile != null)
{
HttpPostedFile attFile = AttachFile2.PostedFile;
int attachFileLength = attFile.ContentLength;
if (attachFileLength > 0)
{
FileNameToAttache = Path.GetFileName(AttachFile2.PostedFile.FileName);
AttachFile1.PostedFile.SaveAs(Server.MapPath(FileNameToAttache));
message.Attachments.Add(new Attachment(Server.MapPath(FileNameToAttache)));
}
}
if (AttachFile3.PostedFile != null)
{
HttpPostedFile attFile = AttachFile3.PostedFile;
int attachFileLength = attFile.ContentLength;
if (attachFileLength > 0)
{
FileNameToAttache = Path.GetFileName(AttachFile3.PostedFile.FileName);
AttachFile1.PostedFile.SaveAs(Server.MapPath(FileNameToAttache));
message.Attachments.Add(new Attachment(Server.MapPath(FileNameToAttache)));
}
}
if (AttachFile4.PostedFile != null)
{
HttpPostedFile attFile = AttachFile4.PostedFile;
int attachFileLength = attFile.ContentLength;
if (attachFileLength > 0)
{
FileNameToAttache = Path.GetFileName(AttachFile4.PostedFile.FileName);
AttachFile1.PostedFile.SaveAs(Server.MapPath(FileNameToAttache));
message.Attachments.Add(new Attachment(Server.MapPath(FileNameToAttache)));
}
}
/* Attaching Files END */
message.Body =
"<html><head><title>" + HttpUtility.HtmlEncode(subjectTextBox.Text) +
"</title></head><body style='background-color:#edf8ff;'><div style='width:600px;'><table width='600' align='center' style='background-color:#ffffff;'>" + "<img src=\\"http:www.perfect-vision.com/Images/FlashReplacement.jpg\\" />" +
"<tr><td colspan='2'style='width:500px;'><p>" + HttpUtility.HtmlEncode(greetingTextBox.Text) + "</p></td></tr><br />" +
"<tr><td colspan='2'style='width:500px;'><p>" + HttpUtility.HtmlEncode(createTextBox.Text) + "</p></td><img src=\\"http:www.perfect-vision.com/Images/UL_SCTE.gif\\" /></tr><br />" +
"<tr><td colspan='2'style='width:500px;'><p>" + HttpUtility.HtmlEncode(closingTextBox.Text) + "</p></td></tr>" +
"<tr><td><img src=\\"http:www.perfect-vision.com/Images/FlashReplacement.jpg\\" /></td></tr>" +
"</table></div></body><html>";
smtpClient.Host = "DEVSMTP.corp.perfect-10.tv";
smtpClient.Send(message);
resultLabel.Text = "Email sent! <br />";
}
catch (Exception ex)
{
resultLabel.Text = "Couldn't Send the Message!";
}
}
protected void clearButton_Click(object sender, EventArgs e)
{
}
}