I manage to upload multiple files from a web form to my local drive. Say at "D:uploadedfilesamy". How do I attach all files in the directory on my email? The email without attachment is sending just fine.
I'm having some trouble with the asp.net FileUpload Control and sending email attachments.
I am able to run the following code and successfully create and attach 1 attachment to an email send mail it to myself with no problems.
[Code]....
I need to be able to handle multiple files and I am also saving the files to an sql database. I tried using multiple file upload controls (max 3) and can save the files to the database successfully, but any and all attachments that are emailed, appear corrupted in my inbox. So, I removed all the fileupload controls and left just on on the page. I created a generic List<Attachment> to store the attachments created from the fileupload control and then iterate through all entries and attach them to my email and save them to the database. Once again, saving to the database works perfectly and i can read the attachments back, but the email attachments are corrupt. Is there anything obvious that I am doing wrong?
I have tried with your pice of code(Published date : sep 16, 2012) to send multiple attachments in asp.net. On my local machine it is working perfectly. When I host my application on server, mail contains attachment(Single or multiple both) is not sending. Attachment and remove files for attachments are working properly but attachment mail is not sending on server.
I have a form on which anyone can send me his/her information. Now i want to add 2 or 3 file upload options so that anyone from the website can send me their files along with the information. For getting information i am using this code (in button click) which works fine for me:
using System; using System.Net.Mail; using System.Text; using System.Web.UI; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; protected void btnMyButton_Click(object sender, EventArgs e) { if (Page.IsValid) { try { string enquiry = txtEnquiry.Text.Trim(); if (String.IsNullOrEmpty(enquiry)) return; string emailTitle = GetLocaleResourceString("MyTopic.EmailTitle"); string fullName = txtFullName.Text.Trim(); string email = txtEmail.Text.Trim(); string subject = string.Format("{0}. {1}", SettingManager.StoreName, emailTitle); var body = new StringBuilder(); body.AppendFormat("<hr />{0} ({1})<hr />", emailTitle, DateTime.Now.ToShortDateString()); body.AppendFormat("<b>{0}:</b> {1}<br />", GetLocaleResourceString("MyTopic.FullName"), fullName); body.AppendFormat("<b>{0}:</b> {1}<br />", GetLocaleResourceString("MyTopic.E-MailAddress"), email); body.Append("<br />"); body.AppendFormat("<b>{0}:</b><br /> {1}<br />", GetLocaleResourceString("MyTopic.Enquiry"), MessageManager.FormatContactUsFormText(enquiry)); var from = new MailAddress(email, fullName); var emailAccount = MessageManager.DefaultEmailAccount; string body2 = body.ToString(); //required for some SMTP servers if (SettingManager.GetSettingValueBoolean("Email.UseSystemEmailForContactUsForm")) { from = new MailAddress(emailAccount.Email, emailAccount.DisplayName); body2 = string.Format("<b>From</b>: {0} - {1}<br /><br />{2}", Server.HtmlEncode(fullName), Server.HtmlEncode(email), body2); } var to = new MailAddress(emailAccount.Email, emailAccount.DisplayName); MessageManager.InsertQueuedEmail(5, from, to, string.Empty, string.Empty, subject, body2, DateTime.UtcNow, 0, null, emailAccount.EmailAccountId); pnlResult.Visible = true; pnlRequestQuote.Visible = false; }
Now I added 3 file upload options in the page, now when i try to add code in .cs file i get errors. This is what i am trying to add (in BOLD)
protected void btnMyButton_Click(object sender, EventArgs e) { if (Page.IsValid) { try { string enquiry = txtEnquiry.Text.Trim(); if (String.IsNullOrEmpty(enquiry)) return; string emailTitle = GetLocaleResourceString("MyTopic.EmailTitle"); string fullName = txtFullName.Text.Trim(); string email = txtEmail.Text.Trim(); string subject = string.Format("{0}. {1}", SettingManager.StoreName, emailTitle); var body = new StringBuilder(); body.AppendFormat("<hr />{0} ({1})<hr />", emailTitle, DateTime.Now.ToShortDateString()); body.AppendFormat("<b>{0}:</b> {1}<br />", GetLocaleResourceString("MyTopic.FullName"), fullName); body.AppendFormat("<b>{0}:</b> {1}<br />", GetLocaleResourceString("MyTopic.E-MailAddress"), email); body.Append("<br />"); body.AppendFormat("<b>{0}:</b><br /> {1}<br />", GetLocaleResourceString("MyTopic.Enquiry"), MessageManager.FormatContactUsFormText(enquiry)); //'Attach the file if (FileUpload2.HasFile) { body.Attachments.Add(new Attachment(AttachmentFile.PostedFile.InputStream, AttachmentFile.FileName)); body.Attachments.Add(new Attachment(FileUpload1.PostedFile.InputStream, FileUpload1.FileName)); body.Attachments.Add(new Attachment(FileUpload2.PostedFile.InputStream, FileUpload2.FileName)); } else { body.Attachments.Add(new Attachment(AttachmentFile.PostedFile.InputStream, AttachmentFile.FileName)); body.Attachments.Add(new Attachment(FileUpload1.PostedFile.InputStream, FileUpload1.FileName)); } var from = new MailAddress(email, fullName); var emailAccount = MessageManager.DefaultEmailAccount; string body2 = body.ToString(); //required for some SMTP servers if (SettingManager.GetSettingValueBoolean("Email.UseSystemEmailForContactUsForm")) { from = new MailAddress(emailAccount.Email, emailAccount.DisplayName); body2 = string.Format("<b>From</b>: {0} - {1}<br /><br />{2}", Server.HtmlEncode(fullName), Server.HtmlEncode(email), body2); } var to = new MailAddress(emailAccount.Email, emailAccount.DisplayName); MessageManager.InsertQueuedEmail(5, from, to, string.Empty, string.Empty, subject, body2, DateTime.UtcNow, 0, null, emailAccount.EmailAccountId); pnlResult.Visible = true; pnlRequestQuote.Visible = false; }
I get this error message:
System.Text.StringBuilder' does not contain a definition for 'Attachments' and no extension method 'Attachments' accepting a first argument of type 'System.Text.StringBuilder' could be found (are you missing a using directive or an assembly reference?)
I hava a web form to fill a pdf, add attachments and send it through email. I need to fisically delete the attachments after send the email. I have some problems (exception) that says "the file has been used by another process". This code is in a function that create the message with the attach.
I have used this solution. And it's working fine for single id. But when i am using multiple id as reciepent mail id , it's mailing the text only to the first id. I am providing multiple id as below:
Here, if I place gmail id first and ymail id second, the would be send's only to gmail id not to ymail id, and same in case if I place ymail first and gmail second then mail send to ymail and not to gmail.
I am trying to sent an email to some addresses i did that using the System.Net.mail the problem is that i need to make the mail message different for each recipient because i need to put a link inside the email that contain the id this user, the problem is the large number of recipient that i cant use a loop to invoke sending function for each usesr like:
I have an asp.net panel having various controls including gridview. I have converted this panel into pdf and attached it as an email attachment using memory stream. Everything is working fine. Now I have an File upload control outside panel through which I have to attach a file and send it in mail along with the already attached panel. But I am unable to figure out how to do it.
protected void SaveEmails(object sender, EventArgs e) { for (int i = 0; i < this.Emails.Count; i++) { string constr = ConfigurationManager.ConnectionStrings["ConString2"].ConnectionString; string sqlStatment = "INSERT INTO [Emails] ([From],[Subject],[Body],[Date]) VALUES (@From ,@Subject,@Body,@Date)"; using (SqlConnection con = new SqlConnection(constr)) { using (SqlCommand cmd = new SqlCommand(sqlStatment, con))
[Code] ....
I want to store attachments also... How to store attachments....