Email

Discussion in 'ASP .Net Web Controls' started by Morris Neuman, Jan 16, 2009.

  1. Hi,

    I want to have a function where the user can enter an email address and then
    send email with a selected file as an attachment.

    Can you show me any walkthroughs or document where this is done?
     
    Morris Neuman, Jan 16, 2009
    #1
    1. Advertisements

  2. Do you know how to send an email in ASP.NET at all, or is it only the
    attaching a file that you need help with? The two main classes you will need
    to deal with are:

    System.Net.Mail.SmtpClient
    System.Net.Mail.MailMessage

    All you need to do with the SmtpClient is pass it a String in the
    constructor specifying the SMTP server. Most of the properties of
    MailMessage should be pretty self-explanatory. To add a file to the
    MailMessage as an attachment, use the Add method of the Attachments
    property. Once you have set all the desired properties of the MailMessage,
    use a statement similar to the following to send it:

    yoursmtp.Send(yourmailmsg)

    Hopefully this helps!
     
    Nathan Sokalski, Jan 16, 2009
    #2
    1. Advertisements

  3. Morris Neuman

    Steven Cheng Guest

    Hi Morris,

    As for sending email in ASP.NET web page(with attachment), I think you need
    to implement such a web page via the following approach:

    ** Your page will need some FileUpload controls to let user first add some
    attachments. These operation will post back to server so as to save all the
    attachments to a temp folder on server(for later email sending use). For
    ASP.NET file uploading, you can refer to the below article:

    #File Upload with ASP.NET
    http://www.codeproject.com/KB/aspnet/fileupload.aspx

    ** After you've file uploading functionality on page, you can add code to
    send email. In .net 2.0, you can use Sysetem.Net.Mail classes to send
    email(also support add attachments). There are many samples here for your
    reference:

    #Sending Email with System.Net.Mail
    http://weblogs.asp.net/scottgu/archive/2005/12/10/432854.aspx

    http://aspnet.4guysfromrolla.com/articles/072606-1.aspx



    And I've also found a code project article which combine the above two
    parts. I think it is quite close to what you want.

    #ASP.NET email with multiple attachments
    http://www.codeproject.com/KB/aspnet/ASPNETwebmail.aspx

    Sincerely,

    Steven Cheng

    Microsoft MSDN Online Support Lead


    Delighting our customers is our #1 priority. We welcome your comments and
    suggestions about how we can improve the support we provide to you. Please
    feel free to let my manager know what you think of the level of service
    provided. You can send feedback directly to my manager at:
    .

    ==================================================
    Get notification to my posts through email? Please refer to
    http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

    Note: MSDN Managed Newsgroup support offering is for non-urgent issues
    where an initial response from the community or a Microsoft Support
    Engineer within 2 business day is acceptable. Please note that each follow
    up response may take approximately 2 business days as the support
    professional working with you may need further investigation to reach the
    most efficient resolution. The offering is not appropriate for situations
    that require urgent, real-time or phone-based interactions. Issues of this
    nature are best handled working with a dedicated Microsoft Support Engineer
    by contacting Microsoft Customer Support Services (CSS) at
    http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
    ==================================================
    This posting is provided "AS IS" with no warranties, and confers no rights.



    --------------------
     
    Steven Cheng, Jan 16, 2009
    #3
  4. Thanks. Got the basics of the email working.

    One question:
    Is there a way the user enter several email addresses in textbox1, each
    address seperated by a semicolon, set EmailTo = textbox1.Text and then add
    entire string to message.To.Add(new MailAddress(EmailTo1))?

    I get error doing this and have to parse the textbox1.Text string and do an
    message.To.Add(new...) for each address.
    --
    Thanks
    Morris


     
    Morris Neuman, Jan 18, 2009
    #4
  5. If you separate the addresses by a comma rather than a semicolon you can
    simply do the following:

    message.To.Add(textbox1.Text)

    If the addresses are separated by a semicolon, you can simply modify this
    statement by using the String.Replace() method as follows:

    message.To.Add(textbox1.Text.Replace(";",","))

    There is no need to manually parse the addresses (although this wouldn't be
    that hard using the String.Split() method and a simple loop). Hopefully this
    helps.
    --
    Nathan Sokalski

    http://www.nathansokalski.com/

     
    Nathan Sokalski, Jan 19, 2009
    #5
  6. Morris Neuman

    Steven Cheng Guest

    Thanks for Nathan's input.

    Nice solution. By using the Replace method, we can avoid parsing the multi
    address (via split) and join them again with the new separator.

    Regards,

    Sincerely,

    Steven Cheng

    Microsoft MSDN Online Support Lead


    Delighting our customers is our #1 priority. We welcome your comments and
    suggestions about how we can improve the support we provide to you. Please
    feel free to let my manager know what you think of the level of service
    provided. You can send feedback directly to my manager at:
    .



    --------------------
     
    Steven Cheng, Jan 19, 2009
    #6
  7. Thanks for the response. I tried your suggestion and entered in multiple
    email addresses followed by comma, I even tried with and with a space after
    the comma.
    However in both cases, the first recipient receives the email but the second
    one does not.
    In debugging I do see that Textbox1.Text = ",
    ", and the message To Count =1 showing just the first
    email address.

    When the first recipient gets the message, the to address in the email
    received only has the 1 email address.

    Is there something else I have to do to allow user to input multiple email
    address and the send to all in the input list?
     
    Morris Neuman, Jan 19, 2009
    #7
  8. Morris Neuman

    Steven Cheng Guest

    Thanks for your reply Morris,

    Then, I think we'd better still use the stereotype code to add multiple
    receivers:

    =============================
    public static void SendMail()
    {
    string receivers =
    ";;";

    MailMessage msg = new MailMessage();
    msg.From = new MailAddress("");

    foreach (string rec in receivers.Split(';'))
    {
    msg.To.Add(rec);
    }

    msg.Subject = "This is just a test message!";
    msg.Body = "Sorry for the inconvenience.";

    SmtpClient client = new SmtpClient("smtphost");
    client.UseDefaultCredentials = true;
    client.Send(msg);
    }
    ==============================

    http://www.systemnetmail.com/faq/3.2.3.aspx

    Hope this helps.

    Sincerely,

    Steven Cheng

    Microsoft MSDN Online Support Lead


    Delighting our customers is our #1 priority. We welcome your comments and
    suggestions about how we can improve the support we provide to you. Please
    feel free to let my manager know what you think of the level of service
    provided. You can send feedback directly to my manager at:
    .


    --------------------
     
    Steven Cheng, Jan 20, 2009
    #8
  9. Great this worked. Thanks.

    --
    Thanks
    Morris


     
    Morris Neuman, Jan 20, 2009
    #9
  10. Morris Neuman

    Steven Cheng Guest

    You're welcome Morris,

    If there is anything else we can help, please feel free to post here.

    Sincerely,

    Steven Cheng

    Microsoft MSDN Online Support Lead


    Delighting our customers is our #1 priority. We welcome your comments and
    suggestions about how we can improve the support we provide to you. Please
    feel free to let my manager know what you think of the level of service
    provided. You can send feedback directly to my manager at:
    .

    --------------------
     
    Steven Cheng, Jan 21, 2009
    #10
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.