EDN Admin
Well-known member
Hi,
In my program, Im converting files from crystal reports to .pdf with dynamic names. Each file is associated with one client. Each client has their own email address. I have all the email address in the database table (sql server). I would
like, everytime theres new pdf has been created it emails to that client with attachement of that PDF file.
This is the method to convert files from crystal to pdf:
<pre class="prettyprint private void ExportReport(string reportPath)
{
string fileName = Path.GetFileName(reportPath);
ReportDocument report = new ReportDocument();
report.Load(reportPath);
try
{
ExportOptions options = report.ExportOptions;
options.ExportDestinationType = ExportDestinationType.DiskFile;
options.ExportFormatType = ExportFormatType.PortableDocFormat;
options.DestinationOptions = new DiskFileDestinationOptions()
{
DiskFileName = String.Format(
@"C:UserstestDesktopconvertedfiles{0}_DetailedCallReport_{1:yyyyMM}.pdf",
System.IO.Path.GetFileNameWithoutExtension(fileName),
DateTime.Now.AddMonths(-1)
)
};
options.FormatOptions = new PdfRtfWordFormatOptions();
report.Export(); <span class="x_Apple-tab-span" style="white-spacere //send email
email();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
} [/code]
This is how I send email:
<pre class="prettyprint private void email()
{
try
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("myemail@gmail.com");
mail.To.Add("toperson@gmail.com");
mail.Subject = "Detail Call Report";
mail.Body = "test";
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment("C:\Users\test\Desktop\test.pdf");
mail.Attachments.Add(attachment);
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("email", "password");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
label14.Text = "Message Sent";
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}[/code]
<br/>
This email method is not exactly what Im looking for.
When I call email(); method inside my ExportReport(); it does send email, however only to one person with
specified attachement . Not the one is getting generated.
Ive tried to replace this line:
<pre class="prettyprint attachment = new System.Net.Mail.Attachment("C:\Users\test\Desktop\test.pdf");[/code]
<br/>
with this:
<pre class="prettyprint attachment = new System.Net.Mail.Attachment(DiskFileName);[/code]
to grab the attachement, it wont send email.
Ive read some threads here on how to send email to multiple recipients using database, but that did not work for me.
Any ideas on how I can send emails to different users with attachment that belongs to them?
Thank you.
<br/>
View the full article
In my program, Im converting files from crystal reports to .pdf with dynamic names. Each file is associated with one client. Each client has their own email address. I have all the email address in the database table (sql server). I would
like, everytime theres new pdf has been created it emails to that client with attachement of that PDF file.
This is the method to convert files from crystal to pdf:
<pre class="prettyprint private void ExportReport(string reportPath)
{
string fileName = Path.GetFileName(reportPath);
ReportDocument report = new ReportDocument();
report.Load(reportPath);
try
{
ExportOptions options = report.ExportOptions;
options.ExportDestinationType = ExportDestinationType.DiskFile;
options.ExportFormatType = ExportFormatType.PortableDocFormat;
options.DestinationOptions = new DiskFileDestinationOptions()
{
DiskFileName = String.Format(
@"C:UserstestDesktopconvertedfiles{0}_DetailedCallReport_{1:yyyyMM}.pdf",
System.IO.Path.GetFileNameWithoutExtension(fileName),
DateTime.Now.AddMonths(-1)
)
};
options.FormatOptions = new PdfRtfWordFormatOptions();
report.Export(); <span class="x_Apple-tab-span" style="white-spacere //send email
email();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
} [/code]
This is how I send email:
<pre class="prettyprint private void email()
{
try
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("myemail@gmail.com");
mail.To.Add("toperson@gmail.com");
mail.Subject = "Detail Call Report";
mail.Body = "test";
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment("C:\Users\test\Desktop\test.pdf");
mail.Attachments.Add(attachment);
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("email", "password");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
label14.Text = "Message Sent";
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}[/code]
<br/>
This email method is not exactly what Im looking for.
When I call email(); method inside my ExportReport(); it does send email, however only to one person with
specified attachement . Not the one is getting generated.
Ive tried to replace this line:
<pre class="prettyprint attachment = new System.Net.Mail.Attachment("C:\Users\test\Desktop\test.pdf");[/code]
<br/>
with this:
<pre class="prettyprint attachment = new System.Net.Mail.Attachment(DiskFileName);[/code]
to grab the attachement, it wont send email.
Ive read some threads here on how to send email to multiple recipients using database, but that did not work for me.
Any ideas on how I can send emails to different users with attachment that belongs to them?
Thank you.
<br/>
View the full article