Tags | send email with attachment c#, asp.net send email c# Gmail, asp net send email example c#, asp.net send email c# to gmail, how to send email with attachment in asp.net using c#
- Upload and Download File using ASP.Net
- What is Bulk Email Service?
Send Email with Attachment C#
In this asp.net c# tutorial, you will learn that sending an email with an attachment using C# ASP.Net. First, you will need to import the System.Net.Mail namespace. You will learn how to send email using Gmail SMTP Server in ASP.Net.
The System.Net.Mail namespace contains the SmtpClient and MailMessage Classes that we need in order to send the email and the message attachment. Find the below asp.net send email source code:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" 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> <title>asp.net send email attachment fileupload</title> <link href="ui.css" rel="stylesheet" /> </head> <body> <form id="form1" runat="server"> <br /> <div> <br /> <table width="600" border="0" align="center" cellpadding="5" cellspacing="1" bgcolor="#5482fc"> <tr> <td height="50" align="center" class="lgHeader1"> <h2>ASP.Net Send Email with Attachment</h2> </td> </tr> </table> <br /> <table width="600" border="0" align="center" cellpadding="5" cellspacing="1" bgcolor="#cccccc"> <tr> <td width="100" align="right" bgcolor="#eeeeee" class="header1"> To </td> <td bgcolor="#FFFFFF"> <asp:TextBox ID="txtTo" runat="server" Columns="50"></asp:TextBox> </td> </tr> <tr> <td width="100" align="right" bgcolor="#eeeeee" class="header1"> Subject </td> <td bgcolor="#FFFFFF"> <asp:TextBox ID="txtSubject" runat="server" Columns="50"></asp:TextBox> </td> </tr> <tr> <td width="100" align="right" bgcolor="#eeeeee" class="header1"> Body </td> <td bgcolor="#FFFFFF"> <asp:TextBox ID="txtBody" runat="server" Columns="40" TextMode="MultiLine"></asp:TextBox> </td> </tr> <tr> <td align="right" bgcolor="#eeeeee" class="header1"> Attachment </td> <td bgcolor="#FFFFFF"> <asp:FileUpload ID="fuAttachment" runat="server" /> </td> </tr> <tr> <td width="100" align="right" bgcolor="#eeeeee" class="header1"> Gmail Email </td> <td bgcolor="#FFFFFF"> <asp:TextBox ID="txtEmail" runat="server" Columns="50"></asp:TextBox> </td> </tr> <tr> <td align="right" bgcolor="#eeeeee" class="header1"> Gmail Password </td> <td bgcolor="#FFFFFF"> <asp:TextBox ID="txtPassword" TextMode="Password" runat="server" Columns="50"></asp:TextBox> </td> </tr> <tr> <td align="right" bgcolor="#eeeeee" class="header1">Action </td> <td bgcolor="#FFFFFF"> <asp:Button ID="btnSubmit" runat="server" Text="Send Email" OnClick="btnSubmit_Click" /> </td> </tr> <tr> <td width="100" align="right" bgcolor="#eeeeee" class="header1">Status </td> <td bgcolor="#FFFFFF" class="basix"> <asp:Literal ID="litStatus" runat="server"></asp:Literal> </td> </tr> </table> <br /> </div> </form> </body> </html> using System; using System.Net.Mail; using System.IO; using System.Net; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void btnSubmit_Click(object sender, EventArgs e) { try { string to = txtTo.Text; string from = txtEmail.Text; string subject = txtSubject.Text; string body = txtBody.Text; using (MailMessage mm = new MailMessage(txtEmail.Text, txtTo.Text)) { mm.Subject = txtSubject.Text; mm.Body = txtBody.Text; if (fuAttachment.HasFile) { string FileName = Path.GetFileName(fuAttachment.PostedFile.FileName); mm.Attachments.Add(new Attachment(fuAttachment.PostedFile.InputStream, FileName)); } mm.IsBodyHtml = false; SmtpClient smtp = new SmtpClient(); smtp.Host = "smtp.gmail.com"; smtp.EnableSsl = true; NetworkCredential NetworkCred = new NetworkCredential(txtEmail.Text, txtPassword.Text); smtp.UseDefaultCredentials = true; smtp.Credentials = NetworkCred; smtp.Port = 587; smtp.Send(mm); litStatus.Text = "Your message has been sent."; } } catch (Exception ex) { litStatus.Text = ex.ToString(); } } } |
Note: Make sure you have Turn On access to Less Secure Apps as shown below.
Did you find this post useful? I hope you liked this article. Please share with me your valuable suggestions and feedback.