Cannot include an Attachment via and Email Form

Discussion in 'ASP.NET / ASP.NET Core' started by saihati, Jun 7, 2011.

  1. Gents,
    Can we modify this code to include the attachment via a web form?



    <%@ Page Language="VB" %>
    <%@ Import Namespace="System.Net.Mail" %>
    <script runat="server">
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
    Dim strFrom = "[email protected]"
    Dim strTo = "[email protected]"
    Dim MailMsg As New MailMessage(New MailAddress(strFrom.Trim()), New MailAddress(strTo))
    MailMsg.BodyEncoding = Encoding.Default
    MailMsg.Subject = "Subject"
    MailMsg.Body = "This is a sample message"
    MailMsg.Priority = MailPriority.High
    MailMsg.IsBodyHtml = True
    'Smtpclient to send the mail message
    Dim SmtpMail As New SmtpClient
    SmtpMail.Host = "localhost"
    SmtpMail.Send(MailMsg)
    lblMessage.Text = "Mail Sent"
    End Sub
    </script>
    <html>
    <body>
    <form runat="server">
    <asp:Label id="lblMessage" runat="server"></asp:Label>
    </form>
    </body>
    </html>



    Thanks.
     
  2. RayH

    RayH DiscountASP.NET Lackey DiscountASP.NET Staff

    You just need to add this code to send an attachment:

    Dim Attach As New Attachment("PathtoFile")
    MailMsg.Attachments.Add(Attach)

    You can use the Server Path found in your Control Panel as a starting point. It maps to the root of your hosting account and you can add additional directories if needed.
     
  3. I managed to do it on the same day of the post but I did not update you guys.

    Anyways, thanks indeed.
     
  4. mjp

    mjp

    Glad you worked it out.
     

Share This Page