Reputation: 6387
I am using PDFsharp to generate a job report. I need that report automatically attached to an outgoing e-mail. Right now when the user is viewing the job information in a angular modal, they click Email and the report is generated and a new modal appears with the e-mail input fields. How do I setup the Email Controller to find the correct PDF in the JobSetupPdfs folder?
PdfController
public string Get(int id = 1)
{
JobDataAdapter adapter = new JobDataAdapter();
Job job = new Job();
job = adapter.GetJob(id);
if (job == null)
{
return string.Empty;
}
try
{
// Create a new PDF document
PdfDocument document = new PdfDocument();
document.Info.Title = "Created with PDFsharp";
gfx.DrawString("Texas Exterior Systems", HeadingFont, XBrushes.Black,
new XRect(0, 50, page.Width, page.Height),
XStringFormats.TopCenter);
gfx.DrawString("Job Setup Sheet", BodyFont, XBrushes.Black,
new XRect(0, 80, page.Width, page.Height),
XStringFormats.TopCenter);
var filename = string.Format(@"C:\Users\texas_000\Desktop\TexasExterior\TexasExterior\JobSetupPdfs\{0}.pdf", job.JobName);
document.Save(filename);
}
catch (Exception ex)
{
Debug.Print(ex.ToString());
}
return string.Empty;
}
Mail Controller
[Authorize]
public ActionResult EmailPdf( string To, string Cc, string comments, string Bcc, HttpPostedFileBase fileUploader)
{
try
{
SmtpClient client = new SmtpClient("mail.texasicecarving.com", 8889);
//client.EnableSsl = true;
client.Timeout = 100000;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("[email protected]", "******");
MailMessage msg = new MailMessage();
if (fileUploader != null)
{
string fileName = Path.GetFileName(fileUploader.FileName);
msg.Attachments.Add(new Attachment(fileUploader.InputStream, fileName));
}
msg.To.Add(new MailAddress( "[email protected]"));
msg.From = new MailAddress("[email protected]");
// msg.Subject = name + " " + subject;
msg.Body = comments;
//MailAddress Ccopy = new MailAddress(user.Email);
//msg.CC.Add(Ccopy);
//MailAddress Bcopy = new MailAddress(Bcc);
//msg.Bcc.Add(Bcopy);
client.Send(msg);
Console.WriteLine("Successfully Sent Message.");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return View();
}
Angular Controller
$scope.EmailPdf = function () {
$http.get('/api/Pdf/{id}').success(function () {
$scope.PrintPreviewModal();
});
}
Update OK, so I have it setup with the correct path. Now how do I have it attach the PDF that was just generated? Would I need to add a time stamp to the PDF and have msg.Attachments attach the newest PDF in that folder?
msg.Attachments.Add(new Attachment(@"C:\Users\texas_000\Desktop\TexasExterior\TexasExterior\JobSetupPdfs\Restoration.pdf"));
Upvotes: 2
Views: 1059
Reputation: 169
You can use the attachments collection on the mail message object.
msg.Attachments.Add(new Attachment(PathToAttachment));
Check out this MSDN article for more information: http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.attachments.aspx
Upvotes: 1