asp.net, wfc, attachment

Webboy2008
Webboy2008 used Ask the Experts™
on
I have the following wcf codes working fine. but now want to add attachment parameter there. basically doing if the attachment is existed, then put the attachment there with email and send out. Simple enough as any other email.

Any ideas how to do that?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Net.Mail;
using System.Net.Mime;
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.aaa.com");
            System.Net.NetworkCredential SMTPUserInfo = new System.Net.NetworkCredential("aaa@aaa.com", "aaa");
            EmailClient.UseDefaultCredentials = false;
            EmailClient.Credentials = SMTPUserInfo;
             EmailClient.Send(MessageInfo);
            returnMSG = "Sent";
        }
        catch (Exception e)
        {
            returnMSG = "Unable to send email. ";
        }

        return returnMSG;
    }
    #endregion
}
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Senior .Net Developer
Commented:
http://netprogramminghelp.com/aspnet/how-to-send-a-mail-with-attachment-using-aspnet/

change this:
  public string SendeMail(string mFromEmail, string mToEmail, string mSubject, string mBody, string mFooter)


to:

  public string SendeMail(string mFromEmail, string mToEmail, string mSubject, string mBody, string mFooter, Attachment file= null)


add:
if (file != null)
  MessageInfo.Attachments.Add(file);

Author

Commented:
It alerts Attachment file= null. and how to call the services?

I have the following and get alert as well

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ServiceReference1;

public partial class _Default : System.Web.UI.Page
{
    ServiceReference1.EmailServiceClient objemail = new EmailServiceClient();
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Write(objemail.SendeMail("xxx@yahoo.com", "xxx@yahoo.com", "Test", "Test", "Test",""));
    }
}
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

Kyle AbrahamsSenior .Net Developer

Commented:
send null:
Response.Write(objemail.SendeMail("xxx@yahoo.com", "xxx@yahoo.com", "Test", "Test", "Test", null));

Author

Commented:
It alerts Attachment file= null in wcf side
Kyle AbrahamsSenior .Net Developer

Commented:
At what point?

Can you post the new code, indicating where you get the errror?

The other way is to create two seperate functions, one where you call it with the attachment and one without.

Author

Commented:
Response.Write(objemail.SendeMail("xxx@yahoo.com", "xxx@yahoo.com", "Test", "Test", "Test",null));       - This one works


how to test with attachment?
create an instance of attachment like this:-
Attachment data = new Attachment(file, MediaTypeNames.Application.Octet);
			// Add time stamp information for the file.
			ContentDisposition disposition = data.ContentDisposition;
			disposition.CreationDate = System.IO.File.GetCreationTime(file);
			disposition.ModificationDate = System.IO.File.GetLastWriteTime(file);
			disposition.ReadDate = System.IO.File.GetLastAccessTime(file);

Response.Write(objemail.SendeMail("xxx@yahoo.com", "xxx@yahoo.com", "Test", "Test", "Test",data)); 

Open in new window

Author

Commented:
the email go thought with attachment, but i don't see the attachment.

protected void Button1_Click(object sender, EventArgs e)
    {
        Attachment data = new Attachment(@"C:\inetpub\wwwroot\iapp\PDFTemplates\test.pdf", MediaTypeNames.Application.Octet);
        // Add time stamp information for the file.
        ContentDisposition disposition = data.ContentDisposition;
        disposition.CreationDate = System.IO.File.GetCreationTime(@"C:\inetpub\wwwroot\iapp\PDFTemplates\test.pdf");
        disposition.ModificationDate = System.IO.File.GetLastWriteTime(@"C:\inetpub\wwwroot\iapp\PDFTemplates\test.pdf");
        disposition.ReadDate = System.IO.File.GetLastAccessTime(@"C:\inetpub\wwwroot\iapp\PDFTemplates\test.pdf");
        Response.Write(objemail.SendeMail("xxx@yahoo.com", "xxx@yahoo.com", "Test", "Test", "Test", data));
    }
Kyle AbrahamsSenior .Net Developer

Commented:
try Server.MapPath("~\PDFTemplates\test.pdf");

http://msdn.microsoft.com/en-us/library/ms524632(v=vs.90).aspx

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