2

In my Asp.net Website I create New users and what I have to do is send email to the user from the website with their Login details. I created a webservices to send email and it works fine. I want to add some HTML stylings to the Email I send to the user.

So I have created a HTML email format and have saved it as a HTML page in a folder(resource) Inside the Project. How can I read the HTML page from the resource folder and attach it with the below code in "BODY" argument and send it to the webservices method. And Also how can I edit the html file so that I will include the user login details to it before sending it to the webservice. Thanks

C# Code to pass value to the webservice method to send email

       string subject = "login details";
       //call the webservice to send email to the newly created user
       ServiceClient service = new ServiceClient();
       service.sendEmail(newuseremail, subject, BODY, message, myemail);
Mark
  • 2,720
  • 15
  • 56
  • 87
  • What version of .NET . Sending email in .NET 1.1 is vastly different than 2.0-4.0. – Ryan Ternier Nov 04 '11 at 16:52
  • 1
    possible duplicate of [Can I set up HTML/Email Templates with ASP.NET?](http://stackoverflow.com/questions/620265/can-i-set-up-html-email-templates-with-asp-net) – Damith Nov 04 '11 at 17:06

4 Answers4

3

For how to read html page - there is nothing special, you must approach it as a simple text file and read using StreamReader for example. To be able to make some changes in the content, consider using some pattern, which may not appear in the html page elsewhere, something like: "$$USER$$" and after reading the file, replace such occurances with User name, or whatever you'd like it to be replaced by. To be able to send html emails from the service, you have to set a property (if not mistaken IsBodyHtml) to true of the MailMessage instance.

And that's all.

Artak
  • 2,819
  • 20
  • 31
1

You want to read the HTML file in as a string. I assume you have access to it locally.

string strHTML=File.ReadAllText("myfile.html");

This is also a duplicate of: Send a email with a HTML file as body (C#)

Community
  • 1
  • 1
Ryan Ternier
  • 8,714
  • 4
  • 46
  • 69
0

Here's a class that wraps everything you need:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Mail;

/// <summary>
/// Wrapper class for the System.Net.Mail objects
/// </summary>
public class SmtpMailMessage : IDisposable
{
    #region declarations

    MailMessage Message;
    SmtpClient SmtpMailClient;

    #endregion

    #region constructors

    /// <summary>
    /// Default constructor for the SmtpMailMessage class
    /// </summary>
    public SmtpMailMessage()
    {
        //initialize the mail message
        Message = new MailMessage();
        Message.Priority = MailPriority.Normal;
        Message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;            
        Message.From = new MailAddress("xxx@abc.com");           

        //initialize the smtp client
        SmtpMailClient = new SmtpClient();
        SmtpMailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
        SmtpMailClient.Host = "192.168.0.1";
        SmtpMailClient.Port = 25;
    }

    /// <summary>
    /// Parameterized constructor for the SmtpMailMessage class. Allows for override of the default
    /// SMTP host and port number
    /// </summary>
    /// <param name="HostIP">The IP address of the exchange server</param>
    /// <param name="PortNumber">The port number for ingoing and outgoing SMTP messages</param>
    public SmtpMailMessage(string HostIP, int PortNumber) : this()
    {
        //override the smtp host value
        SmtpMailClient.Host = HostIP;

        //override the smtp port value
        SmtpMailClient.Port = PortNumber;
    }

    #endregion

    #region subject / body

    /// <summary>
    /// The body content of the mail message
    /// </summary>
    public string Body
    {
        get
        {
            return Message.Body;
        }
        set
        {
            Message.Body = value;
        }
    }

    /// <summary>
    /// the subject of the mail message
    /// </summary>
    public string Subject
    {
        get
        {
            return Message.Subject;
        }
        set
        {
            Message.Subject = value;
        }
    }

    #endregion

    #region mail type

    /// <summary>
    /// Gets or sets a value that determines whether the mail message
    /// should be formatted as HTML or text
    /// </summary>
    public bool IsHtmlMessage
    {
        get
        {
            return Message.IsBodyHtml;
        }
        set
        {
            Message.IsBodyHtml = value;
        }
    }

    #endregion

    #region sender

    /// <summary>
    /// Gets or sets the from address of this message
    /// </summary>
    public string From
    {
        get
        {
            return Message.From.Address;
        }
        set
        {
            Message.From = new MailAddress(value);
        }
    }

    #endregion

    #region recipients

    /// <summary>
    /// Gets the collection of recipients
    /// </summary>
    public MailAddressCollection To
    {
        get
        {
            return Message.To;

        }
    }

    /// <summary>
    /// Gets the collection of CC recipients 
    /// </summary>
    public MailAddressCollection CC
    {
        get
        {
            return Message.CC;
        }
    }

    /// <summary>
    /// Gets the collection of Bcc recipients
    /// </summary>
    public MailAddressCollection Bcc
    {
        get
        {
            return Message.Bcc;
        }
    }

    #endregion

    #region delivery notification

    /// <summary>
    /// Gets or sets the delivery notification settings for this message
    /// </summary>
    public DeliveryNotificationOptions DeliveryNotifications
    {
        get
        {
            return Message.DeliveryNotificationOptions;
        }
        set
        {
            Message.DeliveryNotificationOptions = value;
        }
    }

    #endregion

    #region priority

    /// <summary>
    /// Gets or sets the Priority of this message
    /// </summary>
    public MailPriority PriorityLevel
    {
        get
        {
            return Message.Priority;
        }
        set
        {
            Message.Priority = value;
        }
    }

    #endregion

    #region send methods

    /// <summary>
    /// Sends the message anonymously (without credentials)
    /// </summary>
    public void Send()
    {
        SmtpMailClient.Send(Message);
    }

    /// <summary>
    /// Sends the message with authorization from a network account   
    /// </summary>
    /// <param name="Username">The Windows username of the authorizing user</param>
    /// <param name="Password">The Windows password of the authorizing user</param>
    /// <param name="Domain">The domain name of the network to which the authorizing user belongs</param>
    public void Send(string Username, string Password, string Domain)
    {
        //attach a network credential to this message using the information passed into the method
        SmtpMailClient.Credentials = new NetworkCredential(Username, Password, Domain);

        //send the message
        SmtpMailClient.Send(Message);
    }

    #endregion

    #region IDisposable implementation

    ~SmtpMailMessage()
    {
        Dispose(false);
    }

    public void Dispose()
    {
        Dispose(true);            
    }

    protected virtual void Dispose(bool disposing)
    {
        if (disposing)
        {
            if (Message != null)
                Message.Dispose();
            Message = null;                
            SmtpMailClient = null;
        }
    }

    #endregion        
}
James Johnson
  • 45,496
  • 8
  • 73
  • 110
0
string BODY = String.Empty;
string PageName = "[Path to resource]"; //example C:\DataSource\Website\DomainName\RousourceFolder\page.html

BODY = new StreamReader(PageName).ReadToEnd();
CBRRacer
  • 4,649
  • 1
  • 23
  • 27