Blue Theme Orange Theme Green Theme Red Theme
 
Discover the top 5 tips for understanding .NET Interop
Home | Forums | Videos | Photos | Blogs | Beginners | Advertise with Us
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
DevExpress UI Controls
Search :       Advanced Search »
Home » ASP.NET and Web » Embedded Image E-mailing in VB.Net

Embedded Image E-mailing in VB.Net

This article will explain how to send email with embedded image.

Author Rank :
Page Views : 1924
Downloads : 33
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
EmbadedImageEmailing.zip
 
 
Mindcracker MVP Summit 2012
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 


Introduction

We have seen always that how to email? How attach some file in our emails and even how to e-mail multiple contacts at a time. But never seen, how to email with an embedded image. This article will explain how to send email with embedded image. In most of the article explained with credentials within the same, like we working on emailing.aspx page then we will include our all SMTP credentials within emailing.aspx.cs page. But we will use here our credentials from configuration file, so that we can use the same credential for any other usage. 

Requirements

If you want to run this project then you should have following details 
  1. At-least VB 2005/2008 or Visual Web Developer 2005/2008
  2. IIS configured
  3. SMTP of mail server (smtp.domain.com)
  4. User name of mail server (email id – username@domain.com)
  5. Password of mail server (password - xxxxxxxxxx)
SMTP Details

It is short of Simple Mail Transfer Protocol (SMTP). It is an Internet based standard for emailing, SMTP transmissions usage Internet Protocol (IP). Every email servers has its own SMTP and it has special form as given below

smtp.domainname.com

For example 
Gmail has: smtp.gmail.com

Many email servers charge for its smtp and pop (Post Office Protocol) services. You have to configure your own smtp to this project. 

Explanation

Create a new website named 'EmbadedImageEmailDemo', it will open the following pages by default Default.aspx, Default.aspx.vb, Web.config. Remember, you can only use the 'From' email id same as the credentials you have used in configuration. 

Code for Default.aspx Page

Note: In this page, change only 'From' email id textbox. 

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>EmbededImageEmailDemo</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <p>
        <strong><span style="font-size: 16pt">Embaded Image Email Demo Project</span></strong></p>
        <strong><span style="font-size: 16pt">
            <hr />
        </span></strong>
        <p>
            <asp:Panel ID="Panel2" runat="server" Height="24px" Visible="False" Width="503px">
                <span style="font-size: 16pt; color: #0000ff"><strong>You email has been sent.</strong></span></asp:Panel>
            &nbsp;</p>
        <p>
            <asp:Panel ID="Panel1" runat="server" Height="50px" Width="417px">
                <strong>
            From</strong><p>
            <asp:TextBox ID="txtFrom" runat="server" Enabled="False" >your smtp email id</asp:TextBox>&nbsp;</p>
    <p>
        <strong>
        To</strong></p>
        <p>
            <asp:TextBox ID="txtTo" runat="server" />
        </p>
    <p>
        <strong>
        Subject</strong></p>
        <p>
            <asp:TextBox ID="txtSubject" runat="server" />&nbsp;</p>
    <p>
        <strong>Your Message</strong></p>
        <p>
            <asp:TextBox ID="txtBody" runat="server" TextMode="MultiLine" Rows="6" Columns="60" />&nbsp;</p>
    <p>
    <asp:FileUpload ID="fileImage" runat="server" Width="400px" />
    </p>
    <p>
    <asp:Button ID="btnSendEmail" runat="server" Text="Send Email" />
    </p>
            </asp:Panel>
            &nbsp;</p>
        <p>
            &nbsp;</p>
    </div>
    </form>
</body>
</html>

Code for Default.aspx.vb Page

Partial Class _Default
    Inherits System.Web.UI.Page
    Protected Sub btnSendEmail_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSendEmail.Click
        If fileImage.HasFile Then
            Dim mailMessage As System.Net.Mail.MailMessage = New System.Net.Mail.MailMessage()
            mailMessage.From = New System.Net.Mail.MailAddress(txtFrom.Text.Trim())
            mailMessage.To.Add(txtTo.Text.Trim())
            mailMessage.Subject = txtSubject.Text.Trim()
            Dim plainTextView As System.Net.Mail.AlternateView = System.Net.Mail.AlternateView.CreateAlternateViewFromString(txtBody.Text.Trim(), Nothing, "text/plain")
            Dim htmlView As System.Net.Mail.AlternateView = System.Net.Mail.AlternateView.CreateAlternateViewFromString(txtBody.Text.Trim() + "<image src=cid:HDIImage>", Nothing, "text/html")
            Dim imageResource As New System.Net.Mail.LinkedResource(fileImage.PostedFile.FileName)
            imageResource.ContentId = "HDIImage"
            htmlView.LinkedResources.Add(imageResource)
            mailMessage.AlternateViews.Add(plainTextView)
            mailMessage.AlternateViews.Add(htmlView)
            Dim smtpClient As New System.Net.Mail.SmtpClient()
            smtpClient.Send(mailMessage)
            Panel1.Visible = False
            Panel2.Visible = True
        End If
    End Sub
End Class

Codes for Web.Config Page

Note: You have to modify the credential as per your own. Don't modify any tag otherwise it will cause to errors. 

<?xml version="1.0"?>
<configuration>
          <appSettings/>
          <connectionStrings/>
          <system.web>
                   <compilation debug="true"/>
                   <authentication mode="Windows"/>
          </system.web>
  <system.net>
    <mailSettings>
      <smtp>
        <network host="smtp.domain.com"
          userName="username@domain.com"
          password="xxxxxxxxx"
          port="25"/>
      </smtp>
    </mailSettings>
  </system.net>
</configuration>

Now execute your project to send embedded image emails. 

HAVE A HAPPY CODING!

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Abhimanyu Kumar Vatsa
http://www.itorian.com/about/
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the top 5 tips for understanding .NET
Ricky Leeks presents the top 5 tips for understanding .NET Interoperability. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Team Foundation Server Hosting
Become a Sponsor
 Comments
DevExpress Free UI Controls
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.