HaBo
HaBo

Reputation: 14297

Send email as Attachment

How can i send my html email content as an attachment of any format.

I am using a FAX API, that allows me to send an attachment with respective Fax Number. And the API will Fax that Attachment to the Fax Number provided.

here is my code

 public string SendFax(int ID)
    {
        MailMessage message = new MailMessage();
        //To address 
        message.To.Add(new MailAddress("[email protected]"));
        message.Subject = "My Subject"; 
        //Specify true if it is html message
        message.IsBodyHtml = true;

        Class Details = new Class();
        Details = Details.GetDetails(ID);

        string mailBody = File.ReadAllText(System.Web.HttpContext.Current.Server.MapPath("~/Documents/EmailTemplates/FaxTemplate.htm"));

        //Prescriber details.
        mailBody = mailBody.Replace("%FirstName%", Details.FirstName);
        mailBody = mailBody.Replace("%LastName%", Details.LastName);
        mailBody = mailBody.Replace("%OfficeName%", Details.PracticeName);
        mailBody = mailBody.Replace("%AddressLIne1%", Details.AddressLine1);
        mailBody = mailBody.Replace("%Phone%", Details.Phone);
        mailBody = mailBody.Replace("%FaxNumber%", Details.Fax);
        mailBody = mailBody.Replace("%DEANumber%", Details.DEANUmber);

        message.Body = mailBody;

        status = Send(message);
        if (status.Equals(string.Empty))
            status = "Failure sending Fax" + "|" + "0";
        else
            status = "Successfully Faxed";
        return status;

    }


 public string Send(MailMessage message)
    {
        string host = WebConfigurationManager.AppSettings["SMTPHost"].ToString();
        int port = Convert.ToInt32(WebConfigurationManager.AppSettings["SMTPPort"]);
        string username = WebConfigurationManager.AppSettings["SMTPUsername"].ToString();
        string password = WebConfigurationManager.AppSettings["SMTPPwd"].ToString();
        string fromAddress = WebConfigurationManager.AppSettings["EmailFrom"].ToString();

        SmtpClient smtpC = new SmtpClient();
        smtpC.Host = host;
        smtpC.Port = port;
        //smtpC.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
        smtpC.Credentials = new System.Net.NetworkCredential(username, password);
        smtpC.EnableSsl = false;
        message.From = new MailAddress(fromAddress);
        smtpC.Send(message);
        status = "Success";
        return status;
    }

Upvotes: 0

Views: 813

Answers (1)

Dmitry Savy
Dmitry Savy

Reputation: 1067

Try something like this

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net.Mail;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSend_Click(object sender, EventArgs e)
    {
        MailMessage mail = new MailMessage();
        mail.To.Add(txtTo.Text);
        mail.From = new MailAddress(txtFrom.Text);
        mail.Subject = txtSubject.Text;
        mail.Body = txtMessage.Text;
        mail.IsBodyHtml = true;

        //Attach file using FileUpload Control and put the file in memory stream
        if (FileUpload1.HasFile)
        {
            mail.Attachments.Add(new Attachment(FileUpload1.PostedFile.InputStream, FileUpload1.FileName));
        }
        SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
        smtp.Credentials = new System.Net.NetworkCredential
             ("[email protected]", "YourGmailPassword");
        //Or your Smtp Email ID and Password
        smtp.EnableSsl = true;
        smtp.Send(mail);

    }
}

If you want to add stream of your data to MailMessage try this

System.IO.MemoryStream ms = new System.IO.MemoryStream();
System.IO.StreamWriter writer = new System.IO.StreamWriter(ms);
writer.Write("Hello its my sample file");
writer.Flush();
writer.Dispose();

System.Net.Mime.ContentType ct = new System.Net.Mime.ContentType(System.Net.Mime.MediaTypeNames.Text.Plain);
System.Net.Mail.Attachment attach = new System.Net.Mail.Attachment(ms, ct);
attach.ContentDisposition.FileName = "myFile.txt";

// I guess you know how to send email with an attachment
// after sending email
ms.Close();

Upvotes: 1

Related Questions