Last post Jul 06, 2019 01:46 PM by mgebhard
Jul 05, 2019 09:10 PM|Diana4|LINK
Hi, I have developed a C# ASP.NET Web Application. In that application there is an .aspx page, in which in the Page_Load routine, I select data out of SQL Server 2012 database, check for certain alarm/warning signal, and if the signal is detected in the
data, code is used to send out outlook email to email addresses, which are looked up the database again. Also updates are made to the SQL Server tables. The Email code is shown below…
If am having few problems.
Thank you. The outlook I am using is pasted below..
using Outlook = Microsoft.Office.Interop.Outlook;
private void send_email(string reference_id, string errors_file_name, string png_image_file_path_name, DataTable email_table)
Outlook._Application oApp = new Outlook.Application();
Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
Outlook.Recipients oRecips = (Outlook.Recipients)oMsg.Recipients;
if (email_table.Rows.Count > 0)
foreach (DataRow dr in email_table.Rows)
oMsg.Subject = "Alarm Events for " + reference_id;
oMsg.Subject = "Email Address not found for " + reference_id;
string bodytext = "my body text ";
bodytext = bodytext + "...";
bodytext = bodytext + "...";
oMsg.Body = bodytext;
oMsg.Importance = Outlook.OlImportance.olImportanceNormal;
string sDisplayName = "MyAttachment";
int iPosition = (int)oMsg.Body.Length + 1;
int iAttachmentType = (int)Outlook.OlAttachmentType.olByValue;
Outlook.Attachment oAttach = oMsg.Attachments.Add(errors_file_name, iAttachmentType, iPosition, sDisplayName);
oMsg = null;
oApp = null;
oRecips = null;
Jul 05, 2019 09:16 PM|mgebhard|LINK
Office Interop is not intended for use on a web server. You'll need to change the approach.
Use the SmtpClient to send emails. You'll need to get the configuration from your email service. These are things like the SMPT server, Port, and Credentials.
Jul 05, 2019 11:37 PM|Diana4|LINK
Thank you for your response. But when I go to the 2nd web link you have provided, it says This API is now obsolete. Do you have another easy to use, but robust alternative please ?
Jul 06, 2019 08:18 AM|PatriceSc|LINK
As told by this page you could use
https://github.com/jstedfast/MailKit instead but using SmtpClient will be already a huge improvement. Anyway you'll likely hide this behing your own mail sending method and youl'l be able to replace this later as needed.
Jul 06, 2019 12:31 PM|Diana4|LINK
Thank you. I tried and wrote some sample C# using https://github.com/jstedfast/MailKit.
I send mails to myself for testing, in the c# code of the .aspx web page. And it seems to work. I get the emails to me and I can see them in my outlook Inbox. But very strangely, after few minutes they automatically disappear.
That was not happening when I was using the iterop email code.
Can you please help with this ? I think we are close ..
Jul 06, 2019 01:46 PM|mgebhard|LINK
This new issue has nothing to do with ASP.NET. My best guess is you have a rule set in the Outlook client or you are using POP3 which moves the email from the server to the client.
Secondly, there is nothing wrong with using SmtpClient considering you are using Web Forms which is a legacy framework. SmtpClient totally appropriate for Web Forms and the reason I recommend SmtpClient.