Click here to Skip to main content
15,891,769 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How do I add an attachment from "C:\path\Word.docx" to this code:

C#
MailMessage msg = New MailMessage ();
MailAddress fromAdd = New MailAddress("fromemail@email.com");
msg.[To].Add("toemail@email.com");
msg.Subject = "Choose Session Members";
msg.From = fromAdd;
msg .IsBodyHtml = True;
msg.Priority = MailPriority.Normal;
msg .BodyEncoding = Encoding.Default;
msg.Body = "<center><table><tr><td><h1>Your Message</h1><br/><br/></td></tr>";
msg.Body = msg.Body + "</table></center>";
SmtpClient smtpClient = New SmtpClient ("smtp.yourserver.com", "25");
smtpClient.EnableSsl = True;
smtpClient.UseDefaultCredentials = False;
smtpClient.Credentials = New System.Net.NetworkCredential("smtpserver@yourserver.com", "password");
smtpClient .DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.Send(msg);
smtpClient.Dispose();
Posted
Updated 2-Sep-14 0:14am
v2

1 solution

Use the Following Code to Send mail Using Attachment:

using System;
using System.Net.Mail;

public partial class _Default : System.Web.UI.Page
{
	protected void Button1_Click(object sender, EventArgs e)
	{
		string attachmentFile = null ;
		if (FileUpload1.HasFile)
		{
			try
			{
				FileUpload1.SaveAs("C:\\files\\" + FileUpload1.FileName);
				attachmentFile = FileUpload1.PostedFile.FileName;
			}
			catch (Exception ex)
			{
				Label1.Text = "File Upload Failed !! " + ex.Message.ToString();
			}

			try
			{
				MailMessage mail = new MailMessage();
				SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");

				mail.From = new MailAddress("your-gamila-ddress@gmail.com"); //you have to provide your gmail address as from address
				mail.To.Add(to_txt.Text);
				mail.Subject = subject_txt.Text;
				mail.Body = message_txt.Text;

				System.Net.Mail.Attachment attachment;
				attachment = new System.Net.Mail.Attachment(attachmentFile);
				mail.Attachments.Add(attachment);

				SmtpServer.Port = 587;
				SmtpServer.Credentials = new System.Net.NetworkCredential("gamil-username", "gmail-passowrd"); //you have to provide you gamil username and password
				SmtpServer.EnableSsl = true;

				SmtpServer.Send(mail);
				Label1.Text = "Email successfully sent.";
			}
			catch (Exception ex)
			{
				Label1.Text = "Mail Send Failed !! " + ex.Message.ToString();
			}
		}
		else
		{
			Label1.Text = "Please select a file for uploading";
		}
	}
}
 
Share this answer
 
Comments
Manfred Rudolf Bihy 2-Sep-14 6:35am    
When copying a solution verbatim from a web site you have to add a reference, otherwise it looks as if you wrote it yourself.
Plagiarism, even if ony in Q&A solutions, is not condoned here on CP.

Here is a link of where you copied it from: http://asp.net-informations.com/communications/files/asp-email-attachment-default.aspx.cs.htm

Cheers!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900