How to send an email with attachment in asp.net 2.0

Last post 10-06-2009 3:55 PM by jrstafford1. 9 replies.

Sort Posts:

  • How to send an email with attachment in asp.net 2.0

    07-30-2007, 10:09 PM
  • Re: How to send an email with attachment in asp.net 2.0

    07-31-2007, 12:08 AM
    Answer

     

    code to Send Mail:
    
    C#
    
    using System.Net;
    using System.Net.Mail;
    -----------------------
    
            public static bool SendMail(string strFrom, string strTo, string strSubject, string strMsg)
            {            
                try
                {                
                    // Create the mail message
                    MailMessage objMailMsg = new MailMessage(strFrom, strTo);
                    
                    objMailMsg.BodyEncoding = Encoding.UTF8;
                    objMailMsg.Subject = strSubject;
                    objMailMsg.Body = strMsg;
                    objMailMsg.Priority = MailPriority.High;
                    objMailMsg.IsBodyHtml = true;
    
                    //prepare to send mail via SMTP transport
                    SmtpClient objSMTPClient = new SmtpClient();
                    objSMTPClient.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
                    objSMTPClient.Send(objMailMsg);
                    return true;                
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }     
    
      
    VB:
    
    Public Shared Function SendMail(ByVal strFrom As String, ByVal strTo As String, ByVal strSubject As String, ByVal strMsg As String) As Boolean
    			Try
    				' Create the mail message
    				Dim objMailMsg As MailMessage = New MailMessage(strFrom, strTo)
    
    				objMailMsg.BodyEncoding = Encoding.UTF8
    				objMailMsg.Subject = strSubject
    				objMailMsg.Body = strMsg
    				objMailMsg.Priority = MailPriority.High
    				objMailMsg.IsBodyHtml = True
    
    				'prepare to send mail via SMTP transport
    				Dim objSMTPClient As SmtpClient = New SmtpClient()
    				objSMTPClient.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis
    				objSMTPClient.Send(objMailMsg)
    				Return True
    			Catch ex As Exception
    				Throw ex
    			End Try
    End Function
     
       
    Code to send mail with attechment.,

    public static bool SendMail(string strFrom, string strTo, string strSubject, string strMsg)
    {
    try { // Create the mail message MailMessage objMailMsg = new MailMessage(strFrom, strTo);

    objMailMsg.BodyEncoding = Encoding.UTF8;
    objMailMsg.Subject = strSubject;
    objMailMsg.Body = strMsg;
    Attachment at = new Attachment(Server.MapPath("~/Uploaded/txt.doc"));
    objMailMsg.Attachments.Add(at);
    objMailMsg.Priority = MailPriority.High;
    objMailMsg.IsBodyHtml = true;

    //prepare to send mail via SMTP transport SmtpClient objSMTPClient = new SmtpClient();
    objSMTPClient.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
    objSMTPClient.Send(objMailMsg);
    return true;
    }
    catch (Exception ex)
    {
    throw ex;
    }
    }

    hope it helps./.
     
    नमस्ते,
    [KaushaL] || BloG || MS MVP

    "I would love to change the world, but they won’t give me the source code"


    Don't forget to click "Mark as Answer" on the post that helped you.
    This credits that member, earns you a point and mark your thread as Resolved for the sake of Future Readers.
  • Re: How to send an email with attachment in asp.net 2.0

    08-07-2007, 6:03 AM
    • Member
      31 point Member
    • ronnieedin
    • Member since 08-03-2007, 3:42 AM
    • Posts 110

    is there maximum size of attachment

  • Re: How to send an email with attachment in asp.net 2.0

    09-15-2007, 5:59 PM
    • Participant
      1,471 point Participant
    • Stoian Bucovich
    • Member since 09-15-2007, 9:36 PM
    • Cabo San Lucas, Baja California Sur
    • Posts 231

    Here is how I use to send e-mail from the web feedback form (the code is  VB), and I am using server credentials:

     

    Imports System.Net.Mail

    Partial Class ContactsEnglish
        Inherits System.Web.UI.Page

        Protected Sub send_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles send.Click
            If Page.IsValid() Then
                Dim mySmtpClient As New SmtpClient
                Dim myMail As New MailMessage
                Dim myFrom As New MailAddress(email.Text)
                Dim myTo As New MailAddress("recipients_email@server.com")
                myMail.BodyEncoding = System.Text.Encoding.UTF8
                myMail.To.Add(myTo)
                myMail.From = myFrom
                myMail.Subject = "Mail's subject goes here"
                myMail.Body = vbCrLf & "Country: " & country.Text & vbCrLf & vbCrLf & "Name: " & name.Text & vbCrLf & vbCrLf & "Phone number: " & phone.Text & vbCrLf & vbCrLf & "E-mail: " & email.Text & vbCrLf & vbCrLf & "Inquiry: " & inquiry.Text
                mySmtpClient.Host = "www.YourMailServer.com"
                mySmtpClient.Credentials = New System.Net.NetworkCredential("YourMail@server.com", "YourMailPassword", "www.YourMailServer.com")
                Try
                    mySmtpClient.Send(myMail)
                    lbl.Text = "Your message has been sent. Thank you !"
                Catch ex As Exception
                    lbl.Text = "Sending failed !"
                End Try
            End If
        End Sub
    End Class

     

    P.S.

    This mail will  be sent in text format. The asp's controls that I am using  are 5 text boxes, button send and validation controls for the requred field.

    Hope it helps

    Regards

  • Re: How to send an email with attachment in asp.net 2.0

    10-05-2007, 4:43 AM
    • Member
      455 point Member
    • dotnet_lee
    • Member since 08-25-2006, 12:45 PM
    • UK
    • Posts 190

    Heres another one - In ASP.NET 2.0 using VB also attaching a file like shown above

     

        Public Shared Function SendMail(ByVal strFrom As String, ByVal strTo As String, ByVal strSubject As String, ByVal strMessage As String) As Boolean
    Try
    Dim MySmtpClient As New System.Net.Mail.SmtpClient("Your SMTP Server")
    MySmtpClient.Credentials = New System.Net.NetworkCredential("Your-username", "Your-password")
    Dim msg As New System.Net.Mail.MailMessage(strFrom, strTo, strSubject, strMessage)
    msg.BodyEncoding = Encoding.UTF8
    msg.IsBodyHtml = True
    msg.Priority = Net.Mail.MailPriority.High

    'Add an Attachment
    Dim at As New System.Net.Mail.Attachment(Current.Server.MapPath("~/Uploaded/txt.doc"))
    msg.Attachments.Add(at)

    MySmtpClient.Send(msg)
    Return True

    Catch ex As Exception
    Throw ex
    End Try
    End Function
      
  • Re: How to send an email with attachment in asp.net 2.0

    10-15-2007, 10:08 AM
    • Member
      77 point Member
    • handy01
    • Member since 07-24-2004, 5:44 AM
    • Posts 23
  • Re: How to send an email with attachment in asp.net 2.0

    09-19-2009, 11:35 AM
    • Member
      28 point Member
    • qwsoftdraw
    • Member since 02-22-2008, 8:14 PM
    • Posts 32

    Is there a way using ASP.net 2.0 and visual basic that a person can rename an attachment before it reaches the person your sending it to by email. For example, I want to send an email with an attachment from my website. I have been able to send an email with an attachment from a website successfully, but when I receive the email, the attachment not only has the original name of the file, but it also contains the name of the web path. For instance, if the file is Test.zip and it's located on a web server D:\Base\mywebname.com\TestFolder, then the zip file shows up in my email as: _Base_mywebname.com_TestFolder_Test.zip.

    I do not want the path name to be viewable by the person who receives the attachment by email. Is there a way to keep the path from showing up in the final attachment file name?

    Thanks in advance.

  • Re: How to send an email with attachment in asp.net 2.0

    09-19-2009, 1:13 PM
    • All-Star
      57,621 point All-Star
    • mudassarkhan
    • Member since 02-28-2008, 10:28 AM
    • Mumbai, India
    • Posts 10,201
    • TrustedFriends-MVPs
  • Re: How to send an email with attachment in asp.net 2.0

    09-21-2009, 3:14 PM
    • Member
      28 point Member
    • qwsoftdraw
    • Member since 02-22-2008, 8:14 PM
    • Posts 32

    Thank you for responding.

    For some reason now on my website when I use the same code I had been using, it sends just the name of the attached file. I have to wonder if my website company caught the error and corrected it themselves.

    Anyways...what I want done is working now.

    Trying the code you gave me just gave me Security Errors, but again, thank you for responding.

  • Re: How to send an email with attachment in asp.net 2.0

    10-06-2009, 3:55 PM
    • Member
      134 point Member
    • jrstafford1
    • Member since 09-14-2009, 4:56 PM
    • Midwest United States
    • Posts 45

    Thanks for the posts guys...this was very helpful!


Page 1 of 1 (10 items)