Click here to Skip to main content
15,888,802 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi All i want to send an email with Attachment(pdf,jpg)

here what i have wrote.
C#
public static string SalesNotifiesToProduction(string UserName, string company, string publication,  int noticeid,
                  string projectanme, string summary,string SpecialInstructions,string logo,string PEmail,string Email,string Phonenumber,
           string ResponseType,string EmailResponseTo,string Fax,string DirectURL,string PrintDates)
        {
            try
            {
                MailMessage mail = new MailMessage();
                mail.To.Add(PEmail);
                mail.Bcc.Add("my email");
               
                mail.From = new MailAddress(ConfigurationManager.AppSettings["MailFrom"].ToString());
                mail.Subject = "OPBN Notice #: " + noticeid + " | Type Set Request";
               
//// attachment code here //

/// code ends //

                string str = "";
                if (ResponseType == "Yes")
                {
                    string strFax = "";
                    if (Fax != null)
                    {
                       strFax=" , Fax: " + Fax;                        
                    }
                    string strEmail = "";
                    if (EmailResponseTo != null)
                    {
                        strEmail = " , Email: " + EmailResponseTo;
                    }
                    string strURL = "";
                    if (DirectURL != null)
                    {
                        strURL = " Contact URL: <a href="" + DirectURL + "">Click Here </a> ";
                    }
                    str =strURL + strEmail + strFax;
                }

                string Body = "Some text"; 
                mail.Body = Body;

                mail.IsBodyHtml = true;
                SmtpClient smtp = new SmtpClient(ConfigurationManager.AppSettings["mailserver"].ToString(), Convert.ToInt32(ConfigurationManager.AppSettings["port"]));
                if (Convert.ToBoolean(ConfigurationManager.AppSettings["developement"]))
                {
                    smtp.Credentials = new NetworkCredential(ConfigurationManager.AppSettings["username"].ToString(), ConfigurationManager.AppSettings["password"].ToString());
                }
                smtp.EnableSsl = Convert.ToBoolean(ConfigurationManager.AppSettings["EnableSsl"]);
                smtp.Send(mail);
                return "Sent Notification Success!";
            }
            catch (Exception ex)
            {
               return ex.Message.ToString();
            }
        }


this works perfectly fine and send email. But when i tried to attach a file using this code

 String filename = VirtualPathUtility.GetFileName(logo);


                if (filename != null && filename.Length > 0 )
                {
                    //Attachment file = new Attachment(filename);
                    //mail.Attachments.Add(file);

                    var attachment = new Attachment(filename);
                    mail.Attachments.Add(attachment);
                }

emails doesnot go out. here logo is a string with a URL/path of the file where my pdf/jpg file is stored. so i am getting the filename first and then checking if its null or not and send the email with Attachments.Add function. Now my file is stored in "Main project directory"/PDFfiles/ folder.

Do i have to open the file first then attache it or the file name will find the file in working directory and attache the file automatically?

Also let me know if i am missing anything in code.

Thanks.
Posted
Updated 9-Aug-13 9:59am
v3

Use following piece of code to add attachment,

Attachment attachment = new Attachment(attachmentFilename, MediaTypeNames.Application.Octet);
ContentDisposition disposition = attachment.ContentDisposition;
disposition.CreationDate = File.GetCreationTime(attachmentFilename);
disposition.ModificationDate = File.GetLastWriteTime(attachmentFilename);
disposition.ReadDate = File.GetLastAccessTime(attachmentFilename);
disposition.FileName = Path.GetFileName(attachmentFilename);
disposition.Size = new FileInfo(attachmentFilename).Length;
disposition.DispositionType = DispositionTypeNames.Attachment;
mail.Attachments.Add(attachment);
 
Share this answer
 
Comments
Member 3828789 22-Aug-13 16:44pm    
Hi sorry i was out out few a week. So yah i tried your solutions and it didnt send an email. If i just write the above code which i ahve posted it will send an email but with this its even not sending an email. few things i have to clear here might help you. the string logo which i am trying to attache is a full path of a file say "~/images/filename.jpg" so what i did is
add aline on top of yorr code.
String filename = VirtualPathUtility.GetFileName(logo);
and then your code.
Attachment attachment = new Attachment(filename, MediaTypeNames.Application.Octet);
ContentDisposition disposition = attachment.ContentDisposition;
disposition.CreationDate = File.GetCreationTime(filename);
disposition.ModificationDate = File.GetLastWriteTime(filename);
disposition.ReadDate = File.GetLastAccessTime(filename);
disposition.FileName = Path.GetFileName(filename);
disposition.Size = new FileInfo(filename).Length;
disposition.DispositionType = DispositionTypeNames.Attachment;
mail.Attachments.Add(attachment);

let me know if you found any problem.
Thanks.
Look at this tip. It basically covers every aspect of sending email.

Sending an Email in C# with or without attachments: generic routine.[^]
 
Share this answer
 

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