Author |
|
ay1585 Newbie
Joined: 01 April 2009 Location: India
Online Status: Offline Posts: 7
|
Posted: 01 April 2009 at 1:54am | IP Logged
|
|
|
I am sending a mail using SMTP. I want to save the mail,including the details like To,Cc,Bcc,Subject,Body and also the attachment path(if any attachment exists) in to a database table,using a unique id. How to make this possible?
|
Back to Top |
|
|
Andrew AfterLogic Support
Joined: 28 April 2006 Location: United States
Online Status: Offline Posts: 1189
|
Posted: 01 April 2009 at 4:00am | IP Logged
|
|
|
You can save a message into a database as follows (in C# syntax):
Code:
// Create message object or replace this code with receiving message from POP3/IMAP server
MailMessage msg = new MailMessage();
// Create connection with the database
string myConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\messages.mdb;User Id=admin;Password=;";
OleDbConnection myConnection = new OleDbConnection(myConnectionString);
try
{
myConnection.Open();
// Insert raw message data into the database
string commandString = @"INSERT INTO messages_table (uid, raw_data) VALUES ( @uid, @raw_data )";
OleDbCommand cmd = new OleDbCommand(commandString, myConnection);
cmd.Parameters.Add("@uid", msg.UidOnServer);
cmd.Parameters.Add("@raw_data" , msg.GetMessageRawData());
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
}
finally
{
myConnection.Close();
} |
|
|
Best regards,
Andrew
|
Back to Top |
|
|
ay1585 Newbie
Joined: 01 April 2009 Location: India
Online Status: Offline Posts: 7
|
Posted: 01 April 2009 at 6:39am | IP Logged
|
|
|
I Thank you for your reply. I am trying to work on the code which you have suggested. How ever,I have few queries. First of all,I am sending a mail. I want this sent mail to be saved in my SQLServer Database table for my future reference. This mail should be saved in my Send click itself. So, that based on the "To" recepient address, I may need to send a remainder in future. This is the requirement. Can you please suggest on this?
|
Back to Top |
|
|
Andrew AfterLogic Support
Joined: 28 April 2006 Location: United States
Online Status: Offline Posts: 1189
|
Posted: 01 April 2009 at 6:48am | IP Logged
|
|
|
To save message after or before sending, you should replace msg.GetMessageRawData() with mailer.Message.GetMessageRawData() where "mailer" is an instance of Smtp class used for sending the message.
Best regards,
Andrew
|
Back to Top |
|
|
ay1585 Newbie
Joined: 01 April 2009 Location: India
Online Status: Offline Posts: 7
|
Posted: 01 April 2009 at 6:48am | IP Logged
|
|
|
ay1585 wrote:
I Thank you for your reply. I am trying to work on the code which you have suggested. How ever,I have few queries. First of all,I am sending a mail. I want this sent mail to be saved in my SQLServer Database table for my future reference. This mail should be saved in my Send click itself. So, that based on the "To" recepient address, I may need to send a remainder in future. This is the requirement. Can you please suggest on this? |
|
|
I also have to create my database table accordingly.
For your reference,this is the code that I have used:
objSmtp = new Smtp();
objSmtp.From.AsString = "my mail address";
objSmtp.To.AsString = txtReceiver.Text;
objSmtp.Cc.AsString = txtCc.Text;
objSmtp.Bcc.AsString = txtBcc.Text;
objSmtp.Subject = txtSubject.Text;
objSmtp.Message.BodyHtmlText = Editor.Text;
objSmtp.SmtpServers.Add("my host name", "user name", "password");
objSmtp.DnsServers.Autodetect();
objSmtp.Connect();
objSmtp.Message.Attachments.Add(FileUpload1.PostedFile.FileN ame);
objSmtp.Send();
objSmtp.Disconnect();
|
Back to Top |
|
|