I want to send the uploaded files as an attachment to an email.
Hi,
Thanks for your back.
For attaching email a file dirrectly from the FileUpload control, please chec below code.
private bool SendMail()
{
try
{
/* Create a new blank MailMessage */
MailMessage mailMessage = new MailMessage();
mailMessage.From = txtTo.Text;
mailMessage.To = "your email";
mailMessage.Subject = txtSubject.Text ;
msgMail.Body = txtMessage.Text ;
/* We use the following variables to keep track of
attachments and after we can delete them */
string attach1 = null;
/*strFileName has a attachment file name for attachment process. */
string strFileName = null;
if (FileUpload1.PostedFile != null)
{
/* Get a reference to PostedFile object */
HttpPostedFile attFile = FileUpload1.PostedFile;
/* Get size of the file */
int attachFileLength = attFile.ContentLength;
/* Make sure the size of the file is > 0 */
if (attachFileLength > 0)
{
/* Get the file name */
strFileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
/* Save the file on the server */
FileUpload1.PostedFile.SaveAs(Server.MapPath(strFileName));
/* Create the email attachment with the uploaded file */
MailAttachment attach = new MailAttachment(Server.MapPath(strFileName));
/* Attach the newly created email attachment */
mailMessage.Attachments.Add(attach);
/* Store the attach filename so we can delete it later */
attach1 = strFileName;
}
/* Set the SMTP server and send the email with attachment */
SmtpMail.SmtpServer = ("your smtp");
SmtpMail.Send(mailMessage);
/* Delete the attachements if any */
if (attach1 != null)
{
File.Delete(Server.MapPath(attach1));
}
}
return true;
}
catch (Exception ex)
{
return false;
}
}
protected void btn_Send_Click(object sender, EventArgs e)
{
bool TrueOrFalse = SendMail();
if ((TrueOrFalse == true))
{
Response.Redirect("~/MailSent.aspx");
}
else
{
Label1.Text = "Try again";
}
}
There is example for this, please follow below article.
All-Star
30411 Points
3628 Posts
Re: Updating Specific Panel in an ASPX file
Mar 23, 2014 09:51 AM|Fuxiang Zhang - MSFT|LINK
Hi,
Thanks for your back.
For attaching email a file dirrectly from the FileUpload control, please chec below code.
There is example for this, please follow below article.
http://www.codedigest.com/Articles/ASPNET/374_Send_Email_with_Attachment_directly_from_FileUpload_control_in_ASPNet.aspx
Thanks.
Best Regards!