asp.net, c#, attachment

Webboy2008
Webboy2008 used Ask the Experts™
on
I have the following codes working fine. But now I need to add attachment parameters.
Based on the following codes, can you show me how to add attachment parameters?
and how to call the function on aspx?

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.Configuration;
using System.Net.Mail;
using System.IO;
/// <summary>
/// Summary description for EmailGenerator
/// </summary>
public class EmailGenerator
{
    private string mFromEmail;
    private string mToEmail;
    private string mSubject;
    private string mBody;
    private string mFooter;
    public string FromEmail
    {
        get { return mFromEmail; }
        set { mFromEmail = value; }
    }
    public string ToEmail
    {
        get { return mToEmail; }
        set { mToEmail = value; }
    }
    public string Subject
    {
        get { return mSubject; }
        set { mSubject = value; }
    }
    public string Body
    {
        get { return mBody; }
        set { mBody = value; }
    }
    public string Footer
    {
        get { return mFooter; }
        set { mFooter = value; }
    }
    public string SendeMail()
    {
        try
        {
            MailMessage MessageInfo = new MailMessage(mFromEmail, mToEmail, mSubject, mBody);
            MessageInfo.IsBodyHtml = true;
            SmtpClient EmailClient = new SmtpClient("mail.ttt.com");
            System.Net.NetworkCredential SMTPUserInfo = new System.Net.NetworkCredential("tttt.com", "tttt");
            EmailClient.UseDefaultCredentials = false;
            EmailClient.Credentials = SMTPUserInfo;
            EmailClient.Send(MessageInfo);
            return "Send";
        }
        catch
        {
            return "Unable to send email";
        }

    }
}
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
kaufmedGlanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
MailMessage has an Attachments collection. You can find an example of how to use it at the bottom of the page I linked.
string fileName = "filename.txt";
FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
StreamReader s = new StreamReader(fs);
ContentType ct = new ContentType(MediaTypeNames.Text.Plain);
Attachment data = new Attachment(fs, ct);
MessageInfo.Attachments.Add(data);

Open in new window


You will need to adjust the MediaTypeName to suit as well as the filename

Author

Commented:
Can you merge your codes to my codes?
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

you may simply paste it into any part of your try/catch block prior to the mail being sent.
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.Configuration;
using System.Net.Mail;
using System.Net.Mime;
using System.IO;
/// <summary>
/// Summary description for EmailGenerator
/// </summary>
public class EmailGenerator
{
    private string mFromEmail;
    private string mToEmail;
    private string mSubject;
    private string mBody;
    private string mFooter;
    private string mAttachment;
    private MediaTypeNames mMimeType;
    public string FromEmail
    {
        get { return mFromEmail; }
        set { mFromEmail = value; }
    }
    public string ToEmail
    {
        get { return mToEmail; }
        set { mToEmail = value; }
    }
    public string Subject
    {
        get { return mSubject; }
        set { mSubject = value; }
    }
    public string Body
    {
        get { return mBody; }
        set { mBody = value; }
    }
    public string Footer
    {
        get { return mFooter; }
        set { mFooter = value; }
    }
    public string Attachment
    {
        get { return mAttachment; }
        set { mAttachment = value; }
    }
    public MediaTypeNames MimeType
    {
        get { return mMimeType; }
        set { mMimeType = value; }
    }
    public 
    public string SendeMail()
    {
        try
        {
            MailMessage MessageInfo = new MailMessage(mFromEmail, mToEmail, mSubject, mBody);
            MessageInfo.IsBodyHtml = true;
            SmtpClient EmailClient = new SmtpClient("mail.ttt.com");
            System.Net.NetworkCredential SMTPUserInfo = new  System.Net.NetworkCredential("tttt.com", "tttt");
            EmailClient.UseDefaultCredentials = false;
            EmailClient.Credentials = SMTPUserInfo;
            FileStream fs = new FileStream(mAttachment, FileMode.Open, FileAccess.Read);
            StreamReader s = new StreamReader(fs);
            ContentType ct = new ContentType(mMimeType);
            Attachment data = new Attachment(fs, ct);
            MessageInfo.Attachments.Add(data);
            EmailClient.Send(MessageInfo);
            return "Send";
        }
        catch
        {
            return "Unable to send email";
        }

    }
}

Open in new window

Anuradha GoliSystems Development / Support Specialist

Commented:
Import System.Web.Mail 

              Dim message As MailMessage
              message = New MailMessage ()
              message. To = "receiverID@domainName.Com"
              message. From = "sender@domainName.com"
              message. Subject = "Email Subject"
              message.BodyFormat = MailFormat.Html
              msgMail.Body = "<html><body><h2 " + 
                             "align = center>Hello World! " + 
                             "</h2></body></html>";

  MailAttachment attachment1 =
   new MailAttachment (@"c:\My Documents\OfficeFile1.doc");
  // Add Another One...
  MailAttachment attachment2 = 
   new MailAttachment ("d:\\Documents\\asp.netTurorial.doc");
  message.Attachments.Add (attachment1);
  message.Attachments.Add (attachment2);
 

              SmtpMail.Send (message)
              response. Write ("<BR><font color=red" + 
                               " face=verdana size=2> " + 
                               "Sent the mails. </font>")

Open in new window

Author

Commented:
I switch the codes into wcf. and it actually alert me about the using System.Net.Mime

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Net.Mail;
using System.IO;

// NOTE: If you change the class name "EmailService" here, you must also update the reference to "EmailService" in Web.config.
public class EmailService : IEmailService
{    
    #region IEmailService Members
    public string SendeMail(string mFromEmail, string mToEmail, string mSubject, string mBody, string mFooter)
    {
        string returnMSG = "";
        try
        {
            MailMessage MessageInfo = new MailMessage(mFromEmail, mToEmail, mSubject, mBody);
            MessageInfo.IsBodyHtml = true;
            SmtpClient EmailClient = new SmtpClient("mail.xxx.com");
            System.Net.NetworkCredential SMTPUserInfo = new System.Net.NetworkCredential("xxx", "xxx");
            EmailClient.UseDefaultCredentials = false;
            EmailClient.Credentials = SMTPUserInfo;
            EmailClient.Send(MessageInfo);
            returnMSG = "Sent";
        }
        catch (Exception e)
        {
            returnMSG = "Unable to send email. ";
        }

        return returnMSG;
    }
    #endregion
}
Just remove the system.net.mime, and remove the contenttype code.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial