Embedded Image E-mailing
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 smpt 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
(i) At-least VB 2005/2008 or Visual Web Developer 2005/2008
(ii) IIS configured
(iii) SMTP of mail server (smtp.domain.com)
(iv) User name of mail server (email id – [email protected])
(v) Password of mail server (password - xxxxxxxxxx)
(i) At-least VB 2005/2008 or Visual Web Developer 2005/2008
(ii) IIS configured
(iii) SMTP of mail server (smtp.domain.com)
(iv) User name of mail server (email id – [email protected])
(v) 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.
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.
Codes 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>
</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> </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"
/> </p>
<p>
<strong>Your Message</strong></p>
<p>
<asp:TextBox ID="txtBody" runat="server"
TextMode="MultiLine" Rows="6" Columns="60"
/> </p>
<p>
<asp:FileUpload ID="fileImage" runat="server"
Width="400px" />
</p>
<p>
<asp:Button ID="btnSendEmail" runat="server"
Text="Send Email" />
</p>
</asp:Panel>
</p>
<p>
</p>
</div>
</form>
</body>
</html>
Code for Default.aspx.vb Page
Note: To execute this, no any changes required.
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="[email protected]"
password="xxxxxxxxx"
port="25"/>
</smtp>
</mailSettings>
</system.net>
</configuration>
Now execute your project to send embedded image emails.
Comments
Post a Comment