Infolink

 

Search This Blog

Sep 16, 2012

Include Image in Mail

Include an image in the text mail that my system is sending it using Mail function

/*For sending string imagePath = Server.MapPath("~") + "\\";
                string fileName = imagePath + "EmailNotification.jpg";
                AlternateView av = AlternateView.CreateAlternateViewFromString(body, null, MediaTypeNames.Text.HTML)
                LinkedResource linkedRes = new LinkedResource(fileName);
                linkedRes.ContentId = "image1";
                linkedRes.ContentType.Name = fileName;
                av.LinkedResources.Add(linkedRes);


/*For sending an email notification to the new user*/


protected void SendNotificationByEmail(string toAddresses, string fromAddress, string MailSubject, string MessageBody, bool isBodyHtml)
{
        SmtpClient sc = new SmtpClient("MAIL.Aramco.com");
        try
        {
            MailMessage msg = new MailMessage();
            msg.From = new MailAddress("pssp@aramco.com", "PMOD Safety Services Portal (PSSP)");


            // In case the mail system doesn't like no to recipients. This could be removed
            //msg.To.Add("pssp@aramco.com");

            msg.Bcc.Add(toAddresses);
            msg.Subject = MailSubject;
            msg.Body = MessageBody;
            msg.IsBodyHtml = isBodyHtml;
            sc.Send(msg);
        }
        catch (Exception ex)
        {
            throw ex;
        }

}

protected void SendEmailToUser(string username)
{
        string connString = "Data Source=localhost\\sqlexpress;Initial Catalog=psspdbTest;Integrated           Security=True";

        string networkID = username.ToString();

        //and append (@aramco.com) to it.
        using (SqlConnection conn = new SqlConnection(connString))
        {
            var sbEmailAddresses = new System.Text.StringBuilder(2000);

            //initiate the varibles that will be retreived from the database
            string name = null;

            // Open DB connection.
            conn.Open();

            string cmdText2 = @"SELECT     Name
                                FROM       dbo.employee
                                WHERE     (Username = @networkID)";
            using (SqlCommand cmd = new SqlCommand(cmdText2, conn))
            {
                cmd.Parameters.AddWithValue("@networkID", networkID);
                SqlDataReader reader = cmd.ExecuteReader();
                if (reader != null)
                {
                    if (reader.Read())
                    {
                        name = reader["Name"].ToString();
                        sbEmailAddresses.Append(username).Append("@aramco.com");
                    }
                }

                //var sEMailAddresses = sbEmailAddresses.ToString();
                string body = @"Good day "
                                + name +
                                @", <br /><br />
                                You have been added to the <a href='http://pmv/pssp/Default.aspx'>PMOD Safety Services Portal (PSSP)</a>.
                                <br /><br />
                                PSSP is a home for your safety profile and a path for building a strong safety culture in your life.
                                <br />
                                Give yourself a chance to gain increased safety awareness by surfing the system,
                                participating in the short weekly safety quizzes, reading the PMOD
                                Newsletter and so many other safety services and resources.
                                </b> <br /> <br />
                                <img src='images/Admin/EmailNotification.jpg' alt='PSSP-Welcome Message'/>
                                <br /> <br />
                                This email was generated using the <a href='http://pmv/pssp/Default.aspx'>PMOD Safety Services Portal (PSSP) </a>.
                                Please do not reply to this email.";

                string imagePath = Server.MapPath("~") + "\\";
                string fileName = imagePath + "EmailNotification.jpg";
                AlternateView av = AlternateView.CreateAlternateViewFromString(body, null, MediaTypeNames.Text)
                LinkedResource linkedRes = new LinkedResource(fileName);
                linkedRes.ContentId = "image1";
                linkedRes.ContentType.Name = fileName;
                av.LinkedResources.Add(linkedRes);

                SendNotificationByEmail(sbEmailAddresses.ToString(), "", "Welcome to PMOD Safety Services Portal (PSSP)", body, true);
                sbEmailAddresses.Clear();
                reader.Close();


            }

            conn.Close();
        }
}
Related Posts Plugin for WordPress, Blogger...