Hello everyone,
As now I'm done with sending messages to email accounts with both user controls and login controls . Now one thing that remains now is to send attachments with emails.
I have gone through one of the posts pointing to same problem and in response he wad suggested to visit
http://imar.spaanjaars.com/QuickDocId.aspx?quickdoc=412 . I have gone through the same link there is the following code snippet
Code:
if (FileUpload1.HasFile)
{
string toAddress = "[email protected]";
string fromAddress = [email protected] (2);
string mailServer = "smtp.yourprovider.com";
MailMessage myMailMessage = new MailMessage();
myMailMessage.To.Add(toAddress);
myMailMessage.From = new MailAddress(fromAddress);
myMailMessage.Subject = "Test Message";
string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
Attachment myAttachment =
new Attachment(FileUpload1.FileContent, fileName);
myMailMessage.Attachments.Add(myAttachment);
SmtpClient mySmtpClient = new SmtpClient(mailServer);
mySmtpClient.Send(myMailMessage);
}
I have my code accordingly as there are already
"myMessage"
My code seems like this
Code:
protected void btnSend_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
string fileName = Server.MapPath("~/App_Data/ContactForm.txt");
string mailBody = System.IO.File.ReadAllText(fileName);
mailBody = mailBody.Replace("##Name##", txtName.Text);
mailBody = mailBody.Replace("##Email##", txtEmailAddress.Text);
mailBody = mailBody.Replace("##HomePhone##", txtPhoneHome.Text);
mailBody = mailBody.Replace("##BusinessPhone##", txtPhoneBusiness.Text);
mailBody = mailBody.Replace("##Comments##", txtComments.Text);
MailMessage myMessage = new MailMessage();
myMessage.Subject = "Response From Web Site";
myMessage.Body = mailBody;
myMessage.From = new MailAddress("[email protected]", "name");
myMessage.To.Add(new MailAddress("[email protected]", "name"));
if (FileUpload1.HasFile)
{
string fileName1 = Path.GetFileName(FileUpload1.PostedFile.FileName);
Attachment myAttachment = new Attachment(FileUpload1.FileContent, fileName1);
myMessage.Attachments.Add(myAttachment);
}
SmtpClient mySmtpClient = new SmtpClient();
try
{
mySmtpClient.EnableSsl = true;
mySmtpClient.Send(myMessage);
}
catch (Exception ex)
{
lblMessage.Text = "An error occurred while sending your e-mail. Please try again.";
}
lblMessage.Visible = true;
FormTable.Visible = false;
After adding simple this FileUpload control in ContactForm.aspx page in other row, on sending feedback with simple message without attaching any file is reported as success but no message is received at all. And if I attach file then it is reported as failed
Kindly take a look at this.........