Hi All i want to send an email with Attachment(pdf,jpg)
here what i have wrote.
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";
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 )
{
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.