Page view counter

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

Last post 10-15-2007 10:08 AM by handy01. 5 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./.
     
    Thanx,
    [KaushaL] || BloG || Microsoft 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
    • Loading...
    • ronnieedin
    • Joined on 08-03-2007, 3:42 AM
    • Posts 110
    • Points 31

    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
    • Loading...
    • Stoian Bucovich
    • Joined on 09-15-2007, 9:36 PM
    • Sofia, Bulgaria
    • Posts 229
    • Points 1,457

    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
    • Loading...
    • dotnet_lee
    • Joined on 08-25-2006, 12:45 PM
    • UK
    • Posts 190
    • Points 455

    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
    • Loading...
    • handy01
    • Joined on 07-24-2004, 1:44 AM
    • Posts 23
    • Points 77
Page 1 of 1 (6 items)