HOW TO: Send email using System.Net.Mail

Rate It (28)

Last post 08-01-2007 8:01 AM by Johnny be good. 173 replies.

Sort Posts:

  • Re: HOW TO: Send email using System.Net.Mail

    01-24-2007, 10:06 AM
    Locked
    • Member
      114 point Member
    • will9key
    • Member since 06-30-2006, 7:59 PM
    • Dallas, Texas
    • Posts 39

    Hi folks,

    This code works great for sending an email, but how would you add a file attachment t the email? I've tinkered around with the mailhelper class but can't get it to work. Any suggestions?

     

    Thanks

    Will

  • Re: HOW TO: Send email using System.Net.Mail

    01-24-2007, 10:37 AM
    Locked
    • Member
      114 point Member
    • will9key
    • Member since 06-30-2006, 7:59 PM
    • Dallas, Texas
    • Posts 39
    Cancel that - I found the answer here: http://www.systemnetmail.com/faq/2.3.aspx Big Smile
  • Re: HOW TO: Send email using System.Net.Mail

    01-28-2007, 10:57 AM
    Locked
    • Member
      35 point Member
    • mletsch
    • Member since 10-02-2006, 4:33 PM
    • Posts 10

    StrongTypes, I thank you for posting this.  I'm having trouble getting starting as I am new to development in general.


    I have tried several ways to call the function SendMailMessage with no success.  So I have a couple of questions and I would appreciate a response for a relative beginner if you have the time:

    1. I've placed your file in my app_code folder.  How does a page know to look for it there?  Is there a line that I need to add at the top of the page to tell it look in that file in the app_code folder?
    2. After trying unsuccessfully to call it from inside the app_code folder I gave up and tried copying it in a script inside the page, (I'm trying to use a button and the onclick="Mailhelper.SendMailMessage()" with the parameters filled in to call it). I then get the following error:

    Server Error in '/' Application.

    Parser Error

    Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.

    Parser Error Message: The server tag is not well formed.

    Source Error:

    Line 200:        <tr>
    Line 201: <td colspan="2">
    Line 202: <Club:RolloverButton ID="Newsletter" runat="server" Text="Submit" onclick="SendMailMessage("test@theirdomain","test@mydomain.com","","","Newsletter Request", "NewsletterName")"/>
    Line 203: </td>
    Line 204: </tr>

    Source File: /Join2.aspx    Line: 202


    Version Information: Microsoft .NET Framework Version:2.0.50727.42; ASP.NET Version:2.0.50727.210

     


    If anyone can give a couple of the more basic steps for using this I would greatly appreciate it, (and it looks like there are quite a few of the others who would too).

     

    Thanks so much!
     

  • Re: HOW TO: Send email using System.Net.Mail

    01-31-2007, 1:28 AM
    Locked
    • Member
      527 point Member
    • Ritesh Manglani
    • Member since 10-26-2006, 1:03 PM
    • Vadodara (Gujrat, India)
    • Posts 123
    Ez416:

    whats wrong? my error is

    Unable to read data from the transport connection: net_io_connectionclosed.

    i just copy the file then place it to APP_code folder

     

    I am getting the similar error. Any help on this.

     

    Thanks
    Ritesh

    The Biggest Fun of Life is in Doing Things People Say You Can Not Do.
  • Re: HOW TO: Send email using System.Net.Mail

    02-09-2007, 5:19 AM
    Locked
    • Member
      28 point Member
    • jazeelkm
    • Member since 01-11-2007, 12:31 PM
    • Posts 26

    thanx, nice code

    How can i send a word document(say abc.doc) as an attachament using System.Net.Mail

  • Re: HOW TO: Send email using System.Net.Mail

    02-09-2007, 3:08 PM
    Locked
    • All-Star
      30,698 point All-Star
    • StrongTypes
    • Member since 12-13-2005, 4:21 PM
    • California
    • Posts 6,007
    • ASPInsiders
      Moderator
      TrustedFriends-MVPs
    jazeelkm:

    thanx, nice code

    How can i send a word document(say abc.doc) as an attachament using System.Net.Mail

    See http://www.systemnetmail.com/faq/2.3.aspx.

  • Re: HOW TO: Send email using System.Net.Mail

    02-13-2007, 10:34 AM
    Locked

    Hi,

    I've got the following mail-related question:

    I've got a FormView for users to enter their data. That works fine. But I'd also like to send them a confirmation e-mail with the data they've just entered. How would I do this?

    I know how to create a Mail Message with simple textboxes. But in my example the FormView complicates matters! Can anyone help me? I'm getting quite frustrated... 

    Thanks a lot,

    Jerome

     

  • Re: HOW TO: Send email using System.Net.Mail

    02-14-2007, 8:57 PM
    Locked
    • Member
      27 point Member
    • Watchtower54
    • Member since 07-19-2006, 3:14 PM
    • Posts 6

    Hello all,

     I am getting the following error message.

    The specified string is not in the form required for an e-mail address.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.FormatException: The specified string is not in the form required for an e-mail address.

    Source Error:

    Line 34: 
    Line 35:         'Create the MailMessage object
    Line 36:         Dim msg As New System.Net.Mail.MailMessage()
    Line 37: 
    Line 38:         'Assign from address

    Source File: C:\Inetpub\wwwroot\contact.aspx.vb    Line: 36

    Stack Trace:

    [FormatException: The specified string is not in the form required for an e-mail address.]
       System.Net.Mime.MailBnfHelper.ReadMailAddress(String data, Int32& offset, String& displayName) +872403
       System.Net.Mail.MailAddress.ParseValue(String address) +245
       System.Net.Mail.MailAddress..ctor(String address, String displayName, Encoding displayNameEncoding) +87
       System.Net.Mail.MailMessage..ctor() +271
       contact.btnSubmit_Click(Object sender, EventArgs e) in C:\Inetpub\wwwroot\contact.aspx.vb:36
       System.Web.UI.WebControls.Button.OnClick(EventArgs e) +105
       System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +107
       System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
       System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
       System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5102
    

    The code that I am using is as follows:

    Imports System.Net.Mail

    Partial

    Class contact

    Inherits System.Web.UI.Page

    Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click

    Dim FromName As String = "Homewares Homesite"

    Dim FromEmail As String = "fizzy@homesite.org"

    Dim Subject As String = "Website Submission"

    Dim ToName As String = "M Fizzy"

    Dim ToEmail As String = booboo@homesite.org

    ''This is the series of variables that will contain the data from the form

    Dim aSpace, body, name, phone, email, model, comments

    name = txtName.Text

    phone = txtPhone.Text

    email = txtEmail.Text

    model = drpModel.SelectedValue

    comments = txtComments.Text

    aSpace =

    ""

    body =

    "At " + DateTime.Now + " a question was sent from the Housewares Homesite." & _

    " Below you will find the message sent by " & name & vbCrLf & _

    aSpace & vbCrLf & _

    "------" & vbCrLf & _

    "Name: " & name & vbCrLf & _

    "Phone: " & phone & vbCrLf & _

    "Email: " & email & vbCrLf & _

    "Model they were interested in most: " & model & vbCrLf & _

    "Comments: " & comments

     

    ''Create the SmtpClient object (assumes using localhost)

    Dim smtp As New System.Net.Mail.SmtpClient("mail.techspec.org")

    'Create the MailMessage object

    Dim msg As New System.Net.Mail.MailMessage()

    'Assign from address

    msg.From =

    New System.Net.Mail.MailAddress(FromEmail, FromName)

    'Assign to address

    msg.To.Add(

    New System.Net.Mail.MailAddress(ToEmail, ToName))

    'assign subject, and body

    msg.Subject = Subject

    msg.Body = body

    'Send the message with SmtpClient

    smtp.Send(msg)

    ''Now that the message has been sent, we'll clear out the contents of the form and render it invisible for the "Goodbye" div tag.

    name =

    ""

    phone =

    ""

    email =

    ""

    model =

    ""

    comments =

    ""

    contactForm.Visible =

    False

    goodBye.Visible =

    True

    End Sub

    End

    Class

    My web.config file is configured as such:

      <mailSettings>
       <smtp from="P Fizzy">
        <network host="smtp.homesite.org" password="poweroverwhelming" userName=fizzy@homesite.org />
       </smtp>
      </mailSettings>

    Somethings to keep in mind:

    1.  I've used this code before on another server, and it ran without any problems.
    2. The server that I am currently using is a Merak Mail Server, which I'm sure has some role to play in this.

    For obivous reasons, I have replaced the domain names and such with fictious names.

    Any advice or thoughts would be greatly appreciated, so me and my IT guy can get this page up and running.

  • Re: HOW TO: Send email using System.Net.Mail

    02-15-2007, 9:09 PM
    Locked
    • Member
      64 point Member
    • jcjcjc
    • Member since 11-14-2006, 4:17 AM
    • Posts 639
    thanks for your posting , it's very useful Big Smile
  • Re: HOW TO: Send email using System.Net.Mail

    04-04-2007, 6:21 PM
    Locked
    • Member
      600 point Member
    • wizbay
    • Member since 06-09-2006, 6:58 PM
    • Posts 165

    This is a great emailer.

    It works just fine on my local machine but once I upload the codes to hosting account(godaddy.com, asp 2.0), the application doesn't send email.

    I used try catch on MailHelper.SendMailMessage("fromAddress@yourdomain.com", "toAddress@yourdomain.com", "bccAddress@yourdomain.com", "ccAddress@yourdomain.com", "Sample Subject", "Sample body of text for mail message"),

    It doesn't catch any error but it also doesn't send email. What could be the problem?

  • Re: HOW TO: Send email using System.Net.Mail

    04-13-2007, 6:50 AM
    Locked
    • Member
      2 point Member
    • melihutkan
    • Member since 03-11-2007, 10:40 PM
    • Posts 3
    I am trying to send email by using System.net.mail namespace.
    I do not have problem with sending email. I can clearly send emails.
    But what i also want is that get a notificaton about the mail address
    if the destination mail address is unavailible or the destination mail box is full.
    My code must notice this while sending mail.
    In my aplication,if i send an email to an address which is unavailible or whose mail box is full,the mail is sent
    without any notification.But it is clear that it is not sent.

    Mail server does not send anything about this notification. It accepts the mail for delivery.
    After a while it sends a mail-failure mail. But it is nonsense for my applicaton. I have
    to learn that at the same time while sending the mail.


    This is a piece of code that i use.



    ...
               
    client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
                
    client.SendAsync(message, message.To.ToString());

    ....


            private static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
            {
                
                String token = (string)e.UserState;

                if (e.Cancelled)
                {
                    writeToFile(token + " Send canceled.");
                }
                if (e.Error != null)
                {
                    writeToFile(token + "\nerror : " + e.Error.ToString());
                }
                else
                {
                    writeToFile("Message sent.");
                }
                
                
            }






    Dou you have any idea about that?
  • Re: HOW TO: Send email using System.Net.Mail

    04-13-2007, 6:31 PM
    Locked
    • Member
      198 point Member
    • dotnetguts
    • Member since 03-08-2007, 12:42 PM
    • ~~ DailyFreeCode.com ~~
    • Posts 87
    Filed under: , ,
  • Re: HOW TO: Send email using System.Net.Mail

    04-16-2007, 7:58 AM
    Locked
    • Member
      24 point Member
    • labgj
    • Member since 10-31-2005, 12:00 PM
    • Posts 2

    From your code there is no reference to the instance. Try the following:

     dim _SendMail as new MailHelper

    _SendMail.SendMailMessage("fromAddress@yourdomain.com", "toAddress@yourdomain.com", "bccAddress@yourdomain.com", "ccAddress@yourdomain.com", "Sample Subject", "Sample body of text for mail message")

     

    If you do not use a class and want to reference directly in the ascx or aspx file you can use the below code:

    Imports

    System.Net.Mail

     Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)

    SendMailMessage(_From, _To, _Subject, _Body)

    End Sub

    Public

    Shared Sub SendMailMessage(ByVal from As String, ByVal recepient As String, ByVal subject As String, ByVal body As String)

    Dim mMailMessage As New MailMessage()

    mMailMessage.From =

    New MailAddress(from)

    mMailMessage.To.Add(

    New MailAddress(recepient))

    mMailMessage.Subject = subject

    mMailMessage.Body = body

    mMailMessage.IsBodyHtml =

    True

    mMailMessage.Priority = MailPriority.Normal

    mMailMessage.Attachments.Add(New Attachment("C:\Email\RFC.pdf"))

    Dim mSmtpClient As New SmtpClient()

    mSmtpClient.Host =

    "Your SMTP Server"

    mSmtpClient.Send(mMailMessage)

    End Sub
  • Re: HOW TO: Send email using System.Net.Mail with a form or grid view as a body

    04-20-2007, 3:36 AM
    Locked
    • Member
      37 point Member
    • hishambon
    • Member since 07-04-2006, 3:59 AM
    • Posts 9

    Hi,

    My system ask for me to send a gridview or a form as a body, i tried so much way and nutuil now i cannot found any solution.

    do you have any idea for to do it.

    thanks

  • Re: HOW TO: Send email using System.Net.Mail

    04-20-2007, 5:26 AM
    Locked
    • Member
      37 point Member
    • hishambon
    • Member since 07-04-2006, 3:59 AM
    • Posts 9

    Hi

    i tried to use a formview or grid view as a body for the email.

    As a result, i cannot find any way to make it happen.

    Do you have a sugestion acording to my problem.

    Thank oyu very much

Page 10 of 12 (174 items) « First ... < Previous 8 9 10 11 12 Next >