thisisagirlwhocode
Member
- Joined
- Feb 2, 2022
- Messages
- 15
- Programming Experience
- Beginner
I'm trying to save the Outlook attachment file email and save in sql. I tried this following code and it only saved a numerical values
This is what it saved in my sql, it didn't save the attachment I'm trying to save the attachment file with extension of (.docx .pdf .excel .pptx)
Note: I used varbinary(MAX) for attachment data type in sql
C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
using Outlook = Microsoft.Office.Interop.Outlook;
namespace RetrieveEmail
{
public class Program
{
static void Main(string[] args)
{
Outlook.Application oLk = new Outlook.Application();
Outlook._NameSpace olNS = oLk.GetNamespace("MAPI");
olNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail);
Outlook.Items oItems = oFolderIn.Items;
foreach (object item in oFolderIn.Items)
{
if (item is Outlook.MailItem oMailItem)
{
SqlConnection con = new SqlConnection(@"Data Source=\SQLEXPRESS; initial catalog=EmailReply;Integrated Security=True");
SqlCommand cmd = new SqlCommand("INSERT INTO Emails (SenderName, Subject, Body, Attachment) VALUES (@SenderName, @Subject, @Body, @Attachment)", con);
cmd.Parameters.AddWithValue("@SenderName", oMailItem.SenderName);
cmd.Parameters.AddWithValue("@Subject", oMailItem.Subject);
cmd.Parameters.AddWithValue("@Body", oMailItem.Body);
cmd.Parameters.AddWithValue("@Attachment", oMailItem.EnableSharedAttachments); // I tried this code to extract Outlook attachment file email
con.Open();
int k = cmd.ExecuteNonQuery();
if (k != 0)
{
Console.WriteLine("Record Inserted Succesfully into the Database");
}
con.Close();
}
}
}
}
}
This is what it saved in my sql, it didn't save the attachment I'm trying to save the attachment file with extension of (.docx .pdf .excel .pptx)
Note: I used varbinary(MAX) for attachment data type in sql
Last edited: