Capture and save Email Message and filename.

peter liles 556 Reputation points
2023-05-14T14:33:59.66+00:00

Before i send an email message in asp i would like to capture the Filename of the message.?

I want to be able to save the email message in a folder for reference purpose.

But in order to match email file in folder to a sent email template file i would need to capture the Filename and add it to email template before the send operation??

how may i achieve this aim like bay does with a Email ID reference number when sending delivery emails.

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,417 questions
{count} votes

Accepted answer
  1. Lan Huang-MSFT 28,841 Reputation points Microsoft Vendor
    2023-05-18T08:03:42.2833333+00:00

    Hi @peter liles,

    First you need to make sure your code can send the email successfully, then save the Message.body after sending the email.

    I think you can name the file according to the recipient's email id, or change it to your needs.

     Private Sub SaveMessage(ByVal message As MailMessage)
            Dim ReceiverEmailId As String = message.[To].ToString()
            Dim MessBody As String = message.Body
            Dim path As String = ReceiverEmailId & ".txt"
            File.WriteAllText(Server.MapPath(path), MessBody)
        End Sub
    

    ALL code

    Protected Sub btn_send_Click(ByVal sender As Object, ByVal e As EventArgs)
            Try
                Dim client As SmtpClient = New SmtpClient()
                client.UseDefaultCredentials = False
                client.Credentials = New System.Net.NetworkCredential("***", "***")
                client.Port = 587
                client.Host = "smtp.office365.com"
                client.DeliveryMethod = SmtpDeliveryMethod.Network
                client.EnableSsl = True
                Dim message As MailMessage = New MailMessage()
                message.From = New MailAddress("***")
                message.[To].Add(txtEmail.Text)
                message.Subject = txtSubject.Text
                message.Body = txtmessagebody.Text
                message.IsBodyHtml = True
                ' message.AlternateViews.Add(Mail_Body())
                client.Send(message)
                SaveMessage(message)
            Catch ex As Exception
                Response.Write("Failed")
            End Try
        End Sub
        Private Sub SaveMessage(ByVal message As MailMessage)
            Dim ReceiverEmailId As String = message.[To].ToString()
            Dim MessBody As String = message.Body
            Dim path As String = ReceiverEmailId & ".txt"
            File.WriteAllText(Server.MapPath(path), MessBody)
        End Sub
    

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 61,731 Reputation points
    2023-05-16T01:40:02.7033333+00:00

    normally you would use the messaged, but SimpleSMTP does not generate one. you will need to add one following the spec.

    https://www.rfc-editor.org/rfc/rfc2822

    code:

    var mailDomain = "mail.mydomain.com";
    var messageId = $"<{Guid.NewGuid().ToString()}@{mailDomain}>";
    _mailmsg.Headers.Add("Message-Id", messageId);
    

    note: not having a valid messageid can cause you mail to route to spam

    0 comments No comments

  2. peter liles 556 Reputation points
    2023-05-19T14:26:57.83+00:00

    Once i got the output as described i decided best to add data to a database table instead of creating numerous text files.

    seems good for now

    Thanks

    0 comments No comments