Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Asp.net send email with images using Gmail credentials

Dec 20, 2010
Introduction:

 In this article I will explain how to send email by embedding images using Gmail credentials in asp.net

Description:

Generally we will send mails using SMTP server suppose if we don’t have smtp server in that situation we can send mails by using gmail smtp server in asp.net. I will explain how to implement mail sending concept using Gmail credentials using asp.net. 


To implement this concept first you need to enable POP enable in your Gmail account for this Settings-->Forwarding and POP/IMAP


To implement this mail concept in your asp.net application first we need to add this reference to our application System.Net.Mail namespace

What is System.Net.Mail ?

The System.Net.Mail is a namespace which contains classes to send electronic mail through Simple Mail Transfer Protocol (SMTP) server for delivery.

How we can get this reference (System.Net.Mail)

for that first add System.Net.dll reference to our application.
a    a) On the Project menu, click Add Reference.
b    b) On the .NET tab, locate System.Net.dll, and then click Select.
c    c) Click OK in the Add References.

After that design your aspx page like this


<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Send Mail using with gmail crendentials in asp.net</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table style=" border:1px solid" align="center">
<tr>
<td colspan="2" align="center">
<b>Send Mail using gmail credentials in asp.net</b>
</td>
</tr>
<tr>
<td>
Gmail Username:
</td>
<td>
<asp:TextBox ID="txtUsername" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Gmail Password:
</td>
<td>
<asp:TextBox ID="txtpwd" runat="server" TextMode="Password"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Subject:
</td>
<td>
<asp:TextBox ID="txtSubject" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
To:
</td>
<td>
<asp:TextBox ID="txtTo" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Attach a file:
</td>
<td>
<asp:FileUpload ID="fileUpload1" runat="server" />
</td>
</tr>
<tr>
<td valign="top">
Body:
</td>
<td>
<asp:TextBox ID="txtBody" runat="server" TextMode="MultiLine" Columns="30" Rows="10" ></asp:TextBox>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="btnSubmit" Text="Send" runat="server" onclick="btnSubmit_Click" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
After that add following namcespace in your codebehind


using System.Net.Mail;
Now write following code in button click


protected void btnSubmit_Click(object sender, EventArgs e)
{
try
{
MailMessage Msg = new MailMessage();
// Sender e-mail address.
Msg.From = new MailAddress(txtUsername.Text);
// Recipient e-mail address.
Msg.To.Add(txtTo.Text);
Msg.Subject = txtSubject.Text;
// File Upload path
String FileName = fileUpload1.PostedFile.FileName;
string mailbody = txtBody.Text + "<br/><img src=cid:companylogo>";
LinkedResource myimage = new LinkedResource(FileName);
// Create HTML view
AlternateView htmlMail = AlternateView.CreateAlternateViewFromString(mailbody, null, "text/html");
// Set ContentId property. Value of ContentId property must be the same as
// the src attribute of image tag in email body. 
myimage.ContentId = "companylogo";
htmlMail.LinkedResources.Add(myimage);
Msg.AlternateViews.Add(htmlMail);
// your remote SMTP server IP.
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.Credentials=new System.Net.NetworkCredential(txtUsername.Text,txtpwd.Text);
smtp.EnableSsl = true;
smtp.Send(Msg);
Msg = null;
Page.RegisterStartupScript("UserMsg", "<script>alert('Mail sent thank you...');if(alert){ window.location='SendMail.aspx';}</script>");
}
catch (Exception ex)
{
Console.WriteLine("{0} Exception caught.", ex);
}
}
Demo

our output mail is like this


Other related posts are




If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

14 comments :

Anonymous said...

Article is Very Helpful and cool.
I got an error in uploadind the image, hence unale to send image

komal agrawal said...

really all examples are very usefull and very very helpfull..thank u soooo much

g said...

i used your post for how to send mail with images using Gmail credentials in asp.net or how to send mail with images using asp.net

but there is come some error

Could not find file 'C:\Program Files\Common Files\Microsoft Shared\DevServer\10.0\mahesh.JPG'.

Anonymous said...

hi,
how to set img src=cid:companylogo

Anonymous said...

Hi g
user following to linkResource
LinkedResource myimage = new LinkedResource(Server.MapPath("~//" +FileName), "image/jpg");

you will not get above error.

but there is another issue
at smtp.Send(Msg) line, it throws an exception ("Failure sending mail.")

i am not understanding why this error comes. while my gmail credentials are right.

Deepak Kumar said...

Could not find file 'C:\Program Files\Common Files\Microsoft Shared\DevServer\10.0\image_name.jpg

this error is thrown by the program.
give some solution regarding this..

RAJESH MCA said...

i can' able to attach the image while sednig mail.can u do a favour me?

Anonymous said...

Before using LinkedResource
save the file in some folder the code will work !!!!

kranthi kumar said...
This comment has been removed by the author.
kranthi kumar said...

i run the above code without attachment in .net framework 4.5,vs 2012.It's working fine but showing error

Vivek Tiwari said...

superb, thank you friend

SHAMAL JADHAV said...

Nice article it's really helpful ...................................

Anonymous said...

superb, thank you

Maru ashish said...

error in this code.. plz. corrent then...

Give your Valuable Comments

Other Related Posts

© 2010-2012 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.